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:
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?
By default, FFmpeg uses a generic 256-color palette for every GIF encoding and doesn't take into account the colors in the input video. However, we can use the palettegen and paletteuse filters of FFmpeg to generate a custom palette from the colors in our original video to create a higher-quality GIF.
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
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:
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
The key issue is that any gif picture or frame has an extremely limited palette of only 256 of the possible millions of colors in your video.
This is well explained here.
So, fairly recently (2015, version 2.6) ffmpeg got the palettegen and paletteuse filters that can generate better pallettes for each frame.
Therefore, make sure you are using a fairly recent version of ffmpeg.
So, there's your secret and key search term to get you to make high quality gifs in no time - study up on palettegen filters. Reddit beware.
Some references:
ffmpeg 2.6 release notes
ffmpeg docs
superuser
blog.phk.me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With