How do you embed album art into an MP3 using Python?

Connor picture Connor · Jan 3, 2009 · Viewed 20.5k times · Source

I've been using mutagen for reading and writing MP3 tags, but I want to be able to embed album art directly into the file.

Answer

Grégoire Cachet picture Grégoire Cachet · Dec 21, 2009

Here is how to add example.png as album cover into example.mp3 with mutagen:

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()