37

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.

Community
  • 1
  • 1
Andreas Jansson
  • 3,137
  • 2
  • 30
  • 40
  • Do you want left channel only for some particular reason, or do you just want the audio in general – Daenyth Feb 27 '12 at 01:34
  • @Daenyth, I just want a monophonic signal, so if I can have mp3.audio.toMono(), even better :) – Andreas Jansson Feb 27 '12 at 01:38
  • You might want to see here: http://stackoverflow.com/questions/8064784/mono-playback-of-mp3s-in-python-or-c – Daenyth Feb 27 '12 at 01:40
  • I deleted my answer: I misread `.mp3` as `.wav`. – yurisich Feb 27 '12 at 01:42
  • I know that GStreamer can play back mp3 and is widely used. A Python binding for GStreamer including Python 3 therefore has some chances. I found [gst-python](http://gstreamer.freedesktop.org/modules/gst-python.html). Sorry, I can not make a real answer out of it, because I lack the knowledge of this module, but this might be just a hint for someone else wanting to continue. – NoDataDumpNoContribution May 16 '14 at 17:15
  • 1
    Try - librosa, a popular package for music and audio analysis. Simple usage example is available in my answer below. – Nir Nov 16 '19 at 20:02

7 Answers7

18

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.

Wernight
  • 36,122
  • 25
  • 118
  • 131
17

You could use librosa:

import librosa
y, sr = librosa.load('your_file.mp3')

Further information: https://github.com/librosa/librosa

Nir
  • 1,618
  • 16
  • 24
  • 1
    Your command did not work for me using librosa 0.6.3. Had to do `y, sr = librosa.load('your_file.mp3', sr=44100)` – Axel Bregnsbo Jun 14 '20 at 19:44
  • 1
    From librosa docs: By default, librosa will resample the signal to 22050Hz. meaning `sr` parameter is optional. – Nir Jun 15 '20 at 07:34
  • Please note, that librosa does not support in-buffer reading for mp3 files according to bug in libsndfile mentioned in librosa's dependency SoundFile https://github.com/bastibe/SoundFile/issues/250#issuecomment-572440286 – discort Oct 16 '20 at 07:58
10

Can be done with pydub:

import array
from pydub import AudioSegment
from pydub.utils import get_array_type

sound = AudioSegment.from_file(file=path_to_file)
left = sound.split_to_mono()[0]

bit_depth = left.sample_width * 8
array_type = get_array_type(bit_depth)

numeric_array = array.array(array_type, left._data)
GuySoft
  • 1,723
  • 22
  • 30
7

I ended up using an mpg123 subprocess to convert the mp3 to wav, and then I use scipy.io.wavfile.read to read the wav file.

Andreas Jansson
  • 3,137
  • 2
  • 30
  • 40
7

I am considering using FFmpeg as a subprocess. There is a Python wrapper called pyffmpeg, but I had difficulty installing it on my system (OS X 10.7.3).

You may also want to look at the code here for calling FFmpeg as a subprocess from Python: https://github.com/albertz/learn-midi/blob/master/decode.py

composer314
  • 338
  • 2
  • 8
1

I believe that the best to do this and that is also compatible with Python 3.x is:

https://pypi.python.org/pypi/mplayer.py/

I highly recommend that you look at Darwin M. Bautista's git or google code:

And here is an example from (as provided by him):

from mplayer import Player, CmdPrefix

# Set default prefix for all Player instances
Player.cmd_prefix = CmdPrefix.PAUSING_KEEP

# Since autospawn is True by default, no need to call player.spawn() manually
player = Player()

# Play a file
player.loadfile('mp3_filename')

# Pause playback
player.pause()

# Get title from metadata
metadata = player.metadata or {}
print metadata.get('Title', '')

# Print the filename
print player.filename

# Seek +5 seconds
player.time_pos += 5

# Set to fullscreen
player.fullscreen = True

# Terminate MPlayer
player.quit()

List of supported audio codecs:

  • MPEG layer 1, 2, and 3 (MP3) audio
  • AC3/A52, E-AC3, DTS (Dolby Digital) audio (software or SP/DIF)
  • AAC (MPEG-4 audio)
  • WMA (DivX Audio) v1, v2
  • WMA 9 (WMAv3), Voxware audio, ACELP.net etc (using x86 DLLs)
  • RealAudio: COOK, SIPRO, ATRAC3 (using Real libraries)
  • RealAudio: DNET and older codecs
  • QuickTime: Qclp, Q-Design QDMC/QDM2, MACE 3/6 (using QT libraries), ALAC
  • Ogg Vorbis audio
  • VIVO audio (g723, Vivo Siren) (using x86 DLL)
  • alaw/ulaw, (ms)gsm, pcm, *adpcm and other simple old audio formats
Mansueli
  • 6,223
  • 8
  • 33
  • 57
  • Can you give a code example that addresses the problem in the question? That is, code that extracts PCM data from an input file ready for further analysis? – detly May 17 '14 at 01:46
  • Actually, this looks like it doesn't satisfy the "no subprocess invocation" requirement. It just wraps an MPlayer process, which it looks for as `mplayer`, and I suspect that will be quite fragile on Windows. – detly May 17 '14 at 01:55
0

Code for playing songs in Python

pip3 install python-vlc

Download the python VLC package

Code - import vlc

#plays the song for 10 seconds

import vlc
import time

song = 'zik.mp3'

playSong = vlc.MediaPlayer(song)
playSong.play()
time.sleep(10)
playSong.stop()

or

pip3 install playsound

import playsound

song = 'path_to_the_.mp3_file'

playsound.playsound(song)

That's it!

Garbez François
  • 327
  • 3
  • 13
Sahil Ohe
  • 1
  • 1