Quite often, I use mogrify from ImageMagick and gifsicle to generate an animated GIF from a series of screen grabbed PNG images as listed below, such as in this post. I can never recall what options to use, especially for -format part.

1   Basic

1.1   From images


mogrify -format gif input-*.png
gifsicle -d100 -O3 -k256 -l0 input-*.gif > output.gif

-d delays 100*100thseconds is 1 second, -o optimization level 3, -k 256 colors, and -l loop forever.

In that post, I had to rotate the image for posting using convert, also from ImageMagick.

1.2   Using FFmpeg

With FFmpeg, it can directly output as GIF format, however, it will use dithering and that most likely wouldn’t look good if you are recording a terminal window, furthermore, it will increase the file size significantly.

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBYxJCN-4iGcVoOfBVDJHcCbynP4PcPf7s4pxPAvB2RC9IVehAAnjG-znD8gLjPKQvvntCL5gdnb5hwvWljtESqYOuebBONuRFTQbLXBGrPpZxbMj0DVY_Iy8ImWgkwvc6YNx0kYEVArQ/s800-Ic42/256.png

High quality GIF with FFmpeg explains how to generate and use a palette. In a terminal that use standard 16-color or 256-color, you can record using the following command with this palette image on the right:


ffmpeg -f x11grab -show_region 1 -r $FPS -s ${WIDTH}x${HEIGHT} \
-i :0.0+${X},${Y} -i 256.png \
-lavfi 'copy [x]; [x][1:v] paletteuse=dither=none' \
output.raw.gif

The key point is the dither=none option of paletteuse filter, that will stop FFmpeg from trying to dither.

Note

paletteuse is only added since FFmpeg 2.6 (2015-03-06).

2   Trimming

If you need to trim out some frames, you can use the command as follows, which trims out the first frame (#0) and anything after the sixteenth (#15).


gifsicle output.gif '#1-15' > output.trim.gif

There is also a --delete options, it could be used like:


gifsicle output.gif --delete '#0' '#16-' > output.trim.gif

3   Rotation


gifsicle --rotate-90 output.gif > output.90.gif

There is also --rotate-180 and --rotate-270. You can also use convert from ImageMagick to do the task as seen below.


convert output.gif -rotate -90 output-90.gif

-rotate takes degree in clockwise rotation, therefore -90 is counter-clockwise for 90°.

4   Last frame delay

To change last frame delay, so it wouldn’t restart too fast if short delay is used in generating the GIF, you can do the following to change last frame delay to 5 seconds:


gifsicle -b output.gif -d100 '#0--2' -d500 '#-1'

# or simply

gifsicle -b output.gif -d500 '#-1'

#0--2 selects the frames between the first frame and the second last frame, and #-1 the last frame. For frame number reference, read “Frame Selections” in gifsicle(1).