Created
January 17, 2020 15:00
-
-
Save marcelmaatkamp/d03884d53ba90d70d16962f4d482870b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env node | |
| var request = require('request') ; | |
| var ffmpeg = require('ffmpeg'); | |
| var getPixels = require('get-pixels'); | |
| var GifEncoder = require('gif-encoder'); | |
| const sortAlphaNum = (a, b) => a.localeCompare(b, 'en', { numeric: true }) | |
| try { | |
| // file to extract images from | |
| var process = new ffmpeg('56884.ts'); | |
| process.then(function (video) { | |
| console.log(video.metadata); | |
| video.fnExtractFrameToJPG('extracted/', { | |
| start_time: null, | |
| duration_time: video.duration, | |
| frame_rate: video.fps, | |
| size: video.resolution, | |
| number: 100, | |
| every_n_frames: null, | |
| every_n_seconds: null, | |
| every_n_percentage: null, | |
| keep_pixel_aspect_ratio: false, | |
| keep_aspect_ratio: false, | |
| padding_color: 'black', | |
| file_name: '%t' | |
| }, function (error, images) { | |
| if (!error) { | |
| images = images.sort(sortAlphaNum) | |
| var file = require('fs').createWriteStream('extracted/img.gif'); | |
| // set headers | |
| var gif = new GifEncoder( | |
| video.metadata.video.resolution.w, | |
| video.metadata.video.resolution.h | |
| ); | |
| gif.pipe(file); | |
| gif.setQuality(100); | |
| gif.setDelay(0); | |
| gif.writeHeader(); | |
| // stitch to an animate gif | |
| var addToGif = function(images, counter = 0) { | |
| getPixels(images[counter], function(err, pixels) { | |
| gif.addFrame(pixels.data); | |
| // gif.read(); | |
| if (counter === images.length - 1) { | |
| gif.finish(); | |
| } else { | |
| addToGif(images, ++counter); | |
| } | |
| }) | |
| } | |
| addToGif(images); | |
| } else { | |
| console.log('Error: ', error); | |
| } | |
| }); | |
| }, function (err) { | |
| console.log('Error: ', err); | |
| }); | |
| } catch (e) { | |
| console.log('Error: ', e); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment