In March 2019, I wrote about using Python with the "pytaglib"
library to read and modify audio files’ metadata. This solution
worked nicely for what I needed at the time, but as I rewrote my
WAV-to-MP3 conversion in Python, I found that it lacked support for
adding cover art to the files. After a bit of research, I found
eyed3. It also comes with a nice side-effect: it does not require
installation of the Taglib C++ library.
Installation is simple.
pip install eyeD3
Additionally, on Windows, you need this, too:
pip install python-magic-bin
For more details, visit the installation guide. Like my old
pytaglib post, I will keep this one short and only show code samples for
the most relevant tasks.
Before you do anything, import the eyed3 library, of course.
import eyed3
Load a file.
song = eyed3.load(file)
If the file does not yet have any tags, you must create them before
setting any metadata.
if not song.tag:
song.initTag()
Note that this erases existing tags, so only call that if you start
from scratch. Now you are ready to set the individual tags or read
them if you need them.
song.tag.artist = "Behemoth"
song.tag.album = "The Satanist"
song.tag.genre = "Black Metal"
song.tag.recording_date = 2014
song.tag.track_num = 4
song.tag.title = "Ora pro nobis Lucifer"
The important piece for me, write cover art.
with open(cover_art_filename, "rb") as cover_art:
song.tag.images.set(3, cover_art.read(), "image/jpeg")
The value 3 indicates that the front cover shall be set. See the
documentation for other values. If you are an iTunes user,
then select 0 for "Other". iTunes does not seem to like
"Front Cover" 🙄.
You may run into a warning message like the following if you use genre
names not defined in the ID3 specification.
eyed3.id3:WARNING: Non standard genre name
You do not need to worry about that. If it annoys you, add the
following line to your code. eyed3 still writes the tag without any
issue.
eyed3.log.setLevel("ERROR")