animationffmpeg

FFmpeg: high quality animated GIF?


I'm generating animated a GIF from a video on my server.

The generated GIF is not really high quality and it looks like the pixels are huge.

Example:

example GIF

This is how I generate the GIF:

shell_exec("/usr/bin/ffmpeg -i video.mkv -vf scale=500:-1 -t 10 -r 10 image.gif");

I did a search on Google and came across this:

shell_exec("/usr/bin/ffmpeg -i video.mkv -r 20 -f image2pipe -vcodec ppm - | convert -delay 5 - output.gif");

But the command above doesn't do anything and no output.gif is being generated at all.

There are some tutorials that I came across but none of them worked for me and some of them involve using ImageMagick which I dont have access to.

Could someone please let me know if there is a clear way to generate a high-quality GIF using FFmpeg?


Solution

  • I've written a tool specifically for maximum quality:

    https://gif.ski

    ffmpeg -i video.mp4 frame%04d.png
    gifski -o clip.gif frame*.png
    

    or (note - at the end)

    ffmpeg -i video.mp4 -f yuv4mpegpipe - | gifski -o clip.gif -
    

    It generates good per-frame palettes, but also combines palettes across frames, achieving even thousands of colors per frame.

    If you want to reduce the video dimensions, add a scaling filter (or --width in gifski):

    ffmpeg -i video.mp4 -vf scale=400:240 frame%04d.png
    

    If you want to reduce the frame rate, add the fps filter:

    ffmpeg -i video.mp4 -vf fps=12 frame%04d.png
    

    You can combine the filters with -vf scale=400:240,fps=12


    New versions of gifski are also good at producing small files. With the right settings you may get a file that is both nicer and smaller than ffmpeg's, see --help in gifski for the quality/compression options.