Skip to content

Instantly share code, notes, and snippets.

@bellbind
Created September 27, 2013 06:29
Show Gist options
  • Save bellbind/6724836 to your computer and use it in GitHub Desktop.
Save bellbind/6724836 to your computer and use it in GitHub Desktop.
[nodejs][linux][ffmpeg][chrome][html5]live streaming example on linux
var http = require("http");
var cp = require("child_process");
// live streaming example on linux (for chrome, not for firefox)
// for ubuntu, required libavcodec-extra-53 instead of libavcode53
// for ffmpeg command
// see: http://granular.cs.umu.se/browserphysics/?p=2287
var server = http.createServer(function (req, res) {
console.log(req.url);
if (req.url === "/") {
res.writeHead(200, {
"content-type": "text/html;charset=utf-8",
});
res.end([
"<!doctype html>",
"<html><head><meta charset='utf-8'/></head>",
"<body>",
"<video src='/video.webm' autoplay='true' controls='true' ",
"width='600' height='600' />",
"</body></html>",
].join(""));
return;
}
if (req.url === "/video.webm") {
res.writeHead(200, {
"content-type": "video/webm",
});
var ffmpeg = cp.spawn("ffmpeg", [
"-re", "-f", "x11grab", "-r", "100",
"-s", "600x600", "-i", ":0+0,0",
"-g", "0", "-me_method", "zero", "-flags2", "fast",
"-vcodec", "libvpx",
"-preset", "ultrafast",
"-tune", "zerolatency",
"-b:v", "1M", "-crf", "40", "-qmin", "5", "-qmax", "5",
"-f", "webm",
"-"
]);
res.on("close", function () {ffmpeg.kill();});
ffmpeg.stderr.pipe(process.stderr);
ffmpeg.stdout.pipe(res);
return;
}
});
server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment