Python: Mutagen work example

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 🙂

  1. import mutagen #add library Mutagen
  2. import datetime #add library for time work
  3. def get_data(track): #Create a function that will receive data from the track
  4.     audiofile = mutagen.File(track) #create a variable that consists of a library request for a file
  5.     print('Продолжительность трека:', datetime.timedelta(seconds=audiofile.info.length)) #Output track duration
  6.     print('Битрейт:', audiofile.info.bitrate) #Output track bitrate
  7.     song_title = audiofile.tags.getall('TIT2') #Get the name of the track
  8.     singer_title = audiofile.tags.getall('TPE2') #We get the performer
  9.     print('Исполнитель:', singer_title[0]) #We display the performer
  10.     print('Название песни:', song_title[0], '\n') #Display the name of the track
  11.  
  12. track = str(input('Название песни: ')) #We ask the user to enter a file name
  13. 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 😉
Stay tuned!
As always – if you have questions, write to mail , or in Telegram.