Hack 12. 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 picturesor 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.
1.13.1. The Tools
For this hack you'll need the following tools:
1.13.2. 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 crosshairjust 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:
1.13.3. 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
1.13.4. 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:
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, thoughboth formats are RAM hungry.
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.