Recording animated gifs in ubuntu

Last week I wanted to do a small screen recording on my Ubuntu desktop and save it as an animated gif. I tried ScreenToGif but that didn’t work in Ubuntu, it threw an exception when I pressed the record button. I found a good solution in this askubuntu question and I’m building on top of it to make it as easy as right clicking on a video file.

Overview and software required

There are three basic steps in this process:

  • Make a video recording of the screen. You’ll need gtk-recordmydesktop to record the video. This is a quite user friendly desktop application.
  • Convert the video to JPEG files. You’ll need mplayer to convert the video to JPEG image files.
  • Convert the JPEG files into an animated GIF. You’ll need imagemagick to convert the JPEG images into a single animated GIF.

To install these tools, run:


sudo apt-get install imagemagick mplayer gtk-recordmydesktop

Record the video

No command line kung-fu is required for this one, which is something I can’t say for the next steps. Just start the RecordMyDesktop app (installed earlier with gtk-recordmydesktop) and record the video. It stores it in the ogv format.

Convert to JPEGs

In this part, we use mplayer to convert the video to JPEG images. From a command line:


mplayer -ao null <video file name> -vo jpeg:outdir=output

This command will convert the video into a series of JPEG image files and it will store them in a folder called output.

Convert to animated GIF

Here we convert the JPEG images into a single animated GIF file. Run the following command:


convert output/* output.gif

Optimizing the GIF

If you run the above, you’ll notice that the file size of the GIF may be a bit too large to be put on a web page.

There’s two things you can do:

  1. Animation Optimization
  2. Resize the source JPEG images in advance

Animation Optimization

ImageMagick has some built-in capabilities that go a long way optimizing the final file size of the GIF file. To optimize an animated GIF, try out this command:


convert output.gif -fuzz 10% -layers Optimize optimised.gif

If you want to read more about it:

Resize the source JPEG images

You can scale down the JPEG images without sacrificing too much readability. To convert a single JPEG file, you can use ImageMagick again:


convert in.jpg -resize -resize 440x330 out.jpg

Since we have a bunch of files, some bash scripting will be needed:

#!/bin/bash

# input folder
IN="$1"

# output folder
OUTPUT="$IN-resized-440w"

mkdir $OUTPUT
for i in $(find $IN -type f); do
	o=$OUTPUT/$(basename "$i")
	convert $i -resize 440x330 $o
done

This script takes a JPEG folder as input and creates a new folder with all images resized.

Putting it all together

I like Nautilus scripts and I think it would be great to just be able to right click on a video, “Convert to Animated GIF”, done (well it takes a while to do all the processing, but still).

The following is a rather naive version of such a script:

#!/bin/bash

# input file
VIDEO="$@"

# output file
OUTPUT="$@.gif"

# temporary folder
TMP=/tmp/convert-video-to-gif/

# temporary folder for jpegs
TMP_JPEG=$TMP/output/

TMP_UNOPTIMIZED_GIF=$TMP/unoptimized.gif

# create folder
rm -rf $TMP
mkdir $TMP

mplayer -ao null "$VIDEO" -vo jpeg:outdir=$TMP_JPEG
convert $TMP_JPEG/* $TMP_UNOPTIMIZED_GIF
convert $TMP_UNOPTIMIZED_GIF -fuzz 10% -layers Optimize $OUTPUT

zenity --info --text="Finished converting $VIDEO, result is at $OUTPUT"

Note that it uses zenity to display an info message when it’s all done.

This script will take the video my-video.ogv and convert it into an animated GIF named my-video.ogv.gif. But be aware:

  • it has no sanity checks
  • it won’t work with filenames that contain spaces
  • it doesn’t do the JPEG resizing part

Next time I’ll need an animated GIF, I might get around to writing it a bit better.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s