vvo / gifify

😻 Convert any video file to an optimized animated GIF.
ISC License
6.18k stars 313 forks source link

Programatically tell when gif process is done #38

Open haffmaestro opened 8 years ago

haffmaestro commented 8 years ago

Hey there, trying to make a loop that goes through a folder of movies and converts them all, but I cant seem to figure out how to make it not start making a new gif before the last one finished.

I tried binding to the close callback on the write stream that the gifify command returns, but that doesnt seem to be all that is happening.

Loving gifify though! Any advice on this? Or way to achieve this @vvo?

vvo commented 8 years ago

Hi, I am also unsure which stream will emit the right event or even what is the good stream event name on a write/read stream to listen to for end of write/read. This might even not be a good idea.

What we could do is add a new option like a callback here: gifify(input, options, callback)

The callback would be called when the last step exits: when "gifcicle" programs exits (https://nodejs.org/api/child_process.html#child_process_class_childprocess)

If you feel like contributing (pleeeease) then shoot a PR! If you need more guidance let me know

lithiumlab commented 7 years ago

Hey guys;

I think i have nice solution working for this. This snippet grabs all .mp4 from a folder and process them one by one, exported to the /out folder.

Im hooking on the 'finish' event of the writeableStream. Works great. I have just processed 600 videos with excellent results.

I can make this contribution to the examples folder, i have a few ideas already to improve ouput while processing with a bit more info. Great piece of software bwt! Let me know if you are interested.

var fs = require('fs');
var gifify = require('../');
var path = require('path');

var origin = path.join(__dirname, '/in');
var destination = path.join(__dirname, '/out');

var options = {
    resize: '300:-1',
    // from: 30, // in my case sources were tiny no need to extract pieces
    // to: 35,
    fps: 12,
    type: 'image/gif'
};

var the_files =[];
fs.readdir(origin, (err, files) => {
    files.forEach(file => {
        if (path.extname(file) === '.mp4') {

            var ob = {};
            ob.filename = path.basename(file, '.mp4');
            ob.input = ob.filename + '.mp4';
            ob.output = ob.filename + '.gif';
            ob.inputpath = path.join(origin, ob.input);
            ob.outputpath = path.join(destination, ob.output);
            // console.log(ob.inputpath, ob.outputpath);
            the_files.push(ob);

        }
    });
    console.log('Files: ',the_files.length);
    process_one();
})

function process_one(){

    if(the_files.length > 0){
        var item = the_files.pop();
        var gif = fs.createWriteStream(item.outputpath);
        var stream = gifify(item.inputpath, options).pipe(gif);
        stream.on('finish',function(){
            console.log('Remaining: ', the_files.length );
            process_one();
        })
    }
    else{
        console.log('Finished');
    }

}