2

The code below doesnt seem to update the artwork of the mp3 file. Code:-

from mutagen.id3 import ID3, APIC
audio = ID3(musicFilename)
with open(coverFilename, 'rb') as albumart:
    print albumart.read()
    audio['APIC'] = APIC(
        encoding=3,
        mime='image/jpeg',
        type=3, desc=u'Cover',
        data=albumart.read()
        )
audio.save()

After running the script, the cover of the mp3 file remain empty.

JTT
  • 109
  • 4
  • 14

1 Answers1

4

The problem is your code is that you did print albumart.read(), this will make the cursor of the reader to the end of the file, now when you read it again it will be empty. Your solution is right, just remove the print command. this is my tested solution.

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

audio = MP3('example.mp3', ID3=ID3)    
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()
    )
)
harshil9968
  • 3,254
  • 1
  • 16
  • 26
  • Even I delete the print command, the problem still exist. And I can't add the cover image with your code, either. My mutagen version is 1.38. – JTT Nov 17 '17 at 09:25
  • How can we do this for a m4a file? – Spurgeon Feb 08 '19 at 07:05
  • 1
    @Spurgeon By pure happenstance, I just stumbled upon https://stackoverflow.com/questions/57936384/mutagen-importing-artwork-from-url. – Hermann Feb 18 '23 at 20:42