如何使用 NodeJS 将音频流保存为 wav 文件

How to save an audio stream as a wav file using NodeJS

我的目标是录制来自 Icecast 服务器的无线电流。

我正在使用 icecast node module to fetch the radio stream and then writing the wave file by piping the stream through the wav 模块。

这是我的代码示例:

const icecast = require('icecast');
const url = 'http://87.118.104.139/radiogibsonaac';
var wav = require('wav');

let ice, fileWriter;

ice = icecast.get(url, res => {
    fileWriter = new wav.FileWriter(__dirname+'/recording.wav', {
        channels: 1,
        sampleRate: 16000,
        bitDepth: 128
    });
    res.pipe(fileWriter);
});


setTimeout(()=>{
    fileWriter.end();
    ice.end();
},5000);

流已按预期成功记录到我的磁盘,我可以在 VLC 中收听文件,但 wav 文件本身的格式似乎不正确。

当我尝试使用其他工具编辑文件时,每次都显示错误。 例如,我正在尝试更改 this site 上的音频速度,但它无法识别该文件。

此外,如果我尝试使用 Sox CLI 查看文件信息,它会显示:

sox FAIL formats: can't open input file `recording.wav': Sorry, don't understand .wav size

有人知道我在将 wav 文件写入磁盘的过程中是否遗漏了一个步骤吗?

根据数据流 URL,数据流似乎是 AAC 格式,而您正试图将该数据直接写入 WAV 文件,因此您最终得到的文件带有 WAV header 但 AAC 音频数据。

您需要将流以 AAC 格式写入磁盘,然后对文件进行转换,或者在将流写入磁盘之前转码 on-the-fly。