Linux capture animated GIFs with rectangle select

Linux capture animated GIFs with rectangle select

There are multiple solutions capturing GIFs under Linux and I tried a lot. Most have usability issues, produce way to large output files or don’t even allow to grab a rectangle. As I am using i3 wm, it also needs to be able to be started via a shortcut without any GUI software.

During my search I stumbled upon a post on stack exchange, where the user had the idea to combine ffcast with ffmpeg and some compression. With some tweaks, like framerate, output file etc. this finally offers a flexible and fast way to capture Gifs in Linux.

Prerequisites:

  • xrectsel (Build it yourself or use AUR, if you are on Arch Linux)
  • ffcast (Build it yourself or use AUR, if you are on Arch Linux)
  • ffmpeg (available in most Distributions).
  • xcb
#!/usr/bin/env bash

TMP_AVI=$(mktemp /tmp/captureXXXXXXXXXX.avi)
OUTPUT_FILE="capture-$(date +'%Y-%m-%d_%H-%M-%S').gif"

echo "Hit [q] to stop recording"
ffcast -s % ffmpeg -y -f x11grab -show_region 1 -framerate 8 \
-video_size %s -i %D+%c -codec:v huffyuv \
-vf crop="iw-mod(iw\,2):ih-mod(ih\,2)" $TMP_AVI \
&& convert -set delay 10 -layers Optimize $TMP_AVI $OUTPUT_FILE

Save it in a shell script called for example gifcapture in your path (like in /usr/local/bin). Execute gifcapture from your terminal or bind it to a shortcut.

Stack exchange original source