我一直在使用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()
我用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()