简体   繁体   English

Node JS 广播(音频流)

[英]Node JS radio (audio stream)

Good evening!晚上好! I just try to do my own internet radio, using Node JS.我只是尝试使用 Node JS 做我自己的网络广播。 I saw a lot of questions and tutorials about it, but all of them are old and hard to understand.我看到了很多关于它的问题和教程,但它们都是旧的,难以理解。

const fs = require('fs'),
files = fs.readdirSync('./music'), // I have some mp3 in music folder
clips = [],
dhh = fs.createWriteStream('./tracks.mp3'); // create output stream

let stream, currentfile;

files.forEach(file => {
  clips.push(file);
});

// recursive function
const main = () => {
  if (!clips.length) {
    dhh.end('Done');
    return;
  }

  currentfile = './music/' + clips.shift();
  stream = fs.createReadStream(currentfile);

  stream.pipe(dhh, { end: false });

  stream.on('end', function() {
    console.log(currentfile + ' appended');
    main();
  });
};

main();

So I have all my mp3 in one single file.所以我把所有的 mp3 都放在一个文件里。 What can I do to stream this file to lot of different users, when they are connected.当他们连接时,我该怎么做才能将此文件流式传输给许多不同的用户。 Lot of answers about it advice use BinaryJS, but it was written about 5 years ago.很多关于它的建议的答案都使用 BinaryJS,但它是大约 5 年前写的。 I just do not how to start, so I need your help.我只是不知道如何开始,所以我需要你的帮助。 Thank you!谢谢!

I tried something like this我试过这样的事情

const http = require('http'),
fs = require('fs'),
filePath = './music/my.mp3',
stat = fs.statSync(filePath);

http.createServer(function(request, response) {
  fs.createReadStream(filePath).pipe(response);

  response.writeHead(200, {
    'Content-Type': 'audio/mpeg',
    'Content-Length': stat.size,
  });

}).listen(4000);

When user connect to port 4000, music starts playing, but it is not online stuff, when another user connect music also playing from beginning, but I want to do it like online radio.当用户连接到端口 4000 时,音乐开始播放,但不是在线内容,当另一个用户连接音乐时也从头开始播放,但我想像在线广播一样播放。 But it doesn't work :(但它不起作用:(

I'm also tryin to do the same thing ur doing, and found a very informative blog article here .我也在尝试做你正在做的同样的事情,并在这里找到了一篇非常有用的博客文章。 There's one just one thing in it, it doesn't explain how to add writables/ consumers to the server, so it is incomplete.其中只有一件事,它没有解释如何向服务器添加可写/消费者,所以它是不完整的。 But you can create 90% of your radio with the rest.但是您可以用其余部分来制作 90% 的收音机。 Also I used a different approach so I'd like to share it here to help another soul.我也使用了不同的方法,所以我想在这里分享它以帮助另一个灵魂。 ('writable1' is not defined)Feel free to correct the code. ('writable1' 未定义)随意更正代码。

const express = require('express'),
    router  = express.Router(),
    fs      = require('fs');
    Throttle = require('throttle'),
    ffprobe = require('ffprobe'),
    ffprobeStatic = require('ffprobe-static');

    // Play all songs from db
    router.get('/', function(req, res){

    const bitRate = ffprobe('myAudio.mp3', { path: ffprobeStatic.path }, function(err, info){
        console.log(info.streams[0].bit_rate);
    });
    const readable = fs.createReadStream('myAudio.mp3');
    const throttle = new Throttle(bitRate / 8);
    const writables = [writable1, writable2, writable3];

    readable.pipe(throttle).on('data', (chunk) => {
           for (const writable of writables) {
           writable.write(chunk);
         }
      });

    });

    module.exports = router;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM