-
Notifications
You must be signed in to change notification settings - Fork 0
/
gif-anim-cap-frame
executable file
·102 lines (94 loc) · 3.04 KB
/
gif-anim-cap-frame
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
SETTINGS="$SCRIPTPATH/gif-anim-settings"
. "$SETTINGS" || {
echo "Error running '$SETTINGS' to get settings" >&2;
exit 1;
}
full_frame_path () {
local idx="$1"
printf "$ga_dir_frames/%03d.$ga_frame_ext" "$idx"
}
lock_obtain() {
while [ -e "$ga_framecap_lock_fn" ]; do
# If the lockfile exists, print warning
echo "Lockfile $ga_framecap_lock_fn exists" >&2
# Read the PID
lock_pid=$(cat "$ga_framecap_lock_fn")
# Check if the PID in the lockfile is still running
if ps -p $lock_pid > /dev/null; then
echo "Waiting for lock to be released by PID $lock_pid..." >&2
# Count number of HALF SECONDS that have passed
half_seconds_passed=0
# Wait for the lock to be released, checking every 0.5 seconds
while [ -e "$ga_framecap_lock_fn" ] && ps -p $lock_pid > /dev/null; do
sleep 0.5
((half_seconds_passed++))
# If the lock is not released within $ga_waitlock_secs, kill the PID
if [ $half_seconds_passed -ge $((2*$ga_waitlock_secs)) ]; then
echo "Lock not released, killing PID $lock_pid" >&2
kill $lock_pid || true
break
fi
done
else
echo "Process $lock_pid not running, removing lock" >&2
rm -f "$ga_framecap_lock_fn"
fi
done
# Once the lockfile is gone or removed, create a new one with the current PID and set a trap to remove it when the script exits.
echo $$ > "$ga_framecap_lock_fn"
trap "rm -f $ga_framecap_lock_fn" EXIT
}
# Make dir again since user might have removed frames dir
# (Like, they selected a position with gif-anim-select-area,
# which creates the dir and stores the coordinates, but
# then maybe the user removes the frames/ folder
# and begins capturing frames. So we have to ensure the
# frames/ folder exists.)
mkdir -p "$ga_dir_frames"
# Get prior frame (get filename; strip extension)
idx=$(cd "$ga_dir_frames" && ls *.$ga_frame_ext | tail -1 | sed -e 's/\..*$//' 2>/dev/null)
echo "IDX: $idx"
if [[ -n "$idx" ]]; then
idx=${idx%.*}
while true; do
idx=$((idx+1))
tmppath=$(full_frame_path "$idx")
if [[ ! -f $tmppath ]]; then # Testing if somehow the last file from ls | tail exists
break
fi
done
else
idx=0
fi
# New frame filename:
fn_frame=$(full_frame_path "$idx")
echo " Frame file: $fn_frame"
coords=$(<$ga_fn_coords)
echo "Coordinates: $coords"
if [ -z "$WAYLAND_DISPLAY" ]; then
# Coords are x11 WxH+x+y
# scrot wants x,y,w,h
w=${coords%x*}
h=${coords#*x}; h=${h%%+*}
x=${coords#*+}; x=${x%+*}
y=${coords##*+}
echo "scrot coords (x,y,w,h): $x,$y,$w,$h"
scrot_co="$x,$y,$w,$h"
echo "Sending off our child to go capture some pixels to bring back home"
echo " to the family..."
echo " Running: scrot -k -a '$scrot_co' '$fn_frame'"
scrot -k -a "$scrot_co" "$fn_frame"
else
echo "Sending off our child to go capture some pixels to bring back home"
echo " to the family..."
echo "Running: grim -g '$coords' '$fn_frame'"
grim -g "$coords" "$fn_frame"
fi
if [[ ! -f "$fn_frame" ]]; then
echo "We tried to create frame but somehow couldn't: $fn_frame" >&2
else
echo "Frame captured: $fn_frame"
fi