![](/img/trans.png)
[英]Write a simple python script to convert all .wav files in a specific folder to .mp3 using lame
[英]What is the Python equivalent of Lame MP3 Converter?
我需要在服務器端將 mp3 音頻文件轉換為 64kbps。
現在,我正在使用subprocess
來調用lame
,但我想知道是否有任何好的選擇?
這里似乎有一個關於該主題的舊線程: http://www.dreamincode.net/forums/topic/72083-lame-mp3-encoder-for-python/
最后的結論是通過 Python->C 綁定創建到 lame_enc.dll 的自定義綁定。
得出這個結論的原因是現有的綁定庫(pymedia/py-lame)沒有得到維護。
不幸的是,這家伙沒有讓它工作:)
也許你應該繼續使用subprocess
。 您可以利用該選擇,在稍高的級別抽象您的編碼,並重用代碼/策略來選擇性地執行其他命令行編碼工具(例如 ogg 或 shn 工具)。
我已經看到幾個音頻翻錄工具采用了這種策略。
我一直在使用Python Audio Tools ,它能夠在不同的音頻格式之間進行轉換。
我已經用它把.wav 文件轉換成mp3、.flac 和.m4a。
如果你想使用 LAME 來編碼你的 MP3(而不是 PyMedia),你總是可以使用ctypes來包裝 lame 編碼器 DLL (或者,如果你在 Linux 上)。 您將使用的確切包裝器代碼將與 LAME DLL 版本相關聯(不幸的是,其中很多都在飛來飛去),所以我不能給您任何示例,但是 ctypes 文檔應該足夠清楚關於包裝 DLL。
警告:這里相對較新的程序員,我以前不需要轉換音頻文件。
但是,如果我正確理解服務器端的含義,您可能正在尋找一種管理大規模轉換的好方法,並且您對 python 解決方案的興趣可能部分是為了能夠更好地管理資源使用或集成進入您的加工鏈。 我有一個類似的問題/目標,我結合使用 Merlyn 的建議和Celery解決了這個問題。 我不使用 django-celery,但如果這是基於 django 的項目,那也可能會吸引你。 您可以在此處找到有關 celery 的更多信息:
根據您已經設置的內容,可能需要一些前期時間來進行設置。 要充分利用您需要安裝 rabbitmq/erlang 的所有功能,但如果您按照上面網站上的指南進行操作,現在已經非常快了。
這是我如何將 celery 與子進程一起使用來解決類似問題的示例。 與上面發帖人的建議類似,我使用子進程調用 ffmpeg,這對於視頻工具來說和它一樣好,實際上可能也和它對於音頻工具一樣好。 我在這里包含了一些不必要的內容,讓您了解如何配置自己的一點。
#example of configuring an option, here I'm selecting how much I want to adjust bitrate
#based on my input's format
def generate_command_line_method(self):
if self.bitrate:
compression_dict = {'.mp4':1.5, '.rm':1.5, '.avi': 1.2,
'.mkv': 1.2, '.mpg': 1, '.mpeg':1}
if self.ext.lower() in compression_dict.keys():
compression_factor = compression_dict[self.ext.lower()]
#Making a list to send to the command line through subprocess
ffscript = ['ffmpeg',
'-i', self.fullpath,
'-b', str(self.bitrate * compression_factor),
'-qscale', '3', #quality factor, based on trial and error
'-g', '90', #iframe roughly per 3 seconds
'-intra',
outpath
]
return ffscript
#The celery side of things, I'd have a celeryconfig.py file in the
#same directory as the script that points to the following function, so my task
#queue would know the specifics of the function I'll call through it. You can
#see example configs on the sites above, but it's basically just going to be
#a tuple that says, here are the modules I want you to look in, celery, e.g.
#CELERY_MODULES = ("exciting_asynchronous_module.py",). This file then contains,
from celery.decorators import task
from mymodule import myobject
from subprocess import Popen
@task(time_limit=600) #say, for example, 10 mins
def run_ffscript(ffscript):
some_result = Popen(ffscript).wait()
#Note: we'll wait because we don't want to compound
#the asynchronous aspect (we don't want celery to launch the subprocess and think
#it has finished.
#Then I start up celery/rabbitmq, and got into my interactive shell (ipython shown):
#I'll have some generator feeding these ffscripts command lines, then process them
#with something like:
In[1]: for generated_ffscript in generator:
run_ffscript.delay(generated_ffscript)
讓我知道這對您是否有用。 我在這里回答問題相對較新,不確定我的嘗試是否有幫助。 祝你好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.