Skip to content

Instantly share code, notes, and snippets.

@kurokikaze
Created April 22, 2010 20:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kurokikaze/0ad526ee2bebc006776e to your computer and use it in GitHub Desktop.
Save kurokikaze/0ad526ee2bebc006776e to your computer and use it in GitHub Desktop.
Simple Node.js FFMPEG interface
var sys = require('sys'),
child_process = require('child_process');
var encoder = child_process.spawn('ffmpeg',['-i', "video.mkv", '-s', '300x200', '-f', 'avi', '-vcodec', 'mpeg4', 'video.avi']);
var total_time = 0;
var total_data = ''
encoder.stderr.addListener('data', function(data) {
if (data) {
total_data += data.toString();
if (total_data.toString().match(/Duration:\s\d\d:\d\d:\d\d\.\d\d/)) {
var time = total_data.toString().match(/Duration:\s(\d\d:\d\d:\d\d\.\d\d)/).toString().substring(10,21);
sys.puts('DATA:' + total_data.toString());
sys.puts('Time:' + time);
var seconds = parseInt(time.substr(0,2))*3600 + parseInt(time.substr(3,2))*60 + parseInt(time.substr(6,2));
total_data = '';
total_time = seconds;
sys.puts('sec: ' + seconds);
}
// sys.puts('>' + data.toString());
// sys.puts(data.toString().substr(data.toString().length - 6, 5));
if (data.toString().substr(data.toString().length - 6,5) == '[y/N]') {
sys.puts('File already exist, overwriting');
encoder.stdin.write('Y', 'ascii');
}
if (data.toString().substr(0,5) == 'frame') {
var time = parseInt(data.toString().match(/time=\s*(\d*)\.\d*/)[0].toString().substr(5));
sys.puts('Percent done: ' + ((time / total_time) * 100) + '% (' + time + ' of ' + total_time);
}
}
//sys.puts('OUT:'+ data);
});
encoder.stderr.addListener('exit', function(data) {
sys.puts('Encoding done: ' + data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment