Skip to content

Instantly share code, notes, and snippets.

@dbussert
Forked from deandob/livestream
Last active July 9, 2023 11:33
Show Gist options
  • Select an option

  • Save dbussert/0cf0476b15ab45e0851d to your computer and use it in GitHub Desktop.

Select an option

Save dbussert/0cf0476b15ab45e0851d 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");
});
@jomeier
Copy link
Copy Markdown

jomeier commented Apr 19, 2015

For me it doesn't work at all in firefox (37.0.1) but video is shown in Chrome (42.0.2311.90 m).

In no ffmpeg configuration I get audio.

Which settings do you use for getting audio? Could you post your ffmpeg settings?

@Globik
Copy link
Copy Markdown

Globik commented Jan 22, 2016

@jomeier this is a bad aproach all that above. You may want to use gstreamer or janus webrtc gateway, binary.js socket.

@atomictom
Copy link
Copy Markdown

Thanks for posting this!

For anyone looking to use this, removing the '-bsf:v dump_extra' option fixed the video for me for some reason.

@a9595776
Copy link
Copy Markdown

a9595776 commented Feb 4, 2016

sorry, this code can't run on iphone's safari,but can run on mac's safari. I don't know why not?
I try to removing the '-bsf:v dump_extra', but it can't run on iphone's safari still ...
Thank you~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment