23

I'm trying to find a Python library that would take an audio file (e.g. .ogg, .wav) and convert it into mp3 for playback on a webpage.

Also, any thoughts on setting its quality for playback would be great.

Thank you.

Abid A
  • 7,588
  • 4
  • 32
  • 32

6 Answers6

72

I wrote a library designed to do that =D

from pydub import AudioSegment
AudioSegment.from_file("/input/file").export("/output/file", format="mp3")

Easy!

to specify a bitrate, just use the bitrate kwarg…

from pydub import AudioSegment
sound = AudioSegment.from_file("/input/file")
sound.export("/output/file", format="mp3", bitrate="128k")
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
  • That looks nice! Can you specify the quality of the MP3? I would love to use this in a project, but not being able to set the quality would be a show-stopper for me. – Nick Feb 14 '13 at 23:43
  • is there a way to set up a bitrate to output file? – SaulTigh Feb 26 '13 at 15:08
  • 1
    if this helps.. please ensure you install ffmpeg from http://www.ffmpeg.org/download.html first and have in your executable path. pydub requires it. – ihightower Feb 14 '17 at 10:53
  • 1
    @ihightower. what do you mean when you say executable path? the python root path? can you please elaborate? I have download `ffmpeg` but not sure where to extract it. I'm used to just running a `exe` file. Doesn't look like the package has one. – jason Apr 24 '17 at 22:16
  • 1
    @jason what i mean is.. add ffmpeg.exe in your system path (e.g. this shows how to add a path to windows http://stackoverflow.com/questions/23400030/windows-7-add-path)... i usually copy and standalone required tools in a folder e.g. (C:\Program Files\MyTools) and i add this folder to the PATH.. in this folder i keep (ffmpeg.exe, chromdriver.exe, anyuseful.exe etc.) that is required by python or other programs). once i added... just calling ffmpeg.exe in any program will just work. – ihightower Apr 29 '17 at 08:33
  • Excellent job, sir – Yablargo May 05 '17 at 18:58
  • Can we convert 3gp to mp3? – Ilyas karim Sep 25 '17 at 14:02
  • @Ilyaskarim I'm not familiar with 3gp files but it will depend on whether ffmpeg can convert that format to mp3 – Jiaaro Sep 27 '17 at 18:20
  • Ok, Does it convert mp4 to mp3? – Ilyas karim Sep 28 '17 at 06:05
  • @Ilyaskarim ffmpeg can do that yes – Jiaaro Sep 28 '17 at 15:37
  • That's earned a star on GitHub! – George Ogden Jun 12 '21 at 13:20
  • Great Library @Jiaaro! I just have a quick question though, for the “sound.export”; does it have a completion handler once the conversion is completed? – newProgramm3r Oct 20 '22 at 10:45
  • @newProgramm3r it's synchronous, when that line of code completes, the file is converted – Jiaaro Oct 21 '22 at 19:02
4

I use the Python bindings for gstreamer. It's a bit hard to get started but once you get going nearly anything's possible.

From the command line (from gstreamer's documentation):

gst-launch -v filesrc location=music.wav ! decodebin ! audioconvert ! audioresample ! lame bitrate=192 ! id3v2mux ! filesink location=music.mp3

The input filesrc location=... could be anything gstreamer can play, not just .wav. You could add something called a caps filter to resample to a specific rate before you encode.

In your Python program you would use gst.parse_launch(...), get the filesrc and filesink elements, and call setters to change the input and output filenames.

joeforker
  • 40,459
  • 37
  • 151
  • 246
3

Looks like PyMedia does this:

http://pymedia.org/

and some more info here on converting to various formats, whilst setting the bitrate:

http://pymedia.org/tut/recode_audio.html

e.g.

params= {
'id': acodec.getCodecId('mp3'),
'bitrate': r.bitrate,
'sample_rate': r.sample_rate,
'ext': 'mp3',
'channels': r.channels }
enc= acodec.Encoder( params )
Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
3

Also, the Python Audio Tools should be able to do the job with less need for other libraries, which might be easier if you're doing this on a shared web hosting account. (But admittedly I haven't tried it, so I can't confirm how usable it is.)

ewall
  • 27,179
  • 15
  • 70
  • 84
2

You may use ctypes module to call functions directly from dynamic libraries. It doesn't require you to install external Python libs and it has better performance than command line tools, but it's usually harder to implement (plus of course you need to provide external library).

Tupteq
  • 2,986
  • 1
  • 21
  • 30
1

Another option to avoid installing Python modules for this simple task would be to just exec "lame" or other command line encoder from the Python script (with the popen module.)

juanjux
  • 621
  • 6
  • 15