views:

484

answers:

5

Hello,

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.

+1  A: 

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 )
Jon
PyMedia looks promising. Although it looks like they stop supporting it after Python 2.3
Michael J
+1  A: 

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
+1  A: 

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
+1  A: 

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
A: 

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