如何使用 python 捕获 mp3 流 [英] How do I capture an mp3 stream with python

查看:32
本文介绍了如何使用 python 捕获 mp3 流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

捕获来自 http 的 mp3 流并使用 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))

这给了我数据,但它在 mp3 播放器中出现乱码或无法播放.

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

推荐答案

如果您使用的是 Windows,您可能会不小心进行 CRLF 转换,从而损坏二进制数据.尝试以二进制模式打开 target:

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

这篇关于如何使用 python 捕获 mp3 流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆