简体   繁体   English

什么是 Python 相当于 Lame MP3 转换器?

[英]What is the Python equivalent of Lame MP3 Converter?

I need to convert mp3 audio files to 64kbps on the server side.我需要在服务器端将 mp3 音频文件转换为 64kbps。

Right now, I am using subprocess to call lame , but I wonder if there are any good alternatives?现在,我正在使用subprocess来调用lame ,但我想知道是否有任何好的选择?

There seems to be a slightly old thread on that topic here: http://www.dreamincode.net/forums/topic/72083-lame-mp3-encoder-for-python/这里似乎有一个关于该主题的旧线程: http://www.dreamincode.net/forums/topic/72083-lame-mp3-encoder-for-python/

The final conclusion was to create a custom binding to lame_enc.dll via Python->C bindings.最后的结论是通过 Python->C 绑定创建到 lame_enc.dll 的自定义绑定。

The reason for that conclusion was that the existing binding libraries (pymedia/py-lame) have not been maintained.得出这个结论的原因是现有的绑定库(pymedia/py-lame)没有得到维护。

Unfortunately the guy didn't get it to work:)不幸的是,这家伙没有让它工作:)

Maybe you should continue to use subprocess .也许你应该继续使用subprocess You could take advantage of that choice, abstract your encoding at a slightly higher level, and reuse the code/strategy to optionally execute other command line encoding tools (such as ogg or shn tools).您可以利用该选择,在稍高的级别抽象您的编码,并重用代码/策略来选择性地执行其他命令行编码工具(例如 ogg 或 shn 工具)。

I've seen several audio ripping tools adopt that strategy.我已经看到几个音频翻录工具采用了这种策略。

I've been working with Python Audio Tools , which is capable of make conversions between different audio formats.我一直在使用Python Audio Tools ,它能够在不同的音频格式之间进行转换。

I've already used it to convert.wav files into mp3, .flac and.m4a.我已经用它把.wav 文件转换成mp3、.flac 和.m4a。

Well, Gstreamer has the "ugly plugin" lamemp3enc and there arepython bindings for Gstreamer (gst-python 1.2, supports python 3.3).好吧, Gstreamer有“丑陋的插件”lamemp3enc,并且有用于 Gstreamer 的python 绑定(gst-python 1.2,支持 python 3.3)。 I haven't tried going this route myself so I'm not really in a position to recommend anything... Frankly, a subprocess solution seems a lot simpler, if not "cleaner", to me.我自己没有尝试过这条路线,所以我并没有真正在 position 中推荐任何东西......坦率地说,对我来说,子流程解决方案似乎要简单得多,如果不是“更干净”的话。

If you want to use LAME to encode your MP3s (and not PyMedia), you can always use ctypes to wrap the lame encoder DLL (or.so if you are on Linux).如果你想使用 LAME 来编码你的 MP3(而不是 PyMedia),你总是可以使用ctypes来包装 lame 编码器 DLL (或者,如果你在 Linux 上)。 The exact wrapper code you'll use is going to be tied to the LAME DLL version (and there are many of these flying around, unfortunately), so I can't really give you any example, but the ctypes docs should be clear enough about wrapping DLLs.您将使用的确切包装器代码将与 LAME DLL 版本相关联(不幸的是,其中很多都在飞来飞去),所以我不能给您任何示例,但是 ctypes 文档应该足够清楚关于包装 DLL。

Caveat: relatively new programmer here and I haven't had a need to convert audio files before.警告:这里相对较新的程序员,我以前不需要转换音频文件。

However, if I understand what you mean by server-side, correctly, you might be looking for a good approach to manage mass conversions, and your interest in a python solution might be in part to be able to better manage the resource use or integrate into your processing chain.但是,如果我正确理解服务器端的含义,您可能正在寻找一种管理大规模转换的好方法,并且您对 python 解决方案的兴趣可能部分是为了能够更好地管理资源使用或集成进入您的加工链。 I had a similar problem/goal, which I resolved using a mix of Merlyn's recommendation and Celery .我有一个类似的问题/目标,我结合使用 Merlyn 的建议和Celery解决了这个问题。 I don't use django-celery, but if this is for a django-based project, that might appeal to you as well.我不使用 django-celery,但如果这是基于 django 的项目,那也可能会吸引你。 You can find out more about celery here:您可以在此处找到有关 celery 的更多信息:

Depending on what you have setup already, there may be a little upfront time needed to get setup.根据您已经设置的内容,可能需要一些前期时间来进行设置。 To take advantage of everything you'll need rabbitmq/erlang installed, but if you follow the guide on the sites above, it's pretty quick now.要充分利用您需要安装 rabbitmq/erlang 的所有功能,但如果您按照上面网站上的指南进行操作,现在已经非常快了。

Here's an example of how I use celery with subprocess to address a similar issue.这是我如何将 celery 与子进程一起使用来解决类似问题的示例。 Similar to the poster's suggestion above, I use subprocess to call ffmpeg, which is as good as it gets for video tools, and probably would actually be as good as it gets for audio tools too.与上面发帖人的建议类似,我使用子进程调用 ffmpeg,这对于视频工具来说和它一样好,实际上可能也和它对于音频工具一样好。 I'm including a bit more than necessary here to give you a feel for how you might configure your own a little.我在这里包含了一些不必要的内容,让您了解如何配置自己的一点。

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

Let me know if this was useful to you.让我知道这对您是否有用。 I'm relatively new to answering questions here and not sure if my attempts are helpful or not.我在这里回答问题相对较新,不确定我的尝试是否有帮助。 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM