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

戏子无情 提交于 2019-12-03 07:44:16

问题


I'm searching for a library / module that can transcode an MP3 (other formats are a plus) to OGG, on the fly.

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. However, few browsers support MP3 in there. 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.

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.

Stuff I've looked at:

  • gstreamer -- seems overkill, although has good support for a lot of formats; 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...)
  • PyMedia -- last updated: 01 Feb 2006...

Suggestions?


回答1:


You know, there's no shame in using subprocess to call external utilities. 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. 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.



来源:https://stackoverflow.com/questions/5464912/simple-way-to-transcode-mp3-to-ogg-in-python-live

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