Monday, 14 November 2016

Script to convert script command output to animated GIF

While looking around for some nice terminal recording tools for the Linux terminal I came across a neat script on github which could take in the timing information & command output files from the popular script command & convert it to a neat GIF file.

The script has two dependencies:

  1. It runs only from within a terminal opened under x-windows direct console GUI.
  2. It requires ImageMagick-c++ & ImageMagick to be installed.


Here is the script:


#!/bin/bash

# origin from http://blog.fedora-fr.org/metal3d/post/typescript-to-gif
# modified by tgic to specified output file


# http://www.imagemagick.org/script/command-line-options.php#limit
CONVERTARG="-limit memory 32MiB -limit map 64MiB"

TIMING=$1
SCRIPT=$2
OUTPUT=$3
W=$WINDOWID

if [ ! -f "$TIMING" ] || [ ! -f "$SCRIPT" ] || [ "$OUTPUT" == "" ]
then
    echo "usage: $0 TIMING SCRIPT OUTPUT"
    exit
fi

if [ "$W" == "" ]
then
    echo 'No window found use xvtypescripttogif instead'
    exit
fi

WORKDIR=$(mktemp -d)

t=$(mktemp)
cp $SCRIPT $t

#remove first line
sed -i '1d' $t

#clear screen
clear

#read timing file one by one
curr=0
i=0
while read line
do
    #capture time and chars to read
    cols=($line)
    chars=${cols[1]}

    #read from current char the number of chars to read
    dd if=$t bs=1 skip=$curr count=$chars 2>/dev/null

    #convert to gif frame with a nice frame-number
    n=$(printf "%010d" $i)
    import -window $WINDOWID $WORKDIR/$n.gif

    #and move to next position
    curr=$((curr+chars))
    i=$((i+1))
done <$TIMING
rm -f $t

#now, set gif with delay per frame
i=1
while read line
do
    cols=($line)
    timing=${cols[0]}
    #get next image
    file=$(ls -1 $WORKDIR | head -n $i | tail -n 1)
    timing=$(echo "$timing*100" | bc -l | awk '{print int($0)}')
    command=$command" -delay $timing $WORKDIR/$file"

    i=$((i+1))
done < $TIMING


convert $CONVERTARG $command $WORKDIR/anim-notoptim.gif
convert $CONVERTARG $WORKDIR/anim-notoptim.gif -coalesce -layers Optimize $OUTPUT

rm -rf $WORKDIR


To use it, first run the script command as follows:

[root@cserver ~]# script -t 2> time.txt cmd.txt
Script started, file is cmd.txt
[root@cserver ~]# echo "lets see how this goes"
lets see how this goes
[root@cserver ~]# echo "testing the typescript"
testing the typescript
[root@cserver ~]# exit
exit
Script done, file is cmd.txt


Now supply the time.txt & cmd.txt files as arguments to the conversion script & specify out.gif as the output file.

./typeconv.sh time.txt cmd.txt out.gif

This will replay the commands & create an out.gif file.

Here is the out.gif file created a result of the execution of the script:


Note that the resulting gif does not execute in a infinite loop. It runs only once.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...