Skip to content

Instantly share code, notes, and snippets.

@marek-saji
Forked from tg123/typescripttogif
Last active August 29, 2015 14:00
Show Gist options
  • Save marek-saji/11012396 to your computer and use it in GitHub Desktop.
Save marek-saji/11012396 to your computer and use it in GitHub Desktop.
convert script(1) recordings to gif
#!/bin/bash
# Changelog
# ---------
#
# origin from https://gist.github.com/tg123/6240128
# modified by marek-saji:
# - use single loop
# - skip frames delayed by 0
# - loop animation with additional delay after every loop
# - use xwd(1) for much faster screen capturing
# - cleanup on non-successful exit
# - open gif, when ready
# - launch gnome-terminal(1) if $WINDOWID is empty
#
# origin from http://blog.fedora-fr.org/metal3d/post/typescript-to-gif
# modified by tgic to specified output file
# exit on first error
set -e
if [ -z "$WINDOWID" ]
then
test -z "$COLUMNS" && COLUMNS=$( tput cols 2>/dev/null )
test -z "$LINES" && LINES=$( tput lines 2>/dev/null )
exec gnome-terminal \
--geometry="${COLUMNS}x${LINES}" \
--working-directory="$PWD" \
--command="$0 $*"
fi
# cleanup on non-successful exit
trap 'test -d $WORKDIR && rm -rf $WORKDIR' INT TERM QUIT EXIT
# http://www.imagemagick.org/script/command-line-options.php#limit
CONVERTARG="-monitor -limit memory 32MiB -limit map 64MiB"
PAUSEBETWEENLOOPS=500
TIMING=$1
SCRIPT=$2
OUTPUT=$3
if [ ! -f "$TIMING" ] || [ ! -f "$SCRIPT" ] || [ "$OUTPUT" == "" ]
then
echo "usage: $0 TIMING SCRIPT OUTPUT"
exit
fi
WORKDIR=$(mktemp -d typescript-to-gif.XXXXXXXXXX)
WORKSCRIPT=$WORKDIR/typescript
cp $SCRIPT $WORKSCRIPT
#remove first line
sed -i '1d' $WORKSCRIPT
#clear screen
clear
sleep 1
curr=0
i=0
frames=
while read timing chars
do
timing=$( echo "$timing" | awk -F. '{ print $1 substr($2, 1, 2) }' )
dd if=$WORKSCRIPT bs=1 skip=$curr count=$chars 2>/dev/null
if [ "0" != "$timing" ]
then
#convert to gif frame with a nice frame-number
file=$WORKDIR/$i.xwd
xwd -id $WINDOWID > $file
frames=$frames" -delay $timing $file"
fi
#and move to next position
curr=$(( curr+chars ))
i=$(( i+1 ))
done < $TIMING
frames=$frames" -delay $PAUSEBETWEENLOOPS $file"
echo
echo 'Create gif file...'
convert $CONVERTARG $frames -loop 0 -coalesce -layers Optimize $OUTPUT
OPENER="$(
which xdg-open 2>/dev/null || # Linux
which open 2>/dev/null || # MacOSX
echo printf "gif is ready: %s\n"
)"
nohup $OPENER $OUTPUT >/dev/null 2>&1 &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment