Search code examples
pythonmp3id3

Cannot Embed Cover Art To Mp3 in Python 3.5.2


I have this file "image.jp and this .mp3 file:

"Green Day - When I Come Around [Official Music Video].mp3"

in the directory "test"

I have already successfully set tags as Author, Title, Album and etc using eyeD3 library. and then I try to set the Cover Art.

I've tried two possibilities, but none of them worked: First one: Mutagen:

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

complete_file_path = "test\\"+"Green Day - When I Come Around [Official Music Video].mp3"
path_to_thumb_wf = "test\\"+"image.jpg"

audio = MP3(complete_file_path, ID3=ID3)
# add ID3 tag if it doesn't exist
try:
    audio.add_tags()
except error:
    pass

print(path_to_thumb_wf)

audio.tags.add(
    APIC(
        encoding=3, # 3 is for utf-8
        mime='image/jpg', # image/jpeg or image/png
        type=3, # 3 is for the cover image
        desc=u'Cover',
        data=open(path_to_thumb_wf, 'rb').read()
    )
)
audio.save(v2_version=3)

And the solution using eyeD3

audiofile = eyed3.load(complete_file_path)


# read image into memory
imagedata = open(path_to_thumb_wf,"rb").read()

# append image to tags
audiofile.tag.images.set(3,imagedata,"image/jpeg", u"you can put a description here")

audiofile.tag.save()

I'm using python 3.5.2 on Windows 10. And i don't know if it could influence the result but i'll say anyway, the song has already a cover art that I'd like to change.


Solution

  • As explained in the ID3v2.3 section on APIC:

    There may be several pictures attached to one file, each in their individual "APIC" frame, but only one with the same content descriptor. There may only be one picture with the picture type declared as picture type $01 and $02 respectively.

    In v2.3, IIRC, "content descriptor" isn't actually documented anywhere, so different clients may do slightly different things here, but most tools will treat it either as the picture type plus description string, or as the entire header (text encoding, MIME type, picture type, and encoded description) as a binary blob. (And some tools just ignore it and allow you to store pictures with completely identical frame headers, but I don't think that's relevant with Mutagen.)

    At any rate, this means you're probably just adding another Cover (front) picture, named 'Cover', rather than replacing any existing one.


    You haven't explained how you're looking at the file. But I'm guessing you're trying to open it in Windows Media Player or iTunes or some other player, or view it in Windows Explorer (which I think just asks WMP to read the tag), or something like that?

    Almost all such tools, when faced with multiple images, just show you the first one. (Some of them don't even distinguish on picture type, and show you the first image of any type, even if it's a 32x32 file icon…)

    Some do have a way to view the other pictures, however. For example, in iTunes, if you Get Info or Properties on the track, then go to the Cover Art or similar tab (sorry for the vagueness, but the names have changed across versions), you can see all of the pictures in the tag.


    At any rate, if you want to replace the APIC with a different one, you either need to exactly match the descriptor (and, again, that can mean different things to different libraries…), or, more simply, just delete the old one as well as adding the new one.


    One more thing to watch out for: both iTunes and WMP cache cover art, and assume that it's never going to change once the file has been imported. And WMP also has various things that can override the image in the file, such as a properly-UUID'd folder cover art image in the same directory.