How do you embed album art into an MP3 using Pytho

2019-01-10 08:05发布

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.

5条回答
beautiful°
2楼-- · 2019-01-10 08:49

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.

查看更多
Rolldiameter
3楼-- · 2019-01-10 08:52

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.

查看更多
Lonely孤独者°
4楼-- · 2019-01-10 08:53

I've used the eyeD3 module to do this exact thing.

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()
查看更多
狗以群分
5楼-- · 2019-01-10 08:55

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

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

查看更多
不美不萌又怎样
6楼-- · 2019-01-10 09:07

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

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()
查看更多
登录 后发表回答