Skip to content

Instantly share code, notes, and snippets.

@gerswin
Forked from velsa/app.js
Created November 2, 2015 16:11
Show Gist options
  • Save gerswin/da05866ab3626f842de5 to your computer and use it in GitHub Desktop.
Save gerswin/da05866ab3626f842de5 to your computer and use it in GitHub Desktop.
Express server used to launch ffmpeg, transcode stream from peerflix and serve it to HTML5 video tag
var express = require('express'),
http = require('http'),
path = require('path'),
child_process = require("child_process");
var app = express();
// Server settings
app.set('port', process.env.PORT || 9999);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.errorHandler());
// Serve the video
app.get('/',function(req,res){
// Write header
res.writeHead(200, {
'Content-Type': 'video/webm'
});
// Start ffmpeg
var ffmpeg = child_process.spawn("./ffmpeg", [
// Real time mode
// "-re",
// Source
"-i", "http://localhost:8888",
// Map multiple streams
"-map", "0:0", "-map", "0:1", "-map", "0:1",
// Some optimizations
// "-preset","ultrafast",
// "-tune","zerolatency",
// Motion algorithms off
// "-me_method","zero",
// "-flags2","fast",
// Quantization
// "-qmin","5",
// "-qmax","5",
// Video: VP8 encoding
"-c:v","libvpx",
// quality
"-crf","40",
// target bit rate
"-b:v","4M",
// Audio: Vorbis encoding
"-c:a","libvorbis",
// target bit rate
"-b:a","256K",
// Subtitles: copy
"-c:s","copy",
// File format
"-f","webm",
// Output to STDOUT
"-"
]);
// Pipe the video output to the client response
ffmpeg.stdout.pipe(res);
// Kill the subprocesses when client disconnects
res.on("close", function(){
ffmpeg.kill();
})
});
// Start server
http.createServer(app).listen(app.get('port'), function(){
console.log('Open http://localhost:' + app.get('port')+' to view the video from peerflix');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment