Files
minute-screenshot-linux/screenshot.sh
T

86 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/bash
DIR="/home/immortaly007/screenshot-log"
FORMAT="png"
SIMILARITY_DELETE_TRESHOLD=100
HTML_THUMB_WIDTH=200
SCALE="75%"
# Max time (in seconds) to wait before forcing a screenshot even if spectacle is running
SPECTACLE_BLOCKED_TIMEOUT=600 # 10 minutes
SPECTACLE_BLOCKED_FILE="$HOME/.cache/screenshot-spectacle-blocked"
DAY=`date +%Y-%m-%d`
TIME=`date +%H.%M.%S.%3N`
mkdir -p $DIR/$DAY
SCR_NEW="$DIR/$DAY/$TIME.$FORMAT"
SCR_LAST=`ls -t $DIR/$DAY/*.png | head -n1`
# do a screenshot
if [ "$XDG_CURRENT_DESKTOP" = "KDE" ]
then
if pgrep -x spectacle > /dev/null; then
# Spectacle is running; check if we've been blocked for too long
if [ -f "$SPECTACLE_BLOCKED_FILE" ]; then
BLOCKED_SINCE=`cat "$SPECTACLE_BLOCKED_FILE"`
NOW=`date +%s`
BLOCKED_FOR=$(( NOW - BLOCKED_SINCE ))
if [ "$BLOCKED_FOR" -ge "$SPECTACLE_BLOCKED_TIMEOUT" ]; then
echo "`date`: Spectacle has been blocking screenshot for ${BLOCKED_FOR}s (>= ${SPECTACLE_BLOCKED_TIMEOUT}s); forcing screenshot"
else
echo "`date`: Spectacle is already running (blocked for ${BLOCKED_FOR}s); skipping screenshot"
exit 0
fi
else
# First time we're being blocked; record the timestamp
date +%s > "$SPECTACLE_BLOCKED_FILE"
echo "`date`: Spectacle is already running; skipping screenshot (recording block time)"
exit 0
fi
fi
spectacle -bn -o $SCR_NEW
else
import -window root $SCR_NEW
fi
#requires ImageMagick
DISTANCE=`compare -metric MAE $SCR_LAST $SCR_NEW NULL: 2>&1`
echo $DISTANCE
# ImageMagick outputs something like "21.8357 (0.218357)"; we only want to use the first number for the comparison below, so we use cut to get just that
DISTANCE=`echo $DISTANCE | cut -d " " -f1 -`
if [ "$DISTANCE" = "inf" ]
then
DISTANCE=1000
fi
echo $DISTANCE between $SCR_LAST and $SCR_NEW
IS_SIMILAR=`echo "$DISTANCE < $SIMILARITY_DELETE_TRESHOLD" | bc`
# Clean up the blocked-file now that a screenshot succeeded
rm -f "$SPECTACLE_BLOCKED_FILE"
if [ "$IS_SIMILAR" = "1" ]
then
rm $SCR_NEW
echo "`date`: Distance = $DISTANCE; too similar; RM $SCR_NEW"
# echo "`date` RM" >> $DIR/$DAY/log-.log
else
# If cwebp is available, convert the new screenshot to webp format and delete the original png
if command -v cwebp > /dev/null; then
WIDTH=$(identify -format "%w" $SCR_LAST)
TARGET_WIDTH=$(( WIDTH * ${SCALE%%%} / 100 ))
echo "Converting $SCR_LAST to webp format with target width $TARGET_WIDTH"
# I tried some different cwebp options (-preset text/picture), -m 6), but default seems to give the best results especially given that it's fast.
cwebp -quiet -resize $TARGET_WIDTH 0 $SCR_LAST -o "${SCR_LAST%.png}.webp"
rm $SCR_LAST
SCR_LAST="${SCR_LAST%.png}.webp"
else
# If cwebp is not available, we can still reduce the size of the png by resizing it with mogrify
mogrify -scale $SCALE $SCR_LAST
fi
# Ensure that the new file is still seen as the most recent one
FUTURE=`date +%Y%m%d%H%M.%S -d "+1 second"`
touch -t $FUTURE "$SCR_NEW"
fi