Python library for converting files to MP3 and set

2019-01-23 14:58发布

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.

6条回答
够拽才男人
2楼-- · 2019-01-23 15:12

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 )
查看更多
Animai°情兽
3楼-- · 2019-01-23 15:13

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).

查看更多
三岁会撩人
4楼-- · 2019-01-23 15:15

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.)

查看更多
beautiful°
5楼-- · 2019-01-23 15:19

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.

查看更多
贼婆χ
6楼-- · 2019-01-23 15:26

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.)

查看更多
干净又极端
7楼-- · 2019-01-23 15:31

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")
查看更多
登录 后发表回答