There is an errors embed album art with mutagen?

325 views Asked by At

The problem is, whenever i embed a picture into the mp3 and that mp3 file always increase less bytes than the picture actually is. I'll explain in code:

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

audio = ID3()
with open('111.mp3', 'rb') as f:
    rawfile = f.read()

for dirpath, dirnames, filenames in os.walk('E:/Exp/apic'):
    for fname in filenames:
        path = os.path.join(dirpath, fname)

        ap = APIC(
            encoding = 3,
            mime = 'image/jpg',
            type = 3,
            desc = u'Cover',
            data = open(path, 'rb').read() )

        sz_changed = os.stat('111.mp3').st_size
        audio.add(ap)
        audio.save('111.mp3')
        sz_changed = os.stat('111.mp3').st_size - sz_changed
        print u'Lost bytes:%d' % (len(ap.data)-sz_changed)
        with open('111.mp3', 'wb') as f:
            f.write(rawfile) #reset

the code shows that i can't embed the whole picture into that mp3 and neight my mp3 player nor my phone can read the broken art so what exactly the problem is? although i can successfully embed a pic only when i previously embed a much more larger pic.

now i am going to show the result of the above code:

Lost bytes:4174
Lost bytes:5594
Lost bytes:4924
Lost bytes:4777
Lost bytes:4938
Lost bytes:4750
Lost bytes:5558
Lost bytes:4710
Lost bytes:4924
Lost bytes:5686
Lost bytes:4936
Lost bytes:4788
Lost bytes:5319
Lost bytes:5605
Lost bytes:4790
Lost bytes:4781
Lost bytes:5692
Lost bytes:5248
1

There are 1 answers

0
Raffi On

Try changing:

mime = 'image/jpg' to 'image/jpeg'

I had a similar problem before and that helped