Python Mutagen添加图像封面不起作用

时间:2017-03-08 07:08:21

标签: python image cover mutagen

我尝试使用mutagen编辑mp3标签。现在我让它设置像标题一样的标签。但是APIC仍然无法运作。

我的代码:

from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, TT2, TPE1, TRCK, TALB, USLT, error
# ID3 info:
# APIC: picture
# TIT2: title
# TPE1: artist
# TRCK: track number
# TALB: album
# USLT: lyric

pic_file = 'cover.jpg' # pic file
audio = MP3('song.mp3', ID3=ID3)
try:
    audio.add_tags()
except:
    pass
audio.tags.add(APIC(
    encoding=3,
    mime='image/jpeg',
    type=3,
    desc='Cover Picture',
    data=open(pic_file, encoding='ISO-8859-1').read().encode()
))
audio.tags.add(TT2(encoding=3, text='title'))
audio.tags.add(TALB(encoding=3, text='album'))
#audio.tags.add(TPE1(encoding=3, text=item['artist'].decode('utf-8')))
#audio.tags.add(TRCK(encoding=3, text=str(track_num).decode('utf-8')))
#audio.tags.add(USLT(encoding=3, lang=u'eng', desc=u'desc', text=item['lyric'].decode('utf-8')))
audio.save()
ID3('song.mp3').save(v2_version=3)

我怎样才能让它发挥作用?

谢谢:)

编辑: 搞定了:

imagedata = open(pic_file, 'rb').read()

id3 = ID3('song.mp3')
id3.add(APIC(3, 'image/jpeg', 3, 'Front cover', imagedata))
id3.add(TIT2(encoding=3, text='title'))

id3.save(v2_version=3)

1 个答案:

答案 0 :(得分:0)

解决方案是:

imagedata = open(pic_file, 'rb').read()

id3 = ID3('song.mp3')
id3.add(APIC(3, 'image/jpeg', 3, 'Front cover', imagedata))
id3.add(TIT2(encoding=3, text='title'))

id3.save(v2_version=3)