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

2

7

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?

Barnet answered 20/1, 2020 at 0:31 Comment(0)
B
0

I needed to set the cover to the "APIC:" tag, instead of the "APIC" tag (which I guess is how IDv2.3 is specified).

Barnet answered 4/2, 2020 at 21:35 Comment(0)
K
5

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)
Krummhorn answered 5/2, 2020 at 10:25 Comment(0)
B
0

I needed to set the cover to the "APIC:" tag, instead of the "APIC" tag (which I guess is how IDv2.3 is specified).

Barnet answered 4/2, 2020 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.