简体   繁体   English

从 stream 写入 wav 文件没有音频数据

[英]Writing to wav file from a stream has no audio data

I'm working on creating a wav file from a stream on iheartradio.我正在从 iheartradio 上的 stream 创建一个 wav 文件。 I am able to get the stream url, but once I write the data to a wav file, I cannot play it on any media player I've tried.我能够获得 stream url,但是一旦我将数据写入 wav 文件,我就无法在我尝试过的任何媒体播放器上播放它。 I don't have experience with wav files too much so any help would be greatly appreciated.我没有太多使用 wav 文件的经验,因此将不胜感激。 This is what I've got so far from looking at similar posts:到目前为止,这是我从查看类似帖子中得到的:

import requests

stream_url = "http://stream.revma.ihrhls.com/zc2525"

r = requests.get(stream_url, stream=True)

with open('stream.wav', 'wb') as f:
    try:
        for block in r.iter_content(1024):
            f.write(block)
            print(block)
    except KeyboardInterrupt:
        f.close()

I've tested your code, and it seems to be working.我已经测试了你的代码,它似乎工作正常。 I have no issue at all.我一点问题都没有。 The issue must be your audio player.问题一定是您的音频播放器。 Have you tested it with a wav file you know is working?您是否使用已知有效的 wav 文件对其进行了测试?

You're making the assumption that the response contains a WAV header followed by PCM (Pulse Code Modulated) samples.您假设响应包含 WAV header 后跟 PCM(脉冲编码调制)样本。 That's not the case.事实并非如此。 If you print the response headers, you'll see that the response contains AAC (Advanced Audio Coding) audio data, which is a lossy compressed audio format - totally different from lossless WAV.如果您打印响应标头,您将看到响应包含 AAC(高级音频编码)音频数据,这是一种有损压缩音频格式 - 与无损 WAV 完全不同。 AAC is pretty standard for internet radio streams. AAC 是互联网广播流的标准。

That being said, your code works fine for me, and the generated ".wav" file does play in my Windows 10 Windows Media Player / VLC.话虽如此,您的代码对我来说很好,并且生成的“.wav”文件确实在我的 Windows 10 Windows 媒体播放器/VLC 中播放。 However, Just because you gave your file the ".wav" extension, doesn't mean it's actually a WAV file.但是,仅仅因为您为文件提供了“.wav”扩展名,并不意味着它实际上是一个 WAV 文件。 It could just be that your media players are more pedantic than mine, and refuse to play files where there's a discrepancy between the file's headers and the file's extension.可能只是您的媒体播放器比我的更迂腐,并且拒绝播放文件标题和文件扩展名之间存在差异的文件。 My media players seem to ignore the extension, and attempt to determine the format by inspecting the file headers - you could even rename it to stream.mp3 or stream.foobar , and it will play just the same.我的媒体播放器似乎忽略了扩展名,并尝试通过检查文件头来确定格式 - 您甚至可以将其重命名为stream.mp3stream.foobar ,它的播放方式相同。 Again, the file extensions don't really matter, what matters is the actual data (headers and samples) that you write to the file.同样,文件扩展名并不重要,重要的是您写入文件的实际数据(标题和样本)。 Don't assume that everything is a WAV file.不要假设一切都是 WAV 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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