An interesting library called Mutagen came across me the other day. It serves to process metadata in media files, which means today’s topic will sound like: Mutagen – an example of work.
The Mutagen library is not only good for the large number of supported formats – it is important that the library is working (unlike a dozen others, on Google and Wiki-Python mentioned, but now abandoned and deprecated). According to the developers, the library supports ASF, FLAC, MP4, Monkey’s Audio, MP3, Musepack, Ogg Opus, Ogg FLAC, Ogg Speex, Ogg Theora, Ogg Vorbis, True Audio, WavPack, OptimFROG, and AIFF, as well as receive data about Bitrate the track and its duration. Very good documentation available on the developer’s site, I I’ll share an example of a code that allows you to grab data on the duration of a track, its bitrate, artist, and song title – such a short cheat sheet for working with Mutagen 🙂
import mutagen #add library Mutagen
import datetime #add library for time work
def get_data(track): #Create a function that will receive data from the track
audiofile = mutagen.File(track) #create a variable that consists of a library request for a file
print('Продолжительность трека:', datetime.timedelta(seconds=audiofile.info.length)) #Output track duration
print('Битрейт:', audiofile.info.bitrate) #Output track bitrate
song_title = audiofile.tags.getall('TIT2') #Get the name of the track
singer_title = audiofile.tags.getall('TPE2') #We get the performer
print('Исполнитель:', singer_title) #We display the performer
print('Название песни:', song_title, '\n') #Display the name of the track
track = str(input('Название песни: ')) #We ask the user to enter a file name
get_data(track) #Connect the function
The logical action would be the desire to clean up the music library on the computer. To do this, we will create a program that will receive data about the song artist, its name, and – send this data with a piece of the track for verification. If the sent data is correct – go to the next track, if the sent data is wrong – we request the correct data, overwrite it in the tags of the track and go to the next track. But that’s another story 😉
As always – if you have questions, write to mail , or in Telegram.