Luis Javier
Luis Javier

Reputation: 79

A open source RTMP streaming server for stream mp3 on deman?

I need a open source RTMP server for stream mp3s on demand for linux(centos), i found a few but these dont let me stream on demand, just only live. thanks

Upvotes: 1

Views: 1416

Answers (1)

Scott Stensland
Scott Stensland

Reputation: 28285

this does on demand ... just install node.js and execute using :

node file_containing_below.js

and yes consider it open source ;-)

var http = require('http');
var fs = require('fs');
var util = require('util');

var port = 8888;
var host = "localhost";

http.createServer(function (req, res) {

  var path = "/path/to/some/video/or/audio/file.mp3"; // mp4 or most any video/audio file


  var stat = fs.statSync(path);
  var total = stat.size;

  if (req.headers.range) { // meaning client (browser) has moved the forward/back slider
                                         // which has sent this request back to this server logic ... cool
    var range = req.headers.range;
    var parts = range.replace(/bytes=/, "").split("-");
    var partialstart = parts[0];
    var partialend = parts[1];

    var start = parseInt(partialstart, 10);
    var end = partialend ? parseInt(partialend, 10) : total-1;
    var chunksize = (end-start)+1;
    console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize);

    var file = fs.createReadStream(path, {start: start, end: end});
    res.writeHead(206, { 'Content-Range': 'bytes ' + start + '-' + end + '/' + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': 'video/mp4' });
    file.pipe(res);
  } else {
    console.log('ALL: ' + total);
    res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'video/mp4' });
    fs.createReadStream(path).pipe(res);
  }
}).listen(port, host);

console.log("Server running at http://" + host + ":" + port + "/");

now just point some client (your browser) at URL

http://localhost:8888/

and yes once playing the forward/back sliders automagically work on an arbitrary client (browser)

Upvotes: 1

Related Questions