Analyse frequency of mp3 files with python

12,523

Solution 1

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)

Solution 2

If you're trying to create a spectrogram, numpy has a module called fftpack (I think - FFT is short for Fast Fourier Transform) that can convert a function of amplitude over time to a function of amplitude over frequency. If you create a program that iterates this repeatedly over very short time intervals, you could throw all this into an array and analyze it from there.

If you're just trying to get the sampling rate, you need to find the header, which usually starts with "fff" or "ffe" (it isn't always necessarily at the beginning.) The sampling rate information is in the sixth byte of the header, it's all explained here: http://www.multiweb.cz/twoinches/mp3inside.htm

Share:
12,523
DrNightmare
Author by

DrNightmare

Programmer by day, programmer by night #SOreadytohelp

Updated on July 26, 2022

Comments

  • DrNightmare
    DrNightmare almost 2 years

    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.