我试图从流中记录样本并将片段保存为.mp3文件。以下代码正确读取流并将数据保存到文件。该文件在媒体播放器中播放,但没有标题等,因此当我尝试使用.mp3文件执行其他操作(例如使用LAME将其转换为其他格式)时,它每次都会失败。有没有人有这种事情的经验?直接从流写入.mp3文件
下面是获取.mp3文件的工作示例。对不起,如果你得到一个商业;无线电吮吸。
import urllib2
from datetime import datetime
from datetime import timedelta
# Name of the file stream and how long to record a sample
URL = "http://4893.live.streamtheworld.com:80/KUFXFMAAC_SC"
RECORD_SECONDS = 30
# Open the file stream and write file
filename = 'python.mp3'
f=file(filename, 'wb')
url=urllib2.urlopen(URL)
# Basically a timer
t_start = datetime.now()
t_end = datetime.now()
t_end_old = t_end
# Record in chunks until
print "Recording..."
while t_end-t_start < timedelta(seconds=RECORD_SECONDS):
f.write(url.read(1024))
t_end = datetime.now()
# Or one big read?
#f.write(url.read(1024*517))
f.close()
这也许接近我想要做的:? encoding mp3 from a audio stream of PyTTS
现在,你所得到的只是一个原始流(它不会有任何头文件)。所以理论上,你可以命名文件'python.wav','python.wma',它可能会被正确读取。我不确定mp3是如何从字节码中编码的,但是您需要用mp3外壳“包装”字节码。 –
作为后续工作,请查看[PyMedia](http://pymedia.org/index.html)为您创建MP3。 –
我无法让pymedia为此应用程序工作。我试着将扩展名改为.wav,但我仍然遇到同样的问题。 –