Read MP3 in Python 3

Andreas Jansson picture Andreas Jansson · Feb 27, 2012 · Viewed 48.2k times · Source

What I want to do is simply

mp3 = read_mp3(mp3_filename)
audio_left = mp3.audio_channels[0]

where audio_left will contain raw PCM audio data.

I was looking at Play a Sound with Python, but most of the suggested modules are not ported to Python 3 yet. If possible I'd like to avoid having to install a fully-fledged game dev library.

I'm a complete Python beginner, so I'd like to start off using Python 3.

Answer

Wernight picture Wernight · Apr 3, 2014

To make it easier I'd convert with some tools mp3 to wav, either:

$ ffmpeg -i foo.mp3 -vn -acodec pcm_s16le -ac 1 -ar 44100 -f wav foo.wav
or
$ mpg123 -w foo.wav foo.mp3

Then read the WAV with one of the python WAV libraries. I'd recommend PySoundFile because it works with most generated WAV correctly and installed without issue (as opposed to scikits.audiolab).

Note: Even though scipy.io.wavfile.read() gave me a "WavFileWarning: Unfamiliar format bytes" warning, it also loaded the file properly.