Gary Sieling

Processing MP3s in python

There are a series of Python libraries for handling mp3s12. I tried setting up a few of these – many seem to be out of date, unmaintained, or nearly impossible to set up due to Python version requirements that conflict with other libraries you may wish to use (most likely numpy, matplotlib, and the like)

After going through a few of these, I found that ffmpeg is a well-supported choice for converting sound files, and works on about every OS. MP3s are effectively a compressed sound file, so the best way to work with them seems to be to “uncompress” them to WAV files. Conveniently, these closely resembly the internals of an array of measurements, so they are actually easy to work on. It is worth noting that effectively every Python library that claims to be able to operate on MP3s is actually a C library with Python bindings, so Python is hardly adding anything useful to the mix.

However, it is quite convenient to call ffmpeg as a script directly from Python:

import scipy.io.wavfile as wavfile
import os.path
from subprocess import call

if (not os.path.isfile(fileWav)):
  wavConvertCommand = \
    ["d:\\Software\\ffmpeg-20160619-5f5a97d-win32-static\\bin\\ffmpeg.exe", 
    "-i", fileMp3, "-acodec", "pcm_u8", "-ar", "22050", fileWav]
  call(wavConvertCommand)

To actually work on these, you should install Anaconda3 so you get all the right analysis libraries. Then, to do something like compute the signal to noise ratio in the file, you can do this:

import scipy.io.wavfile as wavfile
import scipy.stats as stats

data = wavfile.read(fileWav)[1]
stats.signaltonoise(data)
  1. https://pypi.python.org/pypi/scikits.audiolab/ []
  2. https://pypi.python.org/pypi/pysndfile []
  3. https://www.continuum.io/downloads []
Exit mobile version