Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I capture an mp3 stream with python

What's the best way of capturing an mp3 stream coming off of http and saving it to disk with 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.

like image 917
runeh Avatar asked Oct 09 '08 14:10

runeh


1 Answers

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

target = open(target_path, "wb")
like image 92
Adam Rosenfield Avatar answered Sep 21 '22 02:09

Adam Rosenfield