Skip to content

Instantly share code, notes, and snippets.

@HalidCisse
Forked from dbussert/livestream
Created October 8, 2019 20:18
Show Gist options
  • Save HalidCisse/4989d48d3f7327f5c12c2f6fed63db69 to your computer and use it in GitHub Desktop.
Save HalidCisse/4989d48d3f7327f5c12c2f6fed63db69 to your computer and use it in GitHub Desktop.
var child_process = require('child_process'),
http = require('http');
url = require('url'),
ffmpeg = null;
var livestream = function (req, resp) {
// For live streaming, create a fragmented MP4 file with empty moov (no seeking possible).
var input = 'udp://225.1.1.1:8208';
console.log("Input: " + input, ffmpeg)
resp.writeHead(200, {
//'Transfer-Encoding': 'binary'
"Connection": "keep-alive"
, "Content-Type": "video/mp4"
//, 'Content-Length': chunksize // ends after all bytes delivered
, "Accept-Ranges": "bytes" // Helps Chrome
});
if (!ffmpeg) {
ffmpeg = child_process.spawn("ffmpeg", [
"-i", input, "-vcodec", "copy", "-f", "mp4", "-movflags", "frag_keyframe+empty_moov",
"-reset_timestamps", "1", "-vsync", "1","-flags", "global_header", "-bsf:v", "dump_extra", "-y", "-" // output to stdout
], {detached: false});
ffmpeg.stdout.pipe(resp);
ffmpeg.stdout.on("data",function(data) {
console.log("Data");
});
ffmpeg.stderr.on("data", function (data) {
console.log("Error -> " + data);
});
ffmpeg.on("exit", function (code) {
console.log("ffmpeg terminated with code " + code);
});
ffmpeg.on("error", function (e) {
console.log("ffmpeg system error: " + e);
});
}
req.on("close", function () {
shut("closed")
})
req.on("end", function () {
shut("ended")
});
function shut(event) {
//TODO: Stream is only shut when the browser has exited, so switching screens in the client app does not kill the session
console.log("Live streaming connection to client has " + event)
if (ffmpeg) {
ffmpeg.kill();
ffmpeg = null;
}
}
return true
}
var http_server = http.createServer(livestream).listen(9090, function () {
console.log("Server listening on port 9090");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment