简体   繁体   English

在python(live)中将mp3转码为ogg的简单方法?

[英]Simple way to transcode mp3 to ogg in python (live)?

I'm searching for a library / module that can transcode an MP3 (other formats are a plus) to OGG, on the fly. 我正在寻找一个库/模块,它可以动态地将MP3(其他格式是加号)转码到OGG。

What I need this for: I'm writing a relatively small web app, for personal use, that will allow people to listen their music via a browser. 我需要的是:我正在编写一个相对较小的网络应用程序,供个人使用,这将允许人们通过浏览器收听他们的音乐。 For the listening part, I intend to use the new and mighty <audio> tag. 对于收听部分,我打算使用新的强大的<audio>标签。 However, few browsers support MP3 in there. 但是,很少有浏览器支持MP3。 Live transcoding seems like the best option because it doesn't waste disk space (like if I were to convert the entire music library) and I will not have performance issues since there will be at most 2-3 listeners at the same time. 实时转码似乎是最好的选择,因为它不会浪费磁盘空间(就像我要转换整个音乐库一样)而且我不会遇到性能问题,因为最多会有2-3个听众同时出现。

Basically, I need to feed it an MP3 (or whatever else) and then get a file-like object back that I can pass back to my framework ( flask , by the way) to feed to the client. 基本上,我需要提供一个MP3(或其他任何东西),然后得到一个类似文件的对象,我可以传回我的框架( flask ,顺便说一句),以提供给客户端。

Stuff I've looked at: 我看过的东西:

  • gstreamer -- seems overkill, although has good support for a lot of formats; gstreamer - 似乎有点矫枉过正,虽然对很多格式有很好的支持; documentation lacks horribly 文档缺乏可怕性
  • timeside -- looks nice and simple to use, but again it has a lot of stuff I don't need (graphing, analyzing, UI...) timeside - 看起来不错,使用简单,但它又有很多我不需要的东西(图形,分析,UI ...)
  • PyMedia -- last updated: 01 Feb 2006... PyMedia - 最后更新时间:2006年2月1日......

Suggestions? 建议?

You know, there's no shame in using subprocess to call external utilities. 您知道,使用subprocess调用外部实用程序并不可耻。 For example, you could construct pipes like: 例如,您可以构建管道,如:

#!/usr/bin/env python
import subprocess
frommp3 = subprocess.Popen(['mpg123', '-w', '-', '/tmp/test.mp3'], stdout=subprocess.PIPE)
toogg = subprocess.Popen(['oggenc', '-'], stdin=frommp3.stdout, stdout=subprocess.PIPE)
with open('/tmp/test.ogg', 'wb') as outfile:
    while True:
        data = toogg.stdout.read(1024 * 100)
        if not data:
            break
        outfile.write(data)

In fact, that's probably your best approach anyway. 事实上,无论如何,这可能是你最好的方法。 Consider that on a multi-CPU system, the MP3 decoder and OGG encoder will run in separate processes and will probably be scheduled on separate cores. 考虑到在多CPU系统上,MP3解码器和OGG编码器将在不同的进程中运行,并且可能会在不同的内核上进行调度。 If you tried to do the same with a single-threaded library, you could only transcode as fast as a single core could handle it. 如果您尝试对单线程库执行相同操作,则只能以单核处理它的速度进行转码。

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

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