~voidraven/s4

ref: 5c00f37d8928d021150d95d3adc666f8384fe1b6 s4/.s4/watch.sh -rwxr-xr-x 4.0 KiB
5c00f37d — lotus addressed more shellcheck issues 1 year, 8 months ago
                                                                                
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/sh
#
# template used to create each camera+computer watch file.


# ------ Begin Global vars --------------------------------------
# ID is the camera number (0-n)
ID=XXX

# We expect /dev/video0 to be the webcam
WEBCAM="/dev/video0"

# Example valid resolutions: 640x480, 1280x720, or 1920x1080
REZ=YYY

# Your networked storage location
STORAGE=ZZZ

# Framerate you want to record at
# 30 or 60 depending on what your cameras support
FRAMERATE=WWW

# Where the local currently recording video is residing.
# When the video is done recording, we copy it over
CACHE_DIR="$HOME/.s4/cache"

# log file location
S4_LOG_DIR="$HOME/.s4/s4.log"

# FFmpeg arguments:
FF_ARG_STD="-hide_banner -nostats -loglevel fatal"
FF_ARG_LOG="-loglevel warning"
#FF_ARG_DBG=""
FF_FORMAT="avi"

# Logging disabled by default
# ( to enable change to "true" )
S4_LOG_ENABLED="NO-S4-LOGGING"
# ------ End Global vars ---------------------------------


# ------ Begin functions ---------------------------------
# flip the status files over to reflect 'True'
fun_set_true () {
    echo "false" > "$STORAGE/s4/cameras/$ID/stop.status"
}


# flip the status files over to reflect 'False'
fun_set_false () {
    echo "true" > "$STORAGE/s4/cameras/$ID/stop.status"
}


# Kicks off recording work
# args: 1
# 1: filename to save video as (e.g. 22-2)
fun_record () {
    fun_set_true
    L_VIDNAME=$1
    # -y flag means always overwrite
    ffmpeg -y $FF_ARG_STD -framerate $FRAMERATE -video_size $REZ -i $WEBCAM "$CACHE_DIR/$L_VIDNAME.$FF_FORMAT" &
}


# Stop the camera and copy over the completed file
# args: 1
# 1: # file to be copied (f.t.b.c) over to the main storage
fun_stop () {
    L_FTBC=$1
    fun_set_false
    kill $(pidof ffmpeg)
    if ! [ -z "$L_FTBC" ] ; then
	cp $CACHE_DIR/$L_FTBC.$FF_FORMAT $STORAGE/s4/cameras/$ID/$L_FTBC.$FF_FORMAT &
    fi
}


# Checks if our status files have been modified
fun_check () {
    RUNSTAT=$(cat "$STORAGE/s4/cameras/$ID/run.status")
    STOPSTAT=$(cat "$STORAGE/s4/cameras/$ID/stop.status")
    TEN=$(date "+%M")
    TEN_CHECK=$(echo "${TEN%"${TEN#?}"}")
    HOUR_CHECK=$(date +"%H")
    if [ $RUNSTAT = "true" ] && [ $STOPSTAT = "false" ] ; then
	fun_record "$HOUR_CHECK-$TEN_CHECK"
    elif [ $RUNSTAT = "false" ] && [ $STOPSTAT = "true" ] ; then
	fun_stop
    else
	echo "Error State: run & stop status files out of sync.."
    fi
}
# ------ End functions ---------------------------------


# was the script called recursively?
if [ "$#" -eq 1 ] && [ "$1" = "check" ] ; then
    # something must have been changed.. lets find out what.
    fun_check
    exit 0
fi


if [ -d $STORAGE/s4 ] ; then
    # where is this script running from?
    THIS_PWD=$(dirname $0)
    # watch the status file for changes, if it changes, re-run this script..
    LAST_STATUS=$(cat $STORAGE/s4/cameras/$ID/run.status)

    while true ; do
	sleep 1
	GRAB_STATUS=$(cat $STORAGE/s4/cameras/$ID/run.status)
	if [ "$GRAB_STATUS" != "$LAST_STATUS" ] ; then
	    LAST_STATUS=$GRAB_STATUS
	    $THIS_PWD/watch$ID.sh check & # makes a recursive background call
	fi
    done & # background the loop

    # Lets start keeping track of the time!
    CUR_HOUR=$(date +"%H")
    # get the minutes; e.g. for 13:27, grab 27
    CUR_MIN=$(date "+%M")
    # get the tens position; e.g. for 27, grab 2
    CUR_MIN_CHECK=$(echo "${CUR_MIN%"${CUR_MIN#?}"}")
    while true ; do
	sleep 1
	TEN=$(date "+%M")
	TEN_CHECK=$(echo "${TEN%"${TEN#?}"}")
	HOUR_CHECK=$(date +"%H")
	# has the hour changed?
	if ! [ $HOUR_CHECK = $CUR_HOUR ] ; then
	    fun_stop "$CUR_HOUR-5" # we can always assume the minutes position
	    CUR_HOUR=$HOUR_CHECK
	    CUR_MIN_CHECK=0
	    sleep 5 # make sure we had enough time to stop recording / kick off the copy
	    fun_record "$CUR_HOUR-0"
	# has 10 minutes elapsed?
	elif ! [ $TEN_CHECK = $CUR_MIN_CHECK ] ; then
	    fun_stop "$CUR_HOUR-$CUR_MIN_CHECK"
	    CUR_MIN_CHECK=$TEN_CHECK
	    sleep 5
	    fun_record "$CUR_HOUR-$CUR_MIN_CHECK"
	fi
    done
    
else
    echo "Storage not initialized (or mounted?), quitting.."
    exit 1
fi

exit 0