如何使用 Python 将专辑封面嵌入 MP3?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/409949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:02:37  来源:igfitidea点击:

How do you embed album art into an MP3 using Python?

pythonmp3metadataid3albumart

提问by Grégtheitroade Cachet

I've been using mutagen for reading and writing MP3 tags, but I want to be able to embed album art directly into the file.

我一直在使用 mutagen 来读取和写入 MP3 标签,但我希望能够将专辑封面直接嵌入到文件中。

回答by Grégtheitroade Cachet

Here is how to add example.png as album cover into example.mp3 with mutagen:

以下是如何使用诱变剂将 example.png 作为专辑封面添加到 example.mp3 中:

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

audio = MP3('example.mp3', ID3=ID3)

# add ID3 tag if it doesn't exist
try:
    audio.add_tags()
except error:
    pass

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()
    )
)
audio.save()

回答by Owen

I've used the eyeD3module to do this exact thing.

我已经使用eyeD3模块来做这件事。

def update_id3(mp3_file_name, artwork_file_name, artist, item_title):    
    #edit the ID3 tag to add the title, artist, artwork, date, and genre
    tag = eyeD3.Tag()
    tag.link(mp3_file_name)
    tag.setVersion([2,3,0])
    tag.addImage(0x08, artwork_file_name)
    tag.setArtist(artist)
    tag.setDate(localtime().tm_year)
    tag.setTitle(item_title)
    tag.setGenre("Trance")
    tag.update()

回答by Andrew Cox

Looks like you have to add a special type of frame to the MP3. See the site on ID3 tags

看起来您必须向 MP3 添加一种特殊类型的框架。请参阅有关ID3 标签的站点

Also the tutorial for mutagen implies that you can add ID3 tags in mutagen see

此外,mutagen 的教程暗示您可以在 mutagen 中添加 ID3 标签,请参阅

回答by Christopher

Possible solution

可能的解决方案

Are you trying to embed images into a lot of files? If so, I found a script (see the link) that goes through a set of directories, looks for images, and the embeds them into MP3 files. This was useful for me when I wanted to actually have something to look at in CoverFlow on my (now defunct) iPhone.

您是否尝试将图像嵌入到大量文件中?如果是这样,我找到了一个脚本(见链接),它遍历一组目录,查找图像,并将它们嵌入到 MP3 文件中。当我想在我的(现已解散的)iPhone 上的 CoverFlow 中实际查看一些内容时,这对我很有用。

回答by jpoppe

A nice small CLI tool which helped me a lot with checking what I did while developing id3 stuff is mid3v2 which is the mutagen version of id3v2. It comes bundled with the Python mutagen library. The source of this little tool gave me also lots of answers about how to use mutagen.

一个不错的小型 CLI 工具帮助我检查我在开发 id3 时所做的事情是 mid3v2,它是 id3v2 的诱变剂版本。它与 Python mutagen 库捆绑在一起。这个小工具的来源也给了我很多关于如何使用诱变剂的答案。