video to gif nodejs in javascript

index.tsx
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs');

function videoToGif(inputVideoPath, outputGifPath) {
    ffmpeg(inputVideoPath)
        .inputOptions(['-vf scale=320:-1', '-t 10']) // Resize video to 320px width keeping aspect ratio, Trim video to first 10 seconds
        .outputOptions(['-vf "fps=10"']) // Set frames per second to 10
        .output(outputGifPath)
        .on('end', () => {
            console.log('Conversion complete');
        })
        .on('error', (err) => {
            console.error(`Error converting video to gif: ${err}`);
        })
        .run();
}

// Usage
const inputVideoPath = 'path/to/inputVideo.mp4';
const outputGifPath = 'path/to/outputGif.gif';
videoToGif(inputVideoPath, outputGifPath);
752 chars
22 lines

Above is a simple Node.js script using the fluent-ffmpeg library to convert a video to a gif. You can adjust the video size, duration, frame rate, and other options as needed within the inputOptions and outputOptions.

related categories

gistlibby LogSnag