有没有一个我可以使用的维护好的 Python 包来检索和设置 MP3 ID3 元数据?
python setup.py install
。 import eyeD3
tag = eyeD3.Tag()
tag.link("/some/file.mp3")
print tag.getArtist()
print tag.getAlbum()
print tag.getTitle()
读取 MP3 文件(轨道长度、比特率等参数)并访问其标签:
if eyeD3.isMp3File(f):
audioFile = eyeD3.Mp3AudioFile(f)
tag = audioFile.getTag()
可以选择特定的标签版本:
tag.link("/some/file.mp3", eyeD3.ID3_V2)
tag.link("/some/file.mp3", eyeD3.ID3_V1)
tag.link("/some/file.mp3", eyeD3.ID3_ANY_VERSION) # The default.
或者您可以遍历原始帧:
tag = eyeD3.Tag()
tag.link("/some/file.mp3")
for frame in tag.frames:
print frame
一旦标签与文件关联,就可以修改和保存:
tag.setArtist(u"Cro-Mags")
tag.setAlbum(u"Age of Quarrel")
tag.update()
如果链接的标签是v2,而你想将其保存为v1: tag.update(eyeD3.ID3_V1_1)
读取标签并将其从文件中移除:
tag.link("/some/file.mp3")
tag.remove()
tag.update()
添加一个新标签:
tag = eyeD3.Tag()
tag.link('/some/file.mp3') # no tag in this file, link returned False
tag.header.setVersion(eyeD3.ID3_V2_3)
tag.setArtist('Fugazi')
tag.update()
import eyed3
(小写字母d)。 - Jake Zeyed3
对于常见的 MP3 文件也会抛出错误。最好使用 mutagen
类中的 EasyID3
,如 此答案 所示。 - Chris Redford我以前使用过mutagen来编辑媒体文件的标签。mutagen的好处是它可以处理其他格式,如mp4、FLAC等。 我使用这个API编写了几个脚本,并且取得了很大的成功。
eyed3
存在的一个问题是对于普通的MP3文件,它会抛出NotImplementedError("Unable to write ID3 v2.2")
。
根据我的经验,mutagen
类中的EasyID3
更加可靠。例如:
from mutagen.easyid3 import EasyID3
audio = EasyID3("example.mp3")
audio['title'] = u"Example Title"
audio['artist'] = u"Me"
audio['album'] = u"My album"
audio['composer'] = u"" # clear
audio.save()
通过这种方式可以访问和保存所有其他标签,这将满足大多数需求。更多信息可以在Mutagen教程中找到。
from ID3 import *
try:
id3info = ID3('file.mp3')
print id3info
# Change the tags
id3info['TITLE'] = "Green Eggs and Ham"
id3info['ARTIST'] = "Dr. Seuss"
for k, v in id3info.items():
print k, ":", v
except InvalidTagError, message:
print "Invalid ID3 tag:", message
在尝试了这里推荐的简单的pip install
方法后,我发现这第四个选项是唯一可行的。其余的都出现了C++中缺少依赖项的导入错误,或者是一些魔法或其他库,pip
错过了它们。因此,为了基本的ID3标签读取(所有版本),请使用以下方法:
https://pypi.python.org/pypi/tinytag/0.18.0
from tinytag import TinyTag
tag = TinyTag.get('/some/music.mp3')
TinyTag可能获取的属性列表:
tag.album # album as string
tag.albumartist # album artist as string
tag.artist # artist name as string
tag.audio_offset # number of bytes before audio data begins
tag.bitrate # bitrate in kBits/s
tag.disc # disc number
tag.disc_total # the total number of discs
tag.duration # duration of the song in seconds
tag.filesize # file size in bytes
tag.genre # genre as string
tag.samplerate # samples per second
tag.title # title of the song
tag.track # track number as string
tag.track_total # total number of tracks as string
tag.year # year or data as string
看看这个:
https://github.com/Ciantic/songdetails
使用示例:
>>> import songdetails
>>> song = songdetails.scan("data/song.mp3")
>>> print song.duration
0:03:12
保存更改:
>>> import songdetails
>>> song = songdetails.scan("data/commit.mp3")
>>> song.artist = "Great artist"
>>> song.save()
import songdetails
song = songdetails.scan("blah.mp3")
if song is not None:
print song.artist
import songdetails
song = songdetails.scan("blah.mp3")
if song is not None:
song.artist = u"The Great Blah"
song.save()
在学会中文之前,请不要忘记在名字前添加u。
您可以使用Python的glob模块进行批量阅读和编辑。
例如:
import glob
songs = glob.glob('*') # script should be in directory of songs.
for song in songs:
# do the above work.
我使用 tinytag 1.3.1 的原因是
1.3.0 (2020-03-09):
added option to ignore encoding errors ignore_errors #73
Improved text decoding for many malformed files
MP3 (ID3 v1, v1.1, v2.2, v2.3+)
Wave/RIFF
OGG
OPUS
FLAC
WMA
MP4/M4A/M4B
from tinytag import TinyTag
fileNameL ='''0bd1ab5f-e42c-4e48-a9e6-b485664594c1.mp3
0ea292c0-2c4b-42d4-a059-98192ac8f55c.mp3
1c49f6b7-6f94-47e1-a0ea-dd0265eb516c.mp3
5c706f3c-eea4-4882-887a-4ff71326d284.mp3
'''.split()
for fn in fileNameL:
fpath = './data/'+fn
tag = TinyTag.get(fpath)
print()
print('"artist": "%s",' % tag.artist)
print('"album": "%s",' % tag.album)
print('"title": "%s",' % tag.title)
print('"duration(secs)": "%s",' % tag.duration)
JoeTagPj>python joeTagTest.py
"artist": "Conan O’Brien Needs A Friend",
"album": "Conan O’Brien Needs A Friend",
"title": "17. Thomas Middleditch and Ben Schwartz",
"duration(secs)": "3565.1829583532785",
"artist": "Conan O’Brien Needs A Friend",
"album": "Conan O’Brien Needs A Friend",
"title": "Are you ready to make friends?",
"duration(secs)": "417.71840447045264",
"artist": "Conan O’Brien Needs A Friend",
"album": "Conan O’Brien Needs A Friend",
"title": "Introducing Conan’s new podcast",
"duration(secs)": "327.22187551899646",
"artist": "Conan O’Brien Needs A Friend",
"album": "Conan O’Brien Needs A Friend",
"title": "19. Ray Romano",
"duration(secs)": "3484.1986772305863",
C:\1d\PodcastPjs\JoeTagPj>