distorteddisco
distorteddisco

Reputation: 249

Streaming audio using Python (without GStreamer)

I'm working on a project that involves streaming .OGG (or .mp3) files from my webserver. I'd prefer not to have to download the whole file and then play it, is there a way to do that in pure Python (no GStreamer - hoping to make it truly cross platform)? Is there a way to use urllib to download the file chunks at a time and load that into, say, PyGame to do the actual audio playing?

Thanks!

Upvotes: 4

Views: 1123

Answers (1)

izidor
izidor

Reputation: 4144

I suppose your server supports Range requests. You ask the server by header Range with start byte and 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 a file object and always download just a needed chunk of file from server. Almost every library accepts a file object as input.

It will be probably slow because of a network latency. You would need to download bigger chunks of the file, preload the file in a separate thread, etc. In other words, you would need to implement the streaming client logic yourself.

Upvotes: 1

Related Questions