Getting Audio Information in Python

Want to grab some basic audio file information in your Python program?

Let’s use Mutagen to look at the type, length, and size of an audio element.

import mutagen

class Audio:

f = "some_audio.m4a"
element = mutagen.File(f)

if element:
    Audio.format_list = element._mimes
    Audio.size = element.size
    Audio.length = element.length
    Audio.error = "Invalid audio file."

Here, we could have imported the M4A handler, but using the File class we don’t have to make audio type assumptions a priori. If the audio format cannot be handled or is invalid, we will simply get a None Type return when sending our file to the File class.