Make a Screen-Capture Movie

Create animated screen-capture movies as the ultimate aid in a presentation or tutorial.

If a picture is worth a thousand words, then how about a thousand pictures—or a graphical animation? Getting your message across depends a lot on the tools you have at hand, the environment you’ll be presenting to, and the audience. It’s one thing to stand in front of a classroom with a blackboard, a projector, and a room full of PCs, but it’s quite another to describe a set of ideas to multiple people standing in front of a kiosk, in a hall filled with competing noises and distractions. Wouldn’t it be great to create an animated presentation complete with screenshots and textual notes without first taking a course in multimedia and purchasing expensive development applications? Of course it would!

Let’s set the stage: the objective is to create a screen-shot movie that demonstrates how to use a feature in an application. In addition, you should be able to provide commentary within the movie to explain what’s going on.

Ideally, you can make a movie with tools that don’t take long to learn and use. The technique demonstrated here shows how to capture screenshots in rapid succession. These screenshots are then converted into a single file that can be read by nothing more complicated than a browser.

The Tools

For this hack you’ll need the following tools:

  • The ImageMagick command-line utilities to create, edit, and convert images from one format to another

  • xwininfo, the window information utility for X

  • A small bash script to record multiple screen captures

How to Use the Tools

Launch the application you want to record. Use xwininfo to obtain the target window’s hexadecimal ID number. When you run the command, your cursor changes to a crosshair—just click on the window whose information you want to capture. In this example, the ID number, 0x140001d, comes from the fifth line in the output below:

	$ xwininfo 

	xwininfo: Please select the window about which you
              would like information by clicking the
			  mouse in that window.
    xwininfo: Window id: 0x140001d "making movies.sxw - OpenOffice.org 1.1.0 " 
      Absolute upper-left X: 4 
	  Absolute upper-left Y: 18 
	  Relative upper-left X: 0 
	  Relative upper-left Y: 0 
	  Width: 920
	  Height: 630
	  Depth: 16
	  Visual Class: TrueColor
	  Border width: 0 
	  Class: InputOutput 
	  Colormap: 0x1400001 (installed) 
	  Bit Gravity State: ForgetGravity 
	  Window Gravity State: StaticGravity 
	  Backing Store State: NotUseful 
	  Save Under State: no 
	  Map State: IsViewable 
	  Override Redirect State: no 
	  Corners: +4+18 -100+18 -100-120 +4-120 
	  -geometry 920x630+4+18

Use the ID number with the import command to capture the window. If this succeeds, you’ll hear two beeps from the PC’s speaker. If you want a frame around the screencapture, add the -frame switch. By the way, the import command can capture screens to any file format, but the MIFF format is very fast:

	$ import -window 0x140001d openoffice.miff

View the screencapture with display:

	$ display openoffice.miff

If you prefer a different format, use convert to, well, convert the image:

	$ convert openoffice.miff openoffice.png

Now that you know how to take a single screenshot, here’s a simple bash script to collect multiple screencaptures for use in your movie. It takes two command-line arguments: the window ID and the number of shots to capture:

	#!/bin/sh
	# A simple bash script to screen capture
	#
	# Supply two arguments, the window id and number of captures

	let x=1

	# loop until it has captured the number of captures requested
	while [ "$x" -le "$2" ]
	do
      import -window $1 "capture$x.miff"
	  # uncomment the line below
	  # if you want more time in between screen captures
    # sleep 2s
      let x+=1
    done

Invoking the script is straightforward. Make it executable, then type:

	$ ./capture.sh w_idno_capt

w_id is the hexadecimal ID obtained from xwininfo, and no_capt is the number of screenshots to record. Every two seconds the capture script will “snap” a screenshot and label it as capture.miff, with a number that indicates the order in which the images were taken. You can, of course, modify the script to capture in other image formats (replace the extension of capture$x.miff with the extension of your desired format) or to increase the time delay between snapshots (uncomment # sleep 2s and increase the value to the desired number or seconds).

Use animate to display a slideshow of your captured images:

	$animate -delay 20 *.miff

The delay number controls how much time to wait between displays of the individual screencaptures. The units are in hundredths of a second.

Finally, converting the animated images to a more convenient, single-file format is accomplished by using the convert utility. There are several likely formats:

  • MNG is a license-free, multi-image file format similar to PNG, but with more bells and whistles. This format is not yet widely used, but it is very neat, and there are plug-ins for all the major browsers. The convert command is:

    	$ convert -delay 20 *.miff capture.mng
  • GIF, you should know.

    	$ convert -delay 20 *.miff capture.gif
  • MPEG encoding requires you to download and compile the mpeg2encode utility source code, but it does allow you to add sound.

    	$ convert -delay 20 *.miff capture.mpg

Enhancing a Screencapture

It’s not always enough to replicate a series of keystrokes and pop-up menus. Sometimes you need details that help better explain what’s going on. For that matter, it’s nice to make a plain screen look fantastic with all sorts of graphical special effects. That’s where mogrify comes in.

	$ mogrify -fill blue -pointsize 25 -draw 'text 10,20 "Hello World" ' \		
	capture1.miff

This command adds the phrase “Hello World” to the capture1.miff image. The words will be colored blue, with a point size of 25. The words are placed relative to the top left corner of the image in terms of x (10 pixels to the right) and y (20 pixels down) coordinates.

The montage command makes its additions on a copy of the original. Remember to use the -geometry switch with the current window size (for example, -geometry 920x630); otherwise, the copy will have a size of 120 x 120 pixels:

	$ montage -fill black -pointsize 50 -draw 'text 100,300 \

Drawing a box behind the words will make them stand out:

	$ montage -fill yellow -draw 'Rectangle 80,250 400,400' -fill black \
				 -pointsize 20 -draw 'text 100,300"@instruction1.txt"' \
	-geometry 920x630 capture1.miff capture1a.miff

Remember that you read the switches from left to right. Why? Because each set of options can be changed by the next option to its right. For example, you won’t be able to see the words in the following example because the yellow box covers them since drawing text precedes drawing the colored box:

	$ montage -fill black -pointsize 20 -draw 'text 100,300"@instruction1.txt"' \
	-fill yellow -draw 'Rectangle 80,250 400,400' -geometry 920x630 \
	capture1.miff capture1a.miff

Tip

Did you notice the @instruction1.txt? The @ token instructs the utility to place the contents of a text file, instruction1.txt in this case, into the image.

Screen Capture Tips

The best way to figure out what looks good is by experimentation. Here are a few things I’ve learned that may save you some time:

  • The more instructions and options on the import command, the longer the capture will take.

  • Do the screen captures using the MIFF file format. Capturing to any other format can slow down the capture process. You can always convert the images to another format at a later time.

  • The screenshot capture rate depends on the window size.

  • Listen to the beeps; this will give you an idea how quickly or slowly you should navigate through your window.

  • Use the root flag to capture the entire screen of your desktop:

    	import -window root capture.miff
  • Identify and rehearse the steps you want to record. This is known as a making a storyboard. The screen-capturing process will save every action, good and bad, so practice the steps before invoking the bash script.

  • Inserting a sleep command inside the bash script can give you much-needed time to prepare the application for the next screenshot without feeling rushed.

  • Reviewing each shot after capture helps identify the good and bad images. Simplify this tedious operation by using display to load and edit an entire series of images with one command.

  • You may have noticed that listing files with numbers doesn’t always sort the way you want. Here’s a shell trick to ensure that the files are never out of sequence:

    	display capture?.miff capture??.miff
  • Clicking once on a displayed image will put the ImageMagick program into editing mode, and clicking the image again will make the menu disappear. The editing tools have many of the same options that are available on the command line. Pressing the space bar advances to the next image.

  • Improve the flow of your presentation by adjusting the speed and duration of the frame rates of your animation. You can do this while converting the individual images.

  • Add comments and special effects to the images only after you’ve established their respective animation frame rates. Make a backup of your animation first.

You should exercise discretion when choosing your file format. I experimented with GIFs, MPEGs, and MNGs. For example, the resulting uncompressed GIF was 14 MB. (ImageMagick binaries do not use the proprietary compression algorithm unless you enable that option and recompile.) The MPEG was 4.6 MB. The MNG was the smallest at 500 K. The fancy screen-capture demo was substantially larger, with the GIF at 26 MB, the MPEG at 6.7 MB, and the MNG again the smallest at 4.7 MB. However, it was interesting to note that converting the fancy movie MNG to PCX files and then converting it back reduced the size to 1.6 MB, 65% smaller.

The MNG format is clearly superior to GIF, but it is not very well supported, and takes quite a long time to decompress before it’s ready to play. Mozilla’s MNG plug-in failed to bring up the small demo. The ImageMagick utility animate worked fine. KDE’s Konqueror could only run the small demo, where an HTML tag embedded the MNG file with the <src> tag. The GIF version of the demos played well in animate, Mozilla, and Konqueror.

Another factor to consider is that file formats such as GIF and MNG don’t really stream, so players need to load the entire file into RAM before you can see it. Large graphics may consume too much RAM, crashing your application. One easy solution is to enlarge the virtual memory by using a swap file. This issue may also come up when using convert.

MPEG is a good choice when resources are at a premium and it’s not possible to add a swap file. You may need to experiment with the convert options to prevent color loss, though.

animate is a good option because the presentation is so exact and easy to control. Viewing GIF format guarantees that any browser can read the file quickly. Beware, though—both formats are RAM hungry.

Conclusion

ImageMagick is a very sophisticated graphics-manipulation package. This hack has covered only its barest capabilities. Anybody who decides to use it as a development platform can increase his productivity by using scripts. ImageMagick has an API with a complete set of language bindings for over 16 languages, including Perl, Java, C, C++, and Cold Fusion.

Robert Bernier

Get Linux Multimedia Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.