Skip to content

#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:

pip install -U mutagen

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:

import mutagen
from mutagen.flac import FLAC

file = "sample.flac"
print(f"file: \t\t {file}")
print("-" * 50)

audio = FLAC(file)
print(f"length: \t {audio.info.length}")
print(f"sample_rate: \t {audio.info.sample_rate}")
print(f"bitrate: \t {audio.info.bitrate}")
print(f"channels: \t {audio.info.channels}")
print(f"pprint: \t {audio.pprint()}")
print("*" * 50)

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:

import mutagen
from mutagen.flac import FLAC

file = "sample.flac"
print(f"file: \t\t {file}")
print("-" * 50)

audio = FLAC(file)
print("Before")
print(audio.pprint())

print("-" * 50)
print("write metadata")

audio["artist"] = "Jon Doe"
audio["albumartist"] = "Jon Doe II"
audio["album"] = "Python rules"
audio["tracknumber"] = "1"
audio["title"] = "Writing Metadata"
audio["date"] = "2024"
audio["genre"] = "Education"
audio.save()

print("-" * 50)
print("After")
print(audio.pprint())

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:

The sections Media and Description contain our metadata

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.