使用python流mp3播客数据

时间:2015-01-10 22:29:08

标签: python sockets icecast

我正在尝试编写自己的套接字流,我使用python的套接字连接到知道的mp3源,流式传输数据并尝试将其作为pcm数据传递到alsaaudio。

我知道必须获取icy-metaint,读取多个字节,获取元数据长度的第一个字节,然后继续读取metaint字节。

不幸的是,我已经没有知识了,下面的代码会导致播放白噪声。

任何帮助都会很棒。谢谢!

#!/usr/bin/env python
import socket
import sys
import alsaaudio
import time
import threading
import Queue

class Player(threading.Thread):

    def __init__(self, messageQueue, metaint):
        threading.Thread.__init__(self)

        self.metaint = metaint

        self.messageQueue = messageQueue
        self.device = alsaaudio.PCM()
        self.rate = 44100
        self.famesize = self.rate 
        self.device.setrate(self.rate)
        self.buffer = ""

    def sendPCM(self):
        print("Buffer length: " + str(len(self.buffer)))
        if len(self.buffer) > self.metaint + 255:
            pcmData = self.buffer[:self.metaint]
            self.device.write(pcmData)
            self.buffer = self.buffer[self.metaint:]
            print ("New buffer length 1: " + str(len(self.buffer)))
            metaDataLength = ord(self.buffer[:1]) * 16
            print ("Metadata length: " + str(metaDataLength))
            self.buffer = self.buffer[1:]
            print ("New buffer length 2: " + str(len(self.buffer)))
            metaData = self.buffer[:metaDataLength]
            print len(metaData)
            self.buffer = self.buffer[metaDataLength:]
            print ("New buffer length 3: " + str(len(self.buffer)))


    def run(self):
        self.sendPCM()
        while True:
            message = self.messageQueue.get()
            if message: self.buffer += message
            self.sendPCM()
            self.messageQueue.task_done()

def getResponseHeaders(socket):
    data = socket.recv(1024)

    while not "\r\n\r\n" in data:
        data = data + socket.recv(1024)

    return data

def getHeaders(response):
    headers = {}
    for line in response.splitlines():
        if line == '\r\n':
            break # end of headers
        if ':' in line:
            key, value = line.split(':', 1)
            headers[key] = value

    return headers


HOST = 'bbcmedia.ic.llnwd.net'
GET = '/stream/bbcmedia_lc1_radio1_p?s=1420917253&e=1420931653&h=1ff16ea945bd420669c48ae72d003c09'
PORT = 80

#create an INET, STREAMing socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))
client_socket.send("GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent:%s\r\nIcy-MetaData:%s\r\nRange:%s\r\n\r\n" % (GET, HOST,"VLC/2.0.5 LibVLC/2.0.5", "1", "bytes=0-"))

responseHeaders = getResponseHeaders(client_socket)
headers = getHeaders(responseHeaders)
metaint = int(headers['icy-metaint'])
br = int(headers['icy-br'])
print (metaint)



queue = Queue.Queue()

player = Player(queue, metaint)
player.daemon = True
player.start()

while 1:
    queue.put(client_socket.recv(4096))


client_socket.close()
sys.exit(0)

1 个答案:

答案 0 :(得分:1)

看起来你实际上并没有解码音频数据。

您正尝试从元数据中分离音频数据,但您还必须通过编解码器运行音频数据以获取PCM样本。 SHOUTcast / Icecast服务器不发送原始PCM。他们通常使用包裹在ADTS中的MP3或AAC。

我不是Python编码器,所以我不知道你能得到什么。一种简单的解码方法是使用FFmpeg。它支持STDIO,因此您可以轻松地将数据传输到它,并让它处理流并返回PCM样本。