Skip to content

Instantly share code, notes, and snippets.

@ngryman
Created June 5, 2015 14:47
Show Gist options
  • Select an option

  • Save ngryman/4ea3237bd0a281172ae2 to your computer and use it in GitHub Desktop.

Select an option

Save ngryman/4ea3237bd0a281172ae2 to your computer and use it in GitHub Desktop.
transcode stream.
var PassThrough = require('stream').PassThrough
, util = require('util')
, spawn = require('child_process').spawn
, os = require('os');
function Transcoder() {
PassThrough.call(this);
this.on('pipe', function(inStream) {
// inStream.unpipe(this)
this.inStream = inStream;
});
this.on('close', function() {
this.child.kill();
})
}
util.inherits(Transcoder, PassThrough);
Transcoder.prototype.pipe = function(outStream) {
// -nostats
var child = this.child = spawn('ffmpeg', '-i pipe:0 -f matroska -c:v copy -c:a mp3 pipe:1'.split(' '), {
cwd: os.tmpdir()
})
child.on('exit', function(code) {
if (!code) {
this.emit('finish');
}
else {
this.emit('error', new Error('FFmpeg error'));
}
}.bind(this));
this.inStream.pipe(child.stdin);
child.stdout.pipe(outStream);
child.stderr.pipe(process.stdout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment