Created
August 30, 2018 05:13
-
-
Save bootrino/1a7ba46276fb17fcd48ba1f4c0988445 to your computer and use it in GitHub Desktop.
nodejs buildgif fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function buildGIF(frames: any) { | |
let firstFrame = new PNG(frames[0].dataBytes); | |
let width = firstFrame.width; | |
let height = firstFrame.height; | |
//console.log(frames.length + " frames at " + width + "x" + height); | |
let encoder = new GIFEncoder(width, height); | |
encoder.createReadStream().pipe(fS.createWriteStream('output.gif')); | |
encoder.start(); | |
let framePromises = frames.map(async (frame: any) => { | |
return await new Promise((resolve: any, reject: any) => { | |
let parsedFrame = new PNG(frame.dataBytes); | |
parsedFrame.decode((px: any) => { | |
resolve(px); | |
}); | |
}).catch(e => { | |
//console.log(e); | |
}); | |
}); | |
for (let framePromise of framePromises) { | |
encoder.addFrame(await | |
framePromise | |
); | |
} | |
encoder.finish(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment