'How to add album art to mp3 file using python 3?
I was wondering what module to use for setting an image as the album art for a particular mp3 file. Mutagen seemed to be a popular choice, but it doesn't seem to work on python 3 and I can't find any documentation.
Solution 1:[1]
Here's a modified version of the code I use. You will want to change the example.mp3 and cover.jpg (and perhaps the mime type too):
import eyed3
from eyed3.id3.frames import ImageFrame
audiofile = eyed3.load('example.mp3')
if (audiofile.tag == None):
audiofile.initTag()
audiofile.tag.images.set(ImageFrame.FRONT_COVER, open('cover.jpg','rb').read(), 'image/jpeg')
audiofile.tag.save()
tag.images.set() takes three arguments:
- Picture Type: This is the type of image it is.
3is the code for the front cover art. You can find them all here. - Image Data: This is the binary data of your image. In the example, I load this in using
open().read(). - Mime Type: This is the type of file the binary data is. If it's a
jpgfile, you'll wantimage/jpeg, and if it's apngfile, you'll wantimage/png.
Solution 2:[2]
an addition to answers above, here is what I struggled on for two days:
you have to set the ID3 version to "V2.3", otherwise the photo won't show up for the file icon. also you have to set a different album name for each MP3 file because otherwise the music player shits itself and shows the same AlbumCover for all music files even if they don't have any AlbumArt
audio.tag.title = u'your_title'
audio.tag.album = u'your_album_name'
audio.tag.images.set(3, open("cover.jpg", 'rb').read(), 'image/jpeg')
audio.tag.save(version=eyed3.id3.ID3_V2_3)
you'd better set a title too because file name won't be displayed in music players.
using the audio.initTag() can also wipe the tag information if you would want that.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
