首页 > 解决方案 > 如何将缓冲区数组转换为音频文件

问题描述

chunks是缓冲区数组,我将它们标记为:Buffer.concat()但是当我将它们转换为 lame 的 wav 时,我得到了 wav 文件,但其中没有任何内容

const Lame = require("node-lame").Lame;
let chunks = [];

const audioStream = receiver.createOpusStream(user);

audioStream.on('data', chunk => {
    console.log(`Received ${chunk.length} bytes of data.`);
    chunks.push(chunk);
});

.......

var buf = Buffer.concat(chunks);
console.log(buf.length);

const decoder = new Lame({
    "output": "./demo.wav"
}).setBuffer(buf);

decoder.decode()
    .then(() => {
        console.log('done');
    })
    .catch((error) => {
        console.log(error);
    });

标签: javascriptnode.js

解决方案


我不熟悉 npm 包。

尝试这个。

const Lame = require("node-lame").Lame;
let chunks = [];


const audioStream = receiver.createOpusStream(user);

// assumption that your buffer is correct
audioStream.on('data', chunk => {
    console.log(`Received ${chunk.length} bytes of data.`);
    chunks.push(chunk);
});

const encoder = new Lame({
    "output": "./demo.wav",
    "bitrate": 192 // what ever bitrate
}).setBuffer(buf);

encoder.encode()
    .then(() => {
        // Encoding finished
        console.log('done');
    })
    .catch((error) => {
        // Something went wrong
        console.log(error);
    });

推荐阅读