Extract and Add FLAC Audio Metadata using the mutagen module in Python
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.
Accessing the FLAC metadata:
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
from mutagen.flac import FLAC
audio = FLAC( "GeeksForGeeks_Music.flac" )
print (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
from mutagen.flac import FLAC
audio = FLAC( "GeeksForGeeks_Music.flac" )
audio[ "YEAR_OF_RELEASE" ] = "2020"
audio[ "WEBSITE" ] = "geeksforgeeks.org"
audio[ "GEEK_SCORE" ] = "9"
audio[ "ARTIST" ] = "GeeksForGeeks Team"
print (audio.pprint())
audio.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
Last Updated :
01 Nov, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...