我一直在使用Mutagen来读写MP3标签,但我希望能够直接将专辑封面嵌入到文件中。
以下是使用mutagen将example.png添加为example.mp3专辑封面的方法:
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error
audio = MP3('example.mp3', ID3=ID3)
# add ID3 tag if it doesn't exist
try:
audio.add_tags()
except error:
pass
audio.tags.add(
APIC(
encoding=3, # 3 is for utf-8
mime='image/png', # image/jpeg or image/png
type=3, # 3 is for the cover image
desc=u'Cover',
data=open('example.png').read()
)
)
audio.save()
TypeError: 'str' object cannot be interpreted as an integer
。完整的跟踪和源代码在这里:https://gist.github.com/brutus/69c51cd706a6cf5d1594 - Brutusv2_version=3
是必要的,这样新标签才能被Windows 7读取,并且我通过直接读入一个ID3
对象来简化了这段代码。否则,这个答案是完美的,没有其他人报告的错误。 - Mark Ransomdata=open('example.png', 'rb').read()
。 - codezjx我已经使用eyeD3模块完成了这个任务。
def update_id3(mp3_file_name, artwork_file_name, artist, item_title):
#edit the ID3 tag to add the title, artist, artwork, date, and genre
tag = eyeD3.Tag()
tag.link(mp3_file_name)
tag.setVersion([2,3,0])
tag.addImage(0x08, artwork_file_name)
tag.setArtist(artist)
tag.setDate(localtime().tm_year)
tag.setTitle(item_title)
tag.setGenre("Trance")
tag.update()
audiofile = eyed43.load(file_name)
和audiofile.tag.artist = artist
等。请参见:http://eyed3.nicfit.net/ - user3141031你是否正在尝试将图像嵌入到许多文件中?如果是这样,我找到了一个脚本(请参见链接),它可以遍历一组目录,查找图像,然后将它们嵌入到MP3文件中。当我想在我的(现在已经失效的)iPhone上实际查看CoverFlow中的内容时,这对我非常有用。