-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpmcam.sh
executable file
·54 lines (47 loc) · 1.46 KB
/
pmcam.sh
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
#!/usr/bin/env bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
OUTPUT_DIR="$SCRIPT_DIR/images"
CAPTURE_INTERVAL="1" # in seconds
FFMPEG=ffmpeg
command -v $FFMPEG >/dev/null 2>&1 || { FFMPEG=avconv ; }
DIFF_RESULT_FILE=$OUTPUT_DIR/diff_results.txt
fn_cleanup() {
rm -f diff.png $DIFF_RESULT_FILE
}
fn_terminate_script() {
fn_cleanup
echo "SIGINT caught."
exit 0
}
trap 'fn_terminate_script' SIGINT
mkdir -p $OUTPUT_DIR
PREVIOUS_FILENAME=""
while true ; do
FILENAME="$OUTPUT_DIR/$(date +"%Y%m%dT%H%M%S").jpg"
echo "-----------------------------------------"
echo "Capturing $FILENAME"
if [[ "$OSTYPE" == "linux-gnu" ]]; then
$FFMPEG -loglevel fatal -f video4linux2 -i /dev/video0 -r 1 -t 0.0001 $FILENAME
elif [[ "$OSTYPE" == "darwin"* ]]; then
# Mac OSX
$FFMPEG -loglevel fatal -f avfoundation -i "default" -r 1 -t 0.0001 $FILENAME
fi
if [[ "$PREVIOUS_FILENAME" != "" ]]; then
# For some reason, `compare` outputs the result to stderr so
# it's not possibly to directly get the result. It needs to be
# redirected to a temp file first.
compare -fuzz 20% -metric ae $PREVIOUS_FILENAME $FILENAME diff.png 2> $DIFF_RESULT_FILE
DIFF="$(cat $DIFF_RESULT_FILE)"
fn_cleanup
if [ "$DIFF" -lt 20 ]; then
echo "Same as previous image: delete (Diff = $DIFF)"
rm -f $FILENAME
else
echo "Different image: keep (Diff = $DIFF)"
PREVIOUS_FILENAME="$FILENAME"
fi
else
PREVIOUS_FILENAME="$FILENAME"
fi
sleep $CAPTURE_INTERVAL
done