Skip to content

Instantly share code, notes, and snippets.

@volcan01010
Created January 13, 2022 09:40
Show Gist options
  • Save volcan01010/14593d75f8c3a47bf1c91fd7f98a1bf0 to your computer and use it in GitHub Desktop.
Save volcan01010/14593d75f8c3a47bf1c91fd7f98a1bf0 to your computer and use it in GitHub Desktop.
Creating animated GIFs of terminal output

There are various ways to create animated GIFs for terminal output. Three methods that can be used on Linux (in this case Ubuntu) are described below.

asciinema + asciicast2gif

This is probably the easiest way. The disadvantage of this method is that when I tried to record an animation of a progress bar created with the Python Rich library, it didn't show up.

Asciinema can be be installed with pip install asciinema Asciicast2gif is more complicated and is best run from a Docker container. Creating an alias in .bashrc makes it easy to use.

# alias for asciinema rendering (requires Docker to be installed and user in docker group)
alias asciicast2gif='docker run --rm -v $PWD:/data asciinema/asciicast2gif'

The commands are:

asciinema rec my_file.cast
asciicast2gif my_file.cast my_file.gif

The my_file.cast file is JSON and can be modified relatively easily to tweak timesteps or modify content. There are options for setting width, height, colour theme etc.

byzanz-record + gifsicle

Byzanz is a screen recorder that can output to gif, while gifsicle is a tool for editing gifs. The advantage of this method is that it records exactly what plays on the screen just as you see it. The downside is that getting the exact timing and area of the screen to record correct can be a bit fiddly.

sudo apt install byzanz gifsicle

You can make Byzanz execute a command as it begins, but I found it easier to send it to the background and start the code myself. The following command records an 8 second gif of the lower left portion of the screen after a 3 second delay.

byzanz-record -d 8 --delay 3 -x 0 -y 804 -w 730 -h 272 my_file.gif &

If the gif ends up being longer that what you are demonstrating, you can trim the end with gifsicle. The following command takes the first 11 frames from my_file.gif and saves them as my_file_trimmed.gif with an output quality of 3.

gifsicle my_file.gif '#0-11' -O3 -o my_file_trimmed.gif

Note that when I tried to trim frames from the start of the gif the output was corrupted. I think this is because some subsequent frames only contain the changes, rather than the whole image each time.

terminalizer

There is another tool called terminalizer that can also be used. I tried it before but found it tricky to install. Here is a gist with instructions for installing terminalizer that may be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment