【问题标题】:Live streaming using FFMPEG to web audio api使用 FFMPEG 到网络音频 api 的实时流式传输
【发布时间】:2014-02-07 21:16:16
【问题描述】:

我正在尝试使用 node.js + ffmpeg 将音频流式传输到仅使用网络音频 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'));
 });
});

这是我的 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);
};

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

问候, 纳扬

【问题讨论】:

标签: node.js ffmpeg html5-audio web-audio-api


【解决方案1】:

您正在获取大量数据,从中创建单独的节点,并根据网络时间启动它们。为了使音频听起来正确,缓冲区的播放必须不间断,并且采样准确的时间。你需要从根本上改变你的方法。

我这样做的方法是创建一个 ScriptProcessorNode 来管理它自己的 PCM 样本缓冲区。在处理过程中,它将样本读入输出缓冲区。这保证了音频的流畅播放。

【讨论】:

  • 我试过了,但没有运气,你能指出具体的例子或写一段代码吗?
  • 这正是它应该做的。愿意用一些代码来演示吗?
【解决方案2】:

我搞定了!!

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

我已将 FFMPEG 设置为输出单声道音频,它的作用就像一个魅力。这是我的新 FFMOEG 命令:

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

【讨论】:

    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 2012-04-03
    • 2010-10-23
    • 1970-01-01
    相关资源
    最近更新 更多