mp3-stream的python-proxy问题

时间:2009-12-28 14:05:24

标签: python audio mp3 stream

我正在尝试用mp3代理互联网广播。它在访问mp3文件时工作正常,但不适用于mp3流。

我想我错过了一些非常基本的差异,但找不到提示。

祝你好运, 狼

我的测试代码:

#!/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 个答案:

答案 0 :(得分:0)

问题是你正在阅读不是mp3流而是M3U playlist file。该文件本身不包含任何mp3数据。

您的文件内容是简单文字:

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

每一行都是流本身的URL。阅读M3U文件,解析它并从这些URL下载流数据。