pythonpython-3.xmp3albumartmutagen

What kind of object is needed as first argument in mutagen.mp4.MP4Cover?


I would like to use the function mutagen.mp4.MP4Cover(data, imageformat) (direct link to documentation), however the documentation only specifies imageformat, but not what data is.

I have a .png file that I would like to add to an .mp3 as cover/album art.


Solution

  • "I have a .png file that I would like to add to an .mp3 as cover/album art."

    Is this MP3 data actually contained inside an MP4 or M4A format file? If not, then you cannot use .mp4.MP4Cover since the header of MP3 file does have any "slot" to insert some cover art. Containers like MP4 & M4A have a specific place built-in for adding cover (the covr atom).

    For a stand-alone MP3 file you must instead add ID3 metadata.
    Specfically you want to add a tag called APIC (Attached PIC) :

    from mutagen import id3, mp3
    file = mp3.MP3('test.mp3')
    
    imagedata = open('cover.png', 'rb').read()
    file.tags.add(id3.APIC(3, 'image/png', 3, 'Front cover', imagedata))
    file.save()