简体   繁体   English

如何在Python中使用诱变剂将封面图片添加到mp3文件中?

[英]How do I add cover image to a mp3 file using mutagen in Python?

The code below doesnt seem to update the artwork of the mp3 file. 下面的代码似乎没有更新mp3文件的插图。 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. 运行脚本后,mp3文件的封面保持空白。

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. 问题是您的代码是您确实print albumart.read() ,这会使阅读器的光标移到文件的末尾,现在当您再次阅读该文件时,它将为空。 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()
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM