Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutagen's save() does not set or change cover art for MP3 files

I am trying to use Mutagen for changing ID3 (version 2.3) cover art for a bunch of MP3 files in the following way:

from mutagen.mp3 import MP3
from mutagen.id3 import APIC

file = MP3(filename)

with open('Label.jpg', 'rb') as albumart:
    file.tags['APIC'] = APIC(
        encoding=3,
        mime='image/jpeg',
        type=3, desc=u'Cover',
        data=albumart.read()
    )
file.save(v2_version=3)

However, the file (or at least the APIC tag) remains unchanged, as checked by reading the tag back. In the system file explorer, the file does show an updated Date modified, however. How can I get Mutagen to update the cover art correctly?

like image 845
EmielBoss Avatar asked Jan 20 '20 00:01

EmielBoss


1 Answers

The issue is coming about due to the ID3 specification stating that:

There may be several pictures attached to one file, each in their individual "APIC" frame, but only one with the same content descriptor.

This means that ID3 has to store APIC tags using ['APIC:Description']. In addition, the recommended way to add tags is not directly through the dictionary interface as in the example in the question, but using the ID3.add() function. Using the ID3 object also allows us to use the ID3.getall() function to check if the tag has been correctly attached.

from mutagen.id3 import APIC, ID3
file = ID3("test.mp3")

print(file.getall('APIC')) # [] (assuming no APIC tags attached)

with open('image.jpg', 'rb') as albumart:
    file.add(APIC(
        encoding=3,
        mime='image/jpeg',
        type=3, desc=u'Cover',
        data=albumart.read()
    ))

print(file.getall('APIC'))
# [APIC(encoding=<Encoding.UTF16: 1>, mime='image/jpeg', type=<PictureType.COVER_FRONT: 3>, desc='Cover', data=...]
file.save(v2_version=3)
like image 180
CDJB Avatar answered Nov 11 '22 20:11

CDJB