如何解决Lame MP3 Converter的Python等效项是什么?
| 我需要在服务器端将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音频工具,该工具能够在不同的音频格式之间进行转换。
我已经使用它将.wav文件转换为mp3,.flac和.m4a。
,如果要使用LAME编码MP3(而不是PyMedia),则始终可以使用ctypes包装la脚的编码器DLL(如果是Linux,则为.so)。您将使用的确切包装器代码将与LAME DLL版本绑定(不幸的是,其中有很多这样的代码),因此我无法给出任何示例,但是ctypes文档应该是关于包装DLL足够清楚。
,注意:这里是相对较新的程序员,我以前不需要转换音频文件。
但是,如果我正确理解了服务器端的含义,那么您可能正在寻找一种管理大量转换的好方法,而您对python解决方案的兴趣可能部分是为了能够更好地管理资源使用或集成进入您的加工链。我遇到了类似的问题/目标,并结合使用了Merlyn的推荐和Celery。我不使用django-celery,但是如果这是基于django的项目,那也可能会吸引您。您可以在此处找到有关芹菜的更多信息:
http://celeryproject.org/community.html
http://ask.github.com/celery/getting-started/introduction.html
根据您已经设置的内容,可能需要一些前期时间才能进行设置。要充分利用所有功能,您需要安装rabbitmq / erlang,但是如果您按照上述站点上的指南进行操作,现在它很快。
这是我如何将芹菜与子过程结合使用以解决类似问题的示例。与上面发布者的建议类似,我使用子过程调用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)
让我知道这对您是否有用。我在这里回答问题相对较新,不确定我的尝试是否有帮助。祝好运!
,好吧,Gstreamer具有\“ ugly plugin \” lamemp3enc,并且有针对Gstreamer的python绑定(gst-python 1.2,支持python 3.3)。我没有尝试过自己做这条路,所以我真的没有什么建议的地方……坦白说,对我来说,子流程解决方案似乎要简单得多,即使不是“更干净”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。