如何使用python捕获mp3流

时间:2021-11-24 20:55:54

What's the best way of capturing an mp3 stream coming off of http and saving it to disk with python?

捕获来自http的mp3流并使用python将其保存到磁盘的最佳方法是什么?

Thus far I've tried

到目前为止,我已经尝试过

target = open(target_path, "w")
conn = urllib.urlopen(stream_url)
while True:
    target.write(conn.read(buf_size))

This gives me data but its garbled or wont play in mp3 players.

这给了我数据,但它的乱码或不会在MP3播放器中播放。

3 个解决方案

#1


15  

If you're on Windows, you might accidentally be doing CRLF conversions, corrupting the binary data. Try opening target in binary mode:

如果您使用的是Windows,则可能会意外地进行CRLF转换,从而破坏二进制数据。尝试以二进制模式打开目标:

target = open(target_path, "wb")

#2


4  

The best way for this is:

最好的方法是:

urllib.urlretrieve(stream_url, target_path);

#3


1  

Perhaps the syntax changed from the previous urllib answer (that got me to the correct answer btw), but this syntax works for python3:

也许语法改变了以前的urllib答案(这让我得到了正确的答案顺便说一句),但这种语法适用于python3:

import urllib.request

urllib.request.urlretrieve(stream_url, target_path)

#1


15  

If you're on Windows, you might accidentally be doing CRLF conversions, corrupting the binary data. Try opening target in binary mode:

如果您使用的是Windows,则可能会意外地进行CRLF转换,从而破坏二进制数据。尝试以二进制模式打开目标:

target = open(target_path, "wb")

#2


4  

The best way for this is:

最好的方法是:

urllib.urlretrieve(stream_url, target_path);

#3


1  

Perhaps the syntax changed from the previous urllib answer (that got me to the correct answer btw), but this syntax works for python3:

也许语法改变了以前的urllib答案(这让我得到了正确的答案顺便说一句),但这种语法适用于python3:

import urllib.request

urllib.request.urlretrieve(stream_url, target_path)