Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Record terminal session to GIF
#!/bin/bash
TIMING=$1
SCRIPT=$2
W=$WINDOWID
rm -rf /tmp/script-replay-gifs/
mkdir /tmp/script-replay-gifs/
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 /tmp/script-replay-gifs/$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 /tmp/script-replay-gifs/ | head -n $i | tail -n 1)
timing=$(echo "$timing*100" | bc -l | awk '{print int($0)}')
command=$command" -delay $timing /tmp/script-replay-gifs/$file"
i=$((i+1))
done < $TIMING
convert $command /tmp/anim-notoptim.gif
convert /tmp/anim-notoptim.gif -coalesce -layers Optimize /tmp/anim.gif
rm -f /tmp/anim-notoptim.gif
rm -rf /tmp/script-replay-gifs/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.