Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programatically tell when gif process is done #38

Open
haffmaestro opened this issue Dec 27, 2015 · 2 comments
Open

Programatically tell when gif process is done #38

haffmaestro opened this issue Dec 27, 2015 · 2 comments

Comments

@haffmaestro
Copy link

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
Copy link
Owner

vvo commented Jan 4, 2016

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
Copy link

lithiumlab commented Feb 12, 2017

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');
    }

}



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants