HOW TO CAPTURE YOUR SCREEN OUTPUT INTO AN ANIMATION (GIF)

Djamel Hassaine
4 min readMar 31, 2017

First published on dhassaine.co.uk 2016/01/10

This article describes how I created an animated title header (which is a basic hello world Java program). I’m afraid it is targeted at linux users and is quite technical; I’ve written it mainly so I don’t forget how to do it again, but I’m hoping some people out there familiar with linux may find it useful.

Recording a screencast

The first task was to create a screencast video showing the program being typed out in a text editor — in this case a text editor called vim. The screencast capturing tool I used is a free opensource program (as are all the tools used in this tutorial), called Byzanz; this is a small and efficient screencast creator: it records your desktop session or parts of it to an animated GIF, OGG Theora or Flash file format. You can download it from GitHub or if you are on a Debain platform you can install it via a synaptic, by running the following command:

sudo apt-get install byzanz

Once installed, to start recording to a GIF file format, run the following command:

byzanz-record –duration=90 –x=0 –y=0 –width=1920 –height=1080 out.gif

I gave myself plenty of recording time to finish writing the header program, 90 seconds (using –duration option). This turned out to be a lot more time than necessary. Apart from needing to shorten the video, it also needs the first few seconds removed which shows me messing around opening the text editor and then cropped.

Cropping

Cropping the video can be performed using GIMP which is an image manipulation and paint program. Make sure to crop via Tools->Transform tools->Crop. Then export the file to a GIF. I also increased the canvas size so that I could have a nice padded border.

Frame extraction

GIMP displays the individual frames in the layer panel, but before manipulating them you will need to un-optimise the GIF — GIMP uses a clever difference mechanism so that each consecutive frame is visualised by combining all the previous frames. While it is perfectly possible to do all the editing of the GIF in GIMP, I find it more practical to extract all the frames to individual PNG’s and then use other linux tools and the command line.

Extracting the frames can be performed by using ImageMagick‘s convert program:

convert -background ‘#0d0d0d’ -verbose -coalesce out.gif hello.png

You might notice that I’ve changed the background colour, this is to remove the transparent background sections after I increased the canvas size. In the end I didn’t need to scale the images, however ImageMagick’s convert tool could be used for that task. The ouput frames will be named hello-n.png where n is the frame number.

Frames extracted are automatically numbered started from 0.

Deleting frames

To delete the first few frames, you could manually select the files in a file browser and delete them, or delete them one by one using the command line or run a bash for loop with a delete command, e.g.

for((i=0;i<6;i++))
do rm “hello-${i}.png”
done

Deleting the trailing frames can be performed in a similar manner:

for((i=137;i<214;i++))
do rm “hello-${i}.png”
done

Adding frames

To make the animation appear to pause at the end without immediately looping, I replicated the final desired frame a few times, by using the for loop again with the copy (cp) command:

for ((i=136;i<170;i++))
do
cp f135.png “f${i}.png”
done

Renumbering

Most animation software will consume the frames in lexicographical order, so 10 comes after 1 instead of 2. To feed the frames into an animation tool in the correct order, we need to pad the numbers with the appropriate number of zeros.

Frames with zero padded numbers have the same lexicographical and numerical ordering.

To renumber, rename and copy the frames into a new destination, I ran the following rather complicated looking composition of commands:

mkdir new_frames
for i in *.png
do
cp $i “new_frames/f$(echo $i |
awk ‘
{
split(substr($1,7),a,”.”);
printf “%0.03dn”, (a[1]-6);
}’)”
done

I’ve used a for loop again to iterate over the frames, a copy command, and then I’ve fed each file name into a scripting program called AWK, to produce a zero-padded version of the frame number.

“AWK is an interpreted programming language designed for text processing and typically used as a data extraction and reporting tool. It is a standard feature of most Unix-like operating systems.” — Wikipedia.

AWK is incredibly useful, but explaining how to write small programs in this language is unfortunately beyond the scope of this article.

Frames to GIF

Use convert to aggregate the frames into a GIF:

cd new_frames convert -delay 10 -loop 0 *.png hello.gif

I’ve used a delay of 10th of a second and looped the GIF forever with -loop 0.

GIF Optimisation

The final stage is to reduce the memory footprint of the GIF file by loading GIMP up and running Filters->Animation->Optimise(for GIF). This converts each frame into a difference of the previous frames, therefore saving space.

Conclusion

Linux and the command line is incredibly powerful; compositing different tools allows us to achieve complicated tasks, such as producing this GIF, often without any expense.

If you found this article useful/inspiring tap the 💚 so others can enjoy it, too.

Thanks for your time! Follow me on Twitter and LinkedIn.

--

--