有一个错误嵌入专辑艺术与mutagen?

时间:2013-08-09 12:00:28

标签: python mp3 id3 mutagen

问题在于,每当我将图片嵌入到mp3中时,mp3文件总是比图片实际上增加的字节数少。我将在代码中解释:

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

代码显示我无法将整个图片嵌入到该mp3中 而且我的MP3播放器和手机都可以阅读破碎的艺术品 究竟问题到底是什么? 虽然我只能在以前嵌入更大的图片时成功嵌入图片。

现在我将展示上述代码的结果:

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 个答案:

答案 0 :(得分:0)

尝试更改:

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

之前我遇到过类似的问题而且有帮助