简体   繁体   中英

How do I add cover image to a mp3 file using mutagen in Python?

The code below doesnt seem to update the artwork of the mp3 file. Code:-

from mutagen.id3 import ID3, APIC
audio = ID3(musicFilename)
with open(coverFilename, 'rb') as albumart:
    print albumart.read()
    audio['APIC'] = APIC(
        encoding=3,
        mime='image/jpeg',
        type=3, desc=u'Cover',
        data=albumart.read()
        )
audio.save()

After running the script, the cover of the mp3 file remain empty.

The problem is your code is that you did print albumart.read() , this will make the cursor of the reader to the end of the file, now when you read it again it will be empty. Your solution is right, just remove the print command. this is my tested solution.

from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error

audio = MP3('example.mp3', ID3=ID3)    
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()
    )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM