Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python program to illustrate the
# extraction of FLAC audio metadata
# using the mutagen module
  
# Importing the FLAC method
# from the mutagen module
from mutagen.flac import FLAC
  
# Loading a flac file
audio = FLAC("GeeksForGeeks_Music.flac")
  
# Printing all the metadata
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




# Python program to illustrate
# adding tags to the FLAC metadata
# using mutagen module
  
# Importing the FLAC method from
# the mutagen module
from mutagen.flac import FLAC
  
# Loading a flac file
audio = FLAC("GeeksForGeeks_Music.flac")
  
# Adding tags to the metadata
audio["YEAR_OF_RELEASE"] = "2020"
audio["WEBSITE"] = "geeksforgeeks.org"
audio["GEEK_SCORE"] = "9"
  
# Modifying existing metadata tag
audio["ARTIST"] = "GeeksForGeeks Team"
  
# Printing the metadata
print(audio.pprint())
  
# Saving the changes
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
Previous
Next
Share your thoughts in the comments
Similar Reads