How to add album art to mp3 file using python 3?
Asked Answered
P

3

20

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.

Palisade answered 21/7, 2016 at 17:27 Comment(0)
M
28

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. 3 is 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 jpg file, you'll want image/jpeg, and if it's a png file, you'll want image/png.
Monterrey answered 4/9, 2016 at 12:21 Comment(3)
You forgot one line: audiofile.tag.save()Chromite
@Chromite You're absolutely right. I've edited the code to add that line I'd left off the end. I'd left it off because I do a bunch more editing to tags before I save the tag!Monterrey
What about if i want to display the image from audiofile,tag,images[0]?Surprisal
T
9

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 shows the same cover image for all music files even if they don't have a cover image.

  • you'd better set a title too because file name won't be displayed in music players.

  • using the audio.initTag() can also wipe all meta information if you would want that.

audio.initTag()
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)
Tanhya answered 25/4, 2020 at 23:37 Comment(1)
This works a dream. Setting the version number does indeed make windows explorer pick up artwork image.Mature
M
1

As of this writing mutagen already works fine with python3.

Copy-paste from https://mcmap.net/q/662803/-how-do-i-add-cover-image-to-a-mp3-file-using-mutagen-in-python with the tiny fix of adding the binary file mode for the image which is required with python3.

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

audio = MP3(path_mp3, ID3=ID3)
audio.tags.add(
    APIC(
        encoding=3,  # 3 is for utf-8
        mime="image/png",  # can be image/jpeg or image/png
        type=3,  # 3 is for the cover image
        desc='Cover',
        data=open("cover.png", mode='rb').read()
    )
)

NOTE: you might want to wrap in a try-except in order to catch ID3NoHeaderError which will be raised on MP3 files without an ID3 tag. In those cases you can create the tag with e.g. tags = ID3()... followed by tags.save('myaudio.mp3')

Mendel answered 28/1, 2023 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.