简体   繁体   中英

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.

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")

最好的方法是:

urllib.urlretrieve(stream_url, target_path);

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

import urllib.request

urllib.request.urlretrieve(stream_url, target_path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM