Skip to content

Instantly share code, notes, and snippets.

@muammar
Forked from mharsch/gist:5144208
Created August 24, 2016 18:16
Show Gist options
  • Save muammar/255d40e4a4f48337d8dcb688d6c2a164 to your computer and use it in GitHub Desktop.
Save muammar/255d40e4a4f48337d8dcb688d6c2a164 to your computer and use it in GitHub Desktop.
transcode video streams using node.js Streams and avconv (aka ffmpeg)

The avconv utility can be made to work in 'the Unix way' by specifying stdin and/or stdout instead of filenames for the input and output respectively. See: http://libav.org/avconv.html#pipe

Example:

cat input.ts | avconv -i pipe:0 -f mp4 -movflags frag_keyframe pipe:1 | cat > output.mp4

Using node's require('child_process').spawn(), we can pipe streams of video data through avconv's stdin and stdout and thus Stream All The Things.

var fs = require('fs');
var child = require('child_process');

var input_file = fs.createReadStream('./input.ts');
var output_file = fs.createWriteStream('./output.mp4');

var args = ['-i', 'pipe:0', '-f', 'mp4', '-movflags', 'frag_keyframe', 'pipe:1'];
var trans_proc = child.spawn('avconv', args, null);

input_file.pipe(trans_proc.stdin);
trans_proc.stdout.pipe(output_file);

trans_proc.stderr.on('data', function (data) {
	console.log(data.toString());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment