Analyse frequency of mp3 files with python

DrNightmare picture DrNightmare · Apr 10, 2014 · Viewed 11.7k times · Source

I am trying to write a Python script to read an MP3 file and perform some analysis on the frequencies in it. In particular, I want a spectrogram (frequency vs time) as output.

However, when I read the file using open() and piped the contents to a file, I got something like this:

3763 1e65 0311 1814 b094 d3e3 25b3 641b
15a1 f146 62d6 ade6 7708 c5ec 1a0d 7395
201c 46e6 65a9 5276 688a 47eb 80e8 617e
4d66 2d82 2677 f74e e664 6220 69fa 1b46

On further research, I figured that these were somehow related to the MP3 headers and data discussed in this wiki: http://en.wikipedia.org/wiki/MP3#File_structure

How can I use this information to extract frequency data of the file?

PS: I specifically want to analyse MP3 files, NOT WAV files. A workaround would be to convert the MP3 to WAV format and then work on that, as there is a Python module to handle WAV files. But is there a solution to this problem without this conversion?

Thanks in advance.

Answer

ebarr picture ebarr · Apr 10, 2014

If you went with .wav files, there is a python standard library that can handle them (https://docs.python.org/2/library/wave.html). I have played with this in the past and found it quite easy to use.

For mp3 the mutagen package is an option https://github.com/quodlibet/mutagen

Also, this SO question my help: Importing sound files into Python as NumPy arrays (alternatives to audiolab)