Creating a GIF is not hard, actually it’s a one-liner.
But if you have a shaky video as source with slight over-exposure and to regard a size-limit for the final result, then the challenge rises.
Article deals with deshaking, greyscaling, cropping, size-reduction and a palette-optimized GIF-output without the infamous dithering.
The following settings are listed in best order. Trimming can be done first, because then no unnecessary video-material is processed.
Best for a 2 MiByte-limit are 12 fps, 500px wide, greyscaled (if necessary) GIF of 3-4 s length. Together with some minor cropping.
- update to latest ffmpeg-version:
Not doing so can result in issues with x264-codec and the palette-feature is not working (because not implemented yet)
ZSH1ffmpeg -i in.mp4 -vf deshake out.mp4
Uses a typical default setting for deshaking. For worse cases, try this manually configured setting.
ZSH1ffmpeg -v warning -i in.mp4 -vf "fps=12,scale=600:-1:flags=lanczos,format=gray" out.mp4
-1 as second parameter for scale means “proportionally scaled”; combined video-filtering-flags are separated by commas
- trim (length and starting position):
ZSH1ffmpeg -i in.mp4 -ss 00:00:12 -t 00:00:34 out.mp4
Read: copy from second 12 the following 34 seconds to the output. If smaller parts are needed, use a decimal dot after the second.
ZSH1ffmpeg -i in.mp4 -vf "crop=320:240:20:430" out.mp4
Crop a block with width 320 px and height of 240 px starting from (x,y)=(20,430). I prefer to measure the best position and size via GIMP’s selection-tool and a still-frame of the current video.
- final conversion to GIF (palette-optimized):
Use the suggested palette-based rendering skript from this page. In short: it first generates a palette for all the frames for the whole animation. Then renders using ffmpeg the GIF from the source.
ZSH1./gifenc.sh in.mp4 out.gif
Sometimes I load the resulting GIF into GIMP, crop it again and run its “GIF-animation-optimization” (not really helpful, because ffmpeg renders already with “combine” frame-disposal) to squeeze even the last bits out of it. Or delete manually some frames at the beginning or end.