/
record_byday.sh
308 lines (252 loc) · 8.83 KB
/
record_byday.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env bash
####################################################################
# BigBrother CCTV Recording & Live Viewing (mirroring) software #
# Copyright 2016-2017 Andrew Wood #
# #
# record_byday.sh Bourne shell script to perform recording for #
# each camera. Launched by bigbrotherd #
# #
# www.simple.org/bigbrother #
# #
# Licensed under the GNU Public License v 3 #
# The full license can be read at www.gnu.org/licenses/gpl-3.0.txt #
# and is included in the License.txt file included with this #
# software. #
# #
# BigBrother is free open source software but if you find it #
# useful please consider making a donation to the Communications #
# Museum Trust at www.comms.org.uk/donate #
####################################################################
usagestring="Syntax error. Usage: $0 -cam proto://camera/url:port -name CameraName -folder /path/to/ -log /path/to/log/file -cmd /path/to/ffmpeg -container TypeCode"
copyrightstring="Simple BigBrother Copyright Andrew Wood 2016-2017"
echoUsage()
{
echo " "
echo $usagestring
echo " "
echo $copyrightstring
echo " "
}
processSIGINT()
{
echo "Got SIGINT"
echo "$0 Got SIGINT forwarding to OS PID $pid" | $bblogger $logfile
keepGoing=0
kill -INT $pid
}
processSIGTERM()
{
echo "Got SIGTERM"
echo "$0 Got SIGTERM forwarding to OS PID $pid" | $bblogger $logfile
keepGoing=0
kill -TERM $pid
}
processSIGQUIT()
{
echo "Got SIGQUIT"
echo "$0 Got SIGQUIT forwarding to OS PID $pid" | $bblogger $logfile
keepGoing=0
kill -QUIT $pid
}
processSIGHUP()
{
echo "Got SIGHUP"
echo "$0 Got SIGHUP forwarding SIGINT to OS PID $pid" | $bblogger $logfile
kill -INT $pid
}
deleteOldFilesInTodaysFolder()
{
#$1=camname $2=todaysdate
#Delete any files for this cam name which do not contain todays date
for filename in $(ls $folder/byday/$day/$1*)
do
echo "Checking if we should delete $filename" | $bblogger $logfile
found=$(echo "$filename" | grep -c -E $2)
if [ $found -eq 0 ]
then
echo "Deleting $filename" | $bblogger $logfile
rm -f $filename
fi
done
}
if [ $# -ne 12 ]
then
echoUsage
echo "$0 started with incorrect number of arguments, cannot continue" | $bblogger $logfile
exit 1
fi
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
sourceurl="" #safety default init
camname="" #safety default init
folder="" #safety default init
logfile="/dev/null" #safety default init
ffmpegcommand="" #default init
bblogger="$SCRIPTPATH/bblogger"
container="" #default safety init
parseParams()
{
if [ $1 == '-cam' ] && [ $2 != '-cam' ] && [ $2 != '-name' ] && [ $2 != '-folder' ] && [ $2 != '-log' ] && [ $2 != '-cmd' ] && [ $2 != '-container' ]
then
sourceurl=$2
return 0
elif [ $1 == '-name' ] && [ $2 != '-cam' ] && [ $2 != '-name' ] && [ $2 != '-folder' ] && [ $2 != '-log' ] && [ $2 != '-cmd' ] && [ $2 != '-container' ]
then
camname=$2
return 0
elif [ $1 == '-folder' ] && [ $2 != '-cam' ] && [ $2 != '-name' ] && [ $2 != '-folder' ] && [ $2 != '-log' ] && [ $2 != '-cmd' ] && [ $2 != '-container' ]
then
folder=$2
return 0
elif [ $1 == '-log' ] && [ $2 != '-cam' ] && [ $2 != '-name' ] && [ $2 != '-folder' ] && [ $2 != '-log' ] && [ $2 != '-cmd' ] && [ $2 != '-container' ]
then
logfile=$2
return 0
elif [ $1 == '-cmd' ] && [ $2 != '-cam' ] && [ $2 != '-name' ] && [ $2 != '-folder' ] && [ $2 != '-log' ] && [ $2 != '-cmd' ] && [ $2 != '-container' ]
then
ffmpegcommand=$2
return 0
elif [ $1 == '-container' ] && [ $2 != '-cam' ] && [ $2 != '-name' ] && [ $2 != '-folder' ] && [ $2 != '-group' ] && [ $2 != '-log' ] && [ $2 != '-cmd' ] && [ $2 != '-container' ]
then
container=$2
return 0
fi
#invalid params
return 1
}
#read params and if sane set the corresponding vars
parseParams $1 $2
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue" | $bblogger $logfile
exit 1
fi
parseParams $3 $4
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue" | $bblogger $logfile
exit 1
fi
parseParams $5 $6
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue" | $bblogger $logfile
exit 1
fi
parseParams $7 $8
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue" | $bblogger $logfile
exit 1
fi
parseParams $9 ${10}
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue" | $bblogger $logfile
exit 1
fi
parseParams ${11} ${12}
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue" | $bblogger $logfile
exit 1
fi
#check all params have been initialised, they are not still the default 0
if [ $sourceurl == "" ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue" | $bblogger $logfile
exit 1
fi
if [ $camname == "" ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue" | $bblogger $logfile
exit 1
fi
if [ $folder == "" ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue" | $bblogger $logfile
exit 1
fi
if [ $logfile == "/dev/null" ]
then
echoUsage
exit 1
fi
if [ $ffmpegcommand == "" ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue" | $bblogger $logfile
exit 1
fi
if [ $container == "" ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue" | $bblogger $logfile
exit 1
fi
#because $container is used as filename ext we have to ensure it does not contain invalid chars
containervalidchars=$(echo "$group" | grep -c -E '\.|\/|\*|\||>|<|\s')
if [ $containervalidchars -ne 0 ]
then
echo "Error: Container Format $container must not contain slashes,dots,asterisks,pipes,angle brackets or spaces"
echoUsage
exit 1
fi
#all params ok
previousDayValue="NoDay" #dont match any day at startup
pid=0 # dont match anything at startup
trap processSIGINT INT
trap processSIGTERM TERM
trap processSIGQUIT QUIT
trap processSIGHUP HUP
keepGoing=1;
echo "$0 $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} started" | $bblogger $logfile
while [ $keepGoing -ne 0 ]
do
pid=0 #temp reset to nothing to reduce risk of PID recycling problems
timenow=$(date +%H%M) #gives HHMM - avoid colon in time to allow Windows compat with filenames
day=$(date +%A) #gives Monday Tuesday etc
todaysdate=$(date +%Y-%m-%d) #gives YYYY-MM-DD
minspast=$(date +%M)
if [ "$previousDayValue" != $day ]
then
echo "Day has changed or this is first run, deleting any old files for $camname on $day"
echo "Day has changed or this is first run, deleteting any old files for $camname on $day" | $bblogger $logfile
deleteOldFilesInTodaysFolder $camname $todaysdate
previousDayValue=$day
fi
if [ $minspast -eq 00 ]
then
secs=3600
else
sixty=60
mins2run=$(expr $sixty \- $minspast)
secs=$(expr $mins2run \* $sixty)
fi
echo "It is $minspast minutes past the hour, recording for $secs seconds to align with next hour"
#check if $folder/byday/$day/$camname--$todaysdate--$timenow.$container exits
#if so it could be because clock has gone back for DST, or HUP has been sent > once in last min, in which case append -1 -2 etc afer time
dstadjustment=""
dstadjustmentno=0
while [ -f $folder/byday/$day/$camname--$todaysdate--$timenow$dstadjustment.$container ]
do
dstadjustmentno=$(expr $dstadjustmentno \+ 1)
dstadjustment="-"$dstadjustmentno
echo "$0 has detected clock may have gone back or HUP signalled, testing for presence of file $folder/byday/$day/$camname--$todaysdate--$timenow$dstadjustment.$container so as not to overwrite previous file" | $bblogger $logfile
done
$ffmpegcommand -y -loglevel fatal -t $secs -i $sourceurl -acodec copy -vcodec copy -metadata title="$camname--$todaysdate--$timenow" $folder/byday/$day/$camname--$todaysdate--$timenow$dstadjustment.$container &
pid=$!
echo "$0 started ffmpeg OS PID $pid" | $bblogger $logfile
wait $pid
echo "$0 woke up because ffmpeg OS PID $pid exited or a signal was received" | $bblogger $logfile
done
exit 0