使用FFMPEG实时流媒体到Web音频API

Nay*_*yan 5 ffmpeg node.js html5-audio web-audio-api

我正在尝试使用node.js + ffmpeg将音频流传输到仅使用Web Audio API的LAN中连接的浏览器。

不使用元素,因为它添加了自己的8到10秒的缓冲区,并且我想获得最大的高延迟(大约1到2秒)。

音频可以成功播放,但是音频断断续续且嘈杂。

这是我的node.js(服务器端)文件:

var ws = require('websocket.io'), 
server = ws.listen(3000);
var child_process = require("child_process");
var i = 0;
server.on('connection', function (socket) 
{

console.log('New client connected');

var ffmpeg = child_process.spawn("ffmpeg",[
    "-re","-i",
    "A.mp3","-f",
    "f32le",
    "pipe:1"                     // Output to STDOUT
    ]);

 ffmpeg.stdout.on('data', function(data)
 {
    var buff = new Buffer(data);
    socket.send(buff.toString('base64'));
 });
});
Run Code Online (Sandbox Code Playgroud)

这是我的HTML:

var audioBuffer = null;
var context = null;
window.addEventListener('load', init, false);
function init() {
    try {
        context = new webkitAudioContext();
    } catch(e) {
        alert('Web Audio API is not supported in this browser');
    }
}

var ws = new WebSocket("ws://localhost:3000/");

ws.onmessage = function(message)
{
    var d1 = base64DecToArr(message.data).buffer;
    var d2 = new DataView(d1);

    var data = new Float32Array(d2.byteLength / Float32Array.BYTES_PER_ELEMENT);
    for (var jj = 0; jj < data.length; ++jj)
    {
        data[jj] = d2.getFloat32(jj * Float32Array.BYTES_PER_ELEMENT, true);
    }

    var audioBuffer = context.createBuffer(2, data.length, 44100);
    audioBuffer.getChannelData(0).set(data);

    var source = context.createBufferSource(); // creates a sound source
    source.buffer = audioBuffer;
    source.connect(context.destination); // connect the source to the context's destination (the speakers)
    source.start(0);
};
Run Code Online (Sandbox Code Playgroud)

谁能告诉我哪里出了问题?

此致Nayan

Nay*_*yan 5

我开始工作了!!

我所要做的就是调整频道的数量。

我已经将 FFMPEG 设置为输出单声道音频,它的效果非常好。这是我的新 FFMOEG 命令:

var ffmpeg = child_process.spawn("ffmpeg",[
    "-re","-i",
    "A.mp3",
    "-ac","1","-f",
    "f32le",
    "pipe:1"                     // Output to STDOUT
    ]);
Run Code Online (Sandbox Code Playgroud)