Extract and Add FLAC Audio Metadata using the mutagen module in Python - GeeksforGeeks

01 Nov, 2020
Audio metadata is information embedded into audio files to identify and label audio files. Metadata includes information like artist, genre, album and track number. This information is very essential for making music players and other related applications. Streaming platforms also use metadata to categorize music according to various filters like artist, genre and album.
FLAC is a lossless audio format loved by audiophiles, which also contains embedded metadata. Using the mutagen module in Python you can access the metadata as well as add tags to the metadata of a FLAC audio file.
Installation: This module does not come built-in with Python. To install this module type the below pip command in the terminal.
pip install mutagen
If you’d like to follow along, you can use this Google Drive link to download the FLAC file used in this article.
To access the FLAC file metadata, we will use the FLAC() method of the mutagen module to read the FLAC file. Then we will use the pprint() method to fetch its metadata and print it in a human-readable manner.
Python3
# Python program to illustrate the# extraction of FLAC audio metadata# using the mutagen module  # Importing the FLAC method# from the mutagen modulefrom mutagen.flac import FLAC  # Loading a flac fileaudio = FLAC("GeeksForGeeks_Music.flac")  # Printing all the metadataprint(audio.pprint())
Output:
FLAC, 310.31 seconds, 44100 Hz (audio/flac)
GENRE=Geek Music
TRACKNUMBER=1/1
ALBUM=GeeksForGeeks Album
TITLE=GeeksForGeeks_Music
COMMENTS=Special soundtrack for all the GFG Fans.
ARTIST=Neeraj Ranametadata:
You can also add your own tags and values in the metadata using the same syntax you would use to add an item to a Python dictionary. Also, note that it is possible to edit the already existing tag values. Make sure to use the save function after making any changes to the tags.
Example:
Python3
# Python program to illustrate# adding tags to the FLAC metadata# using mutagen module  # Importing the FLAC method from# the mutagen modulefrom mutagen.flac import FLAC  # Loading a flac fileaudio = FLAC("GeeksForGeeks_Music.flac")  # Adding tags to the metadataaudio["YEAR_OF_RELEASE"] = "2020"audio["WEBSITE"] = "geeksforgeeks.org"audio["GEEK_SCORE"] = "9"  # Modifying existing metadata tagaudio["ARTIST"] = "GeeksForGeeks Team"  # Printing the metadataprint(audio.pprint())  # Saving the changesaudio.save()
Output:
FLAC, 310.31 seconds, 44100 Hz (audio/flac)
GENRE=Geek Music
TRACKNUMBER=1/1
ALBUM=Geeks
ForGeeks Album
TITLE=GeeksForGeeks_Music
COMMENTS=Special soundtrack for all the GFG Fans.
YEAR_OF_RELEASE=2020
WEBSITE=geeksforgeeks.org
GEEK_SCORE=9
ARTIST=GeeksForGeeks Team

python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Python | os.path.join() method
Selecting rows in pandas DataFrame based on conditions
Defaultdict in Python
Python | Get unique values from a list
Create a directory in Python
Python | Pandas dataframe.groupby()