views:

171

answers:

1

I am trying to make a proxy for internet-radio in mp3. It is working fine when accessing mp3-files, but not for mp3-streams.

I suppose I am missing some very basic difference but could not find a hint.

Best regards, wolf

My test code:

#!/usr/local/bin/python2.5
import urllib;  
import SocketServer, BaseHTTPServer  
import subprocess  

class Proxy:  
    def __init__(self, port=4500):  
        self.port = port  
        self.server = SocketServer.ThreadingTCPServer(('', self.port), self.Manager)  

    class Manager(BaseHTTPServer.BaseHTTPRequestHandler):  
        def do_GET(self):  
            self.send_response(200)  
            self.send_header("Content-type", "audio/mpeg");
            self.end_headers();

            process = subprocess.Popen("lame --mp3input -m m --abr 128 -b 64 - -", shell=True, bufsize=64,
            stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
            (streamin, streamout) = (process.stdin, process.stdout)
            # Does not work
            url = urllib.urlopen("http://stream.srg-ssr.ch:80%s" % "/drs3/mp3_128.m3u")
            # Does work
            #url = urllib.urlopen("http://www.openbsd.org:80%s" % "/songs/song46.mp3")
            buffer = url.read(4096)
            while len(buffer) > 0:
                streamin.streamout(buffer);
                while 1:
                    data = select.select([streamout.fileno()], [],[],.1);
                    if len(data[0]) == 0:
                        break
                    mp3 = streamout.read(4096)
                    self.wfile.write(mp3)
                buf = url.read(4096)
+1  A: 

The problem is that you are reading not mp3 stream but M3U playlist file. This file doesn't contain any mp3 data itself.

The content of your file is simple text:

http://zlz-stream10.streamserver.ch/1/drs3/mp3_128
http://glb-stream12.streamserver.ch/1/drs3/mp3_128
http://zlz-stream13.streamserver.ch/1/drs3/mp3_128
http://zlz-stream11.streamserver.ch/1/drs3/mp3_128
http://glb-stream10.streamserver.ch/1/drs3/mp3_128
http://zlz-stream12.streamserver.ch/1/drs3/mp3_128
http://glb-stream13.streamserver.ch/1/drs3/mp3_128
http://glb-stream11.streamserver.ch/1/drs3/mp3_128

Each line is an URL for the stream itself. Read the M3U file, parse it and download the stream data from these URLs.

Alexey Kalmykov
+1. M3U file is a playlist.
Amit