【问题标题】:I have an audio data stream from ffmpeg, how can I play it in a browser?我有来自 ffmpeg 的音频数据流,如何在浏览器中播放?
【发布时间】:2013-11-21 05:09:38
【问题描述】:

我已经能够成功地将实时音频从我的麦克风传输到我的节点服务器。我现在想将其流式传输到所有连接的客户端。我一直在尝试使用 Web 套接字。

我正在使用此命令流式传输音频

ffmpeg -f alsa -i hw:0 -acodec mp2 -f mp3 -r 30 http://localhost:8086

Node 获取缓冲区数组,然后我使用 'ws' 包将其写入所有连接的客户端

// HTTP Server to accept incomming MP3 Stream (audio)
var audioServer = require('http').createServer( function(request, response) {

       audioSocket.broadcast(data, {binary:true});

}).listen(8086);

var audioSocket = new (require('ws').Server)({port: 8088});
audioSocket.broadcast = function(data, opts) {

    for( var i in this.clients ) {
        this.clients[i].send(data);
    }
};

知道如何在浏览器上播放这些数据吗?我尝试关注此topic,但 decodeAudioData() 方法失败。

我的客户端代码

node={};
var audio = new WebSocket('ws://localhost:8088/');
audio.binaryType = "arraybuffer";
var context = new webkitAudioContext();

audio.onmessage = function(data){
    node.buf=data.data;
    node.sync=0;
    node.retry=0;
    decode(node);
}

function syncStream(node){ // should be done by api itself. and hopefully will.
    var buf8 = new Uint8Array(node.buf); 
    buf8.indexOf = Array.prototype.indexOf;
    var i=node.sync, b=buf8;
    while(1) {
        node.retry++;
        i=b.indexOf(0xFF,i); if(i==-1 || (b[i+1] & 0xE0 == 0xE0 )) break;
        i++;
    }
    if(i!=-1) {
        var tmp=node.buf.slice(i); //carefull there it returns copy
        delete(node.buf); node.buf=null;
        node.buf=tmp;
        node.sync=i;
        return true;
    }
    return false;
}

function decode(node) {
    context.decodeAudioData(node.buf,
    function(decoded){
        node.source  = context.createBufferSource();
        node.source.connect(context.destination);
        node.source.buffer=decoded; 
        node.source.noteOn(context.currentTime);
        console.log('IT WORKED!  DECODED', decoded);
    },
    function(){ // only on error attempt to sync on frame boundary
        //console.log('error');
        if(syncStream(node)) decode(node);
    });
}

【问题讨论】:

    标签: node.js audio ffmpeg client-side


    【解决方案1】:

    您这样做是为了好玩还是为了现实生活中的产品?我们已经在我们的一个项目中成功使用了 vlc 流服务器,在 Ubuntu 上安装它很容易http://www.videolan.org/vlc/streaming.html

    【讨论】:

    • 我主要是为学校项目做这个。它必须流式传输到网络浏览器,因此 vlc 无法工作
    • 我知道,无论如何,vlc 在此设置的服务器端运行并发送到您想要的任何客户端,例如wiki.videolan.org/Simple_Stream_VLC_to_Website
    • 问题是我想通过 WAN 进行流式传输,而流式传输视频的机器将无法处理大量流量。我想将视频流式传输到网络服务器(节点),然后将该数据从那里写入所有连接的客户端。
    猜你喜欢
    • 2017-07-12
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2017-10-28
    • 2018-07-30
    相关资源
    最近更新 更多