Streaming audio using Python (without GStreamer)

I am working on a project that includes .OGG (or .mp3) stream files from my web server. I would prefer not to download the whole file and then play it back, is there a way to do this in pure Python (without GStreamer - hoping to make it a truly cross platform)? Is there a way to use urllib to load fragments of files at a time and load it into, say, PyGame to play the actual sound?

Thanks!

+4
source share
1 answer

Suppose your server supports range requests . You request the server by the Range header with the start byte and the end byte of the range you want:

import urllib2 req = urllib2.Request(url) req.headers['Range'] = 'bytes=%s-%s' % (startByte, endByte) f = urllib2.urlopen(req) f.read() 

You can implement the file object and always download only the necessary fragment of the file from the server. Almost every library accepts an input file as an object.

It will probably be slow due to network latency. You will need to upload large fragments of the file, preload the file into a separate stream, etc. In other words, you will need to implement the logic of the streaming client yourself.

+1
source

Source: https://habr.com/ru/post/1394415/


All Articles