如何使用 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
How do you embed album art into an MP3 using Python?
提问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
回答by Christopher
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 库捆绑在一起。这个小工具的来源也给了我很多关于如何使用诱变剂的答案。