mkaz.blog

Working with Animated GIFs

A few resources and snippets for working with animated GIFs on Linux. I use animated GIFs for various screenshots to highlight bugs or demonstrate features. It is quite useful to have

For Windows 10, the ScreenToGif app is excellent. It is the only app I know that shows you every frame captured and allows editing them directly before saving. Unfortunately, it is only availalble on Windows.

For Linux, I use a couple of different tools to work with GIFs.

Capture

For capturing the screen, I use the Peek utility. It is simple and works well. It is also in most distro package repositories so readily available.

Convert

The two ways I use to convert a movie file to an animated gif are ffmpeg and gifski.

Convert mp4 to GIF using ffmpeg

Using ffmpegg is a two step process, first you need to create a palette file for the colors.

ffmpeg -i input.mp4 -filter_complex "[0:v] palettegen" palette.png

The second command then uses that palette to create the GIF, in the example below I set the scale to 720px and frames per second to 12, adjust to match your goals.

ffmpeg -i input.mp4 -i palette.png -filter_complex "fps=12,scale=720:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gif

Convert movie to GIF using gifski

The second method to convert a mpg4 to animated gif is to use the gifski utility. Once setup this is much easier to use and produces great results but can be a little trickier to setup.

Gifski is written in Rust and requires a Rust enviornment to build, plus you'll need to install the following packages. On Ubuntu 21.04, I used:

apt install clang libclang-dev libavutil-dev libavformat-dev libavfilter-dev libavdevice-dev

You can then clone the Gifski repo and build using:

cargo build --release --features=video

Once installed, use gifski like so:

gifski --fps 12 --width 720 -o output.gif input.mp4

Edit

One task I'll often want to do is to remove a couple frames from the beginning or end. To do this I use the gifsicle command-line tool, available in package repositories.

The following command creates an output.gif using frames 8-45 from the input gif and resizes down to 720x360.

gifsicle input.gif "#8-45" --resize 720x360 -o output.gif -O3

You can combine multiple GIF using:

gifsicle in1.gif in2.gif in3.gif > output.gif

You can also get information about a GIF using

gifsicle -I input.gif

Create

You can use gifski to take a set of images and create a GIF. For example, here are a set of numbers in PNG format, each number is its own file. number_N.png

The command to create a animated gif of the numbers

gifski number_*.png --output numbers.gif

If you want to slow it down, so each number shows for one second, alter the FPS

gifski --fps 1 number_*.png --output numbers.gif

You can also use ffmpeg, but again I find the gifski args a little easier to work with. An interesting result is gifski used the transparent background, but ffmpeg used a black background.

ffmpeg -framerate 10 -i number_%1d.png numbers.gif