Cryptography is complicated in more than just one way. Therefore, it is commonly recommended not to roll your own, but instead, employ tried and tested methods. Unless you are an experienced cryptographer, it is likely to overlook crucial things, for example, when to authenticate an encrypted message – before decrypting or after? This blog post is about JSON Web Tokens that are digitally signed with an RSA key. Instead of implementing the signing and verification code yourself, you should be using a dedicated server component to do the complex crypto for you, like Hashicorp Vault.
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.
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")
3 indicates that the front cover shall be set. See the
documentation for other values. If you are an iTunes user,
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.