#256: How to Work With FLAC Metadata
Last week we explored our options to work with MP3 metadata. However, if you want to put your music on an iPhone, you most likely go for FLAC – the Free Lossless Audio Codec. Luckily for us, Mutagen can work with FLAC as well and it is even simpler than MP3.
Update Mutagen
If you already installed Mutagen for the last post, you should be good to go. However, if it is a bit longer than you installed Mutagen or you are not sure, you should check if there is an update:
Read metadata from a FLAC file
As with MP3, FLAC offers us two types of metadata (technical and content). But for FLAC files we only need to use the FLAC() class to access both types of metadata:
If we run this code against the sample.flac file, we should get an output like this one:
python read_metadata_flac.py
file: sample.flac
--------------------------------------------------
length: 0.0
sample_rate: 44100
bitrate: 0
channels: 2
pprint: FLAC, 0.00 seconds, 44100 Hz (audio/flac)
**************************************************
Write metadata to a FLAC file
Unfortunately, there is no dictionary that we can use to check what the valid keys are to modify the metadata. Therefore, I suggest we let us inspire by the code of EasyID3 and try those keys first.
As with MP3, we can set the metadata for FLAC through a dictionary:
If we run this code, we should get an output like this one:
python write_metadata_flac.py
file: sample.flac
--------------------------------------------------
Before
FLAC, 0.00 seconds, 44100 Hz (audio/flac)
--------------------------------------------------
write metadata
--------------------------------------------------
After
FLAC, 0.00 seconds, 44100 Hz (audio/flac)
artist=Jon Doe
albumartist=Jon Doe II
album=Python rules
tracknumber=1
title=Writing Metadata
date=2024
genre=Education
We can see the metadata in the Windows Explorer as part of the file properties:

Next
As you can see in this post, working with metadata of a FLAC file is not much different than working with MP3 files. I hope this little excursion into the world of audio metadata was a helpful one.
Next week we try out a different approach to write less code when we define our classes in Python.