Take Continuously Screenshots in a Certain Interval

Some time ago I took a lecture that was streamed on the Internet, but you would only get the slides, when you came to class in person. I didn’t get the logic in that and to save the time commuting to university I wrote a shell script that takes a screenshot every x seconds to capture the slides. It uses import from the imagemagick packet.

#!/bin/bash
no=$1  # number of screenshots as argument
sec=1  # interval of screenshots in seconds, also floats possible

sleep 5 # optional
s = 0
for i in $(seq $no); do
  sleep $sec
  s = $(($i*$sec))
  echo "$s seconds of $(($no*$sec))"
  import -window root image_$(seq -f %0011f $i $i | cut -d ',' -f 1).png
done

But now I had lots of screenshots and I only needed the distinct ones. compare compares two images and if their difference is under a certain threshold they are considered equal and only one of them is kept.

So the final script looks like this. It also crops the screenshot, which you might omit.

#!/bin/bash

no=$1         # number of screenshots as argument
fuzz=20       # which pixels are considered equal, in %
threshold=10  # threshold, under which images are considered equal
sec=1         # interval of screenshots, also floats possible
width=1024    # width of slide
heigth=768    # height of slide
offset=335    # offset from the left

sleep 5
s=0
date
for i in $(seq $no); do
  sleep $sec
  s=$(($i*$sec))
  echo "$s seconds of $(($no*$sec))"
  import -crop ${width}x${height}+${offset} -window root image_$(seq -f %0011f $i $i | cut -d ',' -f 1).png
done

date

i=1
while [ $i -le $no ]
  do
    j=$((i+1))
    while [ $j -le $no ]
      do
        i_=$(seq -f %0011f $i $i | cut -d ',' -f 1)
        j_=$(seq -f %0011f $j $j | cut -d ',' -f 1)
        if [ -f image_${i_}.png ] && [ -f image_${j_}.png ]
          then
            diff=$(compare -fuzz ${fuzz}% -metric ae image_${i_}.png image_${j_}.png diff.png 2>&1)

            echo "difference between $i_ and $j_: $diff"

            if [ $diff -lt $threshold ]
              then
                rm image_${j_}.png
              else
                i=$((j-1))
                break
            fi
        fi
        j=$((j+1))
    done
    i=$((i+1))
done

rm diff.png

i=0
for file in *.png
  do
    mv $file $(seq -f %0011f $i $i | cut -d ',' -f 1).png
    i=$(($i+1))
done

Comments