节点(套接字)实时音频流/广播 [英] node (socket) live audio stream / broadcast

查看:151
本文介绍了节点(套接字)实时音频流/广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问,是否有任何简便的方法可以通过NODE.js以及可能的SOCKET.IO将服务器上的媒体文件(ogg,mp3,spx ..)流式传输(广播)到客户端(浏览器)?

Please, is there any easy way to stream (broadcast) media file (ogg, mp3, spx..) from server to client (browser) via NODE.js and possibly SOCKET.IO?

我必须在服务器端记录音频输入,然后才能为许多客户端实时播放. 我一直在处理binary.js或socket.io流,但无法正确处理.

I have to record audio input on the server side and then be able to play it realtime for many clients. I've been messing with binary.js or socket.io streams but wasnt able to get it right.

我尝试用speex,vorbis或lame编码音频输入,然后通过FS将其加载到客户端,但是我没有成功.还是我必须先捕获PCM,然后在浏览器中对其进行解码?

I've tried to encode audio input with speex, vorbis or lame and then load it by FS to client but I havent been successful. Or do i have to capture PCM and then decode it in browser?

对此有任何建议,我发现没有任何帮助.

Any suggestion on this, nothing Ive found ever helped me.

非常感谢您提供任何提示,链接和想法.

Many thanks for any tips, links and ideas.

推荐答案

您将要查找可在Streams上使用的软件包,然后从那里开始根据需要使用流将其管道输出.使用Express或仅使用内置的HTTP,您就可以轻松完成此操作.这是围绕 osx-音频构建的示例,该示例提供PCM流

You'll want to look for packages that work on Streams and from there it's just about piping your streams to output as necessary. Using Express or just the built-in HTTP you can accomplish this quite easily. Here's an example built around osx-audio which provides a PCM stream, lame which can encode a stream to mp3, and Express:

var Webcast = function(options) {

  var lame = require('lame');
  var audio = require('osx-audio');
  var fs = require('fs');

  // create the Encoder instance
  var encoder = new lame.Encoder({
    // input
    channels: 2,        // 2 channels (left and right)
    bitDepth: 16,       // 16-bit samples
    sampleRate: 44100,  // 44,100 Hz sample rate

    // output
    bitRate: options.bitrate,
    outSampleRate: options.samplerate,
    mode: (options.mono ? lame.MONO : lame.STEREO) // STEREO (default), JOINTSTEREO, DUALCHANNEL or MONO
  });

  var input = new audio.Input();
  input.pipe(encoder);

  // set up an express app
  var express = require('express')
  var app = express()

  app.get('/stream.mp3', function (req, res) {
    res.set({
      'Content-Type': 'audio/mpeg3',
      'Transfer-Encoding': 'chunked'
    });
    encoder.pipe(res);
  });

  var server = app.listen(options.port);
}

module.exports = Webcast;

如何获得输入流可能是最有趣的部分,但这取决于您的实现.流行的request软件包也是围绕Streams构建的,因此它可能只是HTTP请求而已!

How you get your input stream might be the most interesting part, but that will depend on your implementation. The popular request package is built around Streams as well though, so it might just be an HTTP request away!

这篇关于节点(套接字)实时音频流/广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆