GIF screencasting; the UNIX way

gifscreencastingvideo

To take a static screenshot of a selected part of my screen, I often use scrot with -s shot.png. This is great for adding illustrations to StackExchange posts. I even found this script to automatically upload such a screenshot to Imgur.com and put a link in my X clipboard!

Let's turn this up to twelve: How do I similarly create a GIF screencast?

There are programs like recordmydesktop, byzanz & co as discussed on Ask Ubuntu that aim to be "user friendly", but in my experience are buggy, inefficient, mostly unscriptable and unsuited for little one-off things like this.

I just want to select an area and record a GIF, with a console command I can understand, not some arcane unscriptable GUI monstrosity.

How can I do this?

Best Answer

OK then

GIF vimcast!

I started ffcast, did vim, quit ffcast, then converted .avi.gif.

I ran the recording commands in another terminal. Polished script for your $PATH at the end of this answer.

What happened?

Capturing

FFcast helps the user interactively select a screen region and hands over the geometry to an external command, such as FFmpeg, for screen recording.

ffcast is the glorious product of some hacking at the Arch Linux community (mainly lolilolicon). You can find it on github (or in the AUR for Archers). Its dependency list is just bash and ffmpeg, though you'll want xrectsel (AUR link) for interactive rectangle selection.

You can also append ffmpeg flags right after the command. I set -r 15 to capture at 15 frames per second and -codec:v huffyuv for lossless recording. (Play with these to tweak the size/quality tradeoff.)

GIFfing

ImageMagick can read .avi videos and has some GIF optimisation tricks that drastically reduce file size while preserving quality: The -layers Optimize to convert invokes the general-purpose optimiser. The ImageMagick manual has a page on advanced optimisations too.

Final script

This is what I have in my $PATH. It records into a temporary file before converting.

#!/bin/bash
TMP_AVI=$(mktemp /tmp/outXXXXXXXXXX.avi)
ffcast -s % ffmpeg -y -f x11grab -show_region 1 -framerate 15 \
    -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 out.gif

Thanks to BenC for detective work in figuring out the correct flags after the recent ffcast update.

If you'd like to install the dependencies on a Debian-based distro, Louis has written helpful installation notes.

Wheeeeee!

Related Question