Skip to content

Instantly share code, notes, and snippets.

@krzysztofantczak
Forked from mharsch/gist:5144208
Created August 1, 2014 08:44
Show Gist options
  • Save krzysztofantczak/0c09c6214608be6ed44e to your computer and use it in GitHub Desktop.
Save krzysztofantczak/0c09c6214608be6ed44e to your computer and use it in GitHub Desktop.

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