Python library for converting files to MP3 and setting their quality

我怕爱的太早我们不能终老 提交于 2019-11-28 21:22:24

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 )

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

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.

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!