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

I am looking for a library / module that can transcode MP3s (other formats are plus) into OGG, on the fly.

Why do I need this: I am writing a relatively small web application for personal use that will allow people to listen to music through a browser. For the listening part, I intend to use the new and powerful <audio> . However, several browsers support MP3. Direct transcoding seems to be the best option, because it does not waste disk space (for example, if I converted the entire music library), and I will not have performance problems, since at the same time there will be no more than 2-3 listeners.

Basically, I need to feed it MP3 (or something else), and then return the file-like object back so that I can go back to my structure ( flask , by the way) to pass to the client.

The material I was looking at:

  • gstreamer - seems redundant, although it has good support for a large number of formats; the documentation is not enough
  • timeside - looks beautiful and easy to use, but again it has a lot of things that I don’t need (graphics, analysis, UI ...)
  • PyMedia - last update: Feb 01 2006 ...

Suggestions?

+8
python transcoding audio
source share
1 answer

You know, there is no shame when using subprocess to call external utilities. For example, you can create channels such as:

 #!/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, this is probably your best approach. Note that in a multiprocessor system, the MP3 decoder and OGG encoder will work in separate processes and are likely to be scheduled on separate cores. If you tried to do the same with a single-threaded library, you could only re-encode as fast as only one core could process it.

+7
source share

All Articles