Skip to content

Instantly share code, notes, and snippets.

@deoxxa
Forked from kanryu/gist:4351283
Last active December 10, 2015 00:28
Show Gist options
  • Save deoxxa/4351300 to your computer and use it in GitHub Desktop.
Save deoxxa/4351300 to your computer and use it in GitHub Desktop.
//// problem: node.js make memory leaks about child_proces
//
// the javascript doing it tha same to the shell code
// $ cat mako_* | ./ffmpeg -y -f image2pipe -r 1 -vcodec bmp -r 29.7 -i - -vcodec utvideo movie.avi
var ffmpeg = require('basicFFmpeg');
var fs = require('fs');
var sprintf = require('sprintf').sprintf;
var spawn = require('child_process').spawn,
exec = require('child_process').exec;
function genProcArgs(processor) {
var args = ['-y', '-f', 'image2pipe', '-r', '1', '-vcodec', 'bmp', '-r', '29.7', '-i', '-', '-vcodec', 'utvideo', '-f', 'avi'];
args.push("test.avi");
return args;
}
var proc = spawn('ffmpeg', genProcArgs());
var files = []; // keeping all the names in an array makes the following logic a bit easier to follow
for (var i=0;i<=12;++i) {
files.push(sprintf("mako_%04d.bmp", i));
}
var i = 0;
function write_next_image() {
console.log(i);
if (i === 1000 * files.length) { // this is from your original code, j 0 -> 1000 and i 0 -> 13
proc.stdin.end();
return;
}
fs.readFile(files[i++ % files.length], function(err, data) {
var res = proc.stdin.write(data); // .write returns false when no more data can be accepted successfully
if (res) {
write_next_image(); // if we can write more data, do it straight away
} else {
proc.stdin.on("drain", write_next_image); // otherwise, schedule a write operation to happen on the next "drain" event
}
});
}
write_next_image(); // start writing images!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment