2007年6月22日星期五

X11record - an X11 capture program which creates an animated gif



#!/bin/sh

# x11record - an X11 capture program which creates an animated gif
#
# USAGE:
#
# % x11record foo.gif # creates animation GIF
#
# REQUIREMENTS:
#
# * ImageMagick - to create animation GIF movies
# * xwd - to capture window's image
# * xwininfo - to get window's id
# * ungifsicle - to create GIFs
# * awk - for arithmetic
# * mktemp - for secure temporary files
#
# CREDIT: heavily based on the GPL-licensed 'x11rec'. I reimplemented this
# in /bin/sh so it wouldn't have a ruby dependency.
#
#
# Copyright (C) 2005 Benjamin Rutt < brutt@bloomington.in.us>
# All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of
# the GNU General Public License version 2.

if [ $# -ne 1 ]; then
echo "usage: `basename $0` <filename.gif>"
exit 1
fi

requireutil () {
while [ -n "$1" ]; do
type $1 >/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
echo Missing utility "$1". Please install it. >/dev/stderr
exit 1
fi
shift
done
}

requireutil grep mktemp xwininfo date awk convert xwd gifsicle

GIFOUT=$1
set -e
TD=`mktemp -d /tmp/x11record.XXXXXX`
set +e
echo "** Select an X window to record with a mouse. **"
ID=`xwininfo | grep "^xwininfo.*Window id:" | awk '{print $4}'`
echo "Window ID is $ID"
TSTART=`date +%s`
echo "** Type Ctrl+C to finish the recording. **"
FRAMES=0
function x11recfinish {
trap 2
TSTOP=`date +%s`
elapsed=`awk "BEGIN { print $TSTOP - $TSTART}"`
echo
echo "elapsed: $elapsed seconds"
if [ $elapsed -eq 0 ]; then
echo "ERROR: movie too short!"
exit 1
fi
if [ $FRAMES -eq 0 ]; then
echo "ERROR: no frames!"
exit 1
fi

N=0
for f in $TD/*.xwd; do
convert $f $f.gif
rm -f $f
N=`expr $N + 1`
awk "BEGIN { printf \"\rconverted ${N}/${FRAMES}\" }"
done
echo

DELAY=`awk "BEGIN { printf \"%d\", ($elapsed / $FRAMES) * 100}"`
gifsicle --colors 256 -O2 --delay $DELAY $TD/*.gif > $GIFOUT
rm -fr $TD
if [ $? -eq 0 ]; then
echo $GIFOUT created
exit 0
else
exit 1
fi
}

trap "x11recfinish" 2
echo
while true; do
xwd -silent -id $ID > $TD/$FRAMES
mv $TD/$FRAMES `awk "BEGIN { printf \"$TD/%08d.xwd\", $FRAMES}"` >/dev/null 2>&1
FRAMES=`expr $FRAMES + 1`
awk "BEGIN { printf \"\r${FRAMES} frames captured\" }"
done

没有评论: