blob: e407f51bf432a7f104f27d74a86c0fe14218f605 (
plain)
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
|
#!/bin/bash
set -e
DIR="$( cd "$( dirname "$(realpath "${BASH_SOURCE[0]}")" )" &>/dev/null && pwd )"
PROGNAME="$0"
. "$DIR/lib.bash"
curl_opts="-s --connect-timeout 5 --retry 5 --max-time 10 --retry-delay 0 --retry-max-time 40"
allow_multiple=
config_file="$HOME/.config/ipcam_motion_worker/config.txt"
declare -A config=()
usage() {
cat <<EOF
usage: $PROGNAME OPTIONS
Options:
-c|--config FILE configuration file, default is $config_file
-v, -vx be verbose.
-v enables debug logs.
-vx does \`set -x\`, may be used to debug the script.
--allow-multiple don't check for another instance
EOF
exit 1
}
get_recordings_dir() {
curl $curl_opts "${config[api_url]}/api/camera/list" \
| jq ".response.\"${config[camera]}\".recordings_path" | tr -d '"'
}
# returns two words per line:
# filename filesize
get_recordings_list() {
curl $curl_opts "${config[api_url]}/api/recordings/${config[camera]}?filter=motion" \
| jq '.response.files[] | [.name, .size] | join(" ")' | tr -d '"'
}
report_failure() {
local file="$1"
local message="$2"
local response=$(curl $curl_opts -X POST "${config[api_url]}/api/motion/fail/${config[camera]}" \
-F "filename=$file" \
-F "message=$message")
print_response_error "$response" "report_failure"
}
report_timecodes() {
local file="$1"
local timecodes="$2"
local response=$(curl $curl_opts -X POST "${config[api_url]}/api/motion/done/${config[camera]}" \
-F "filename=$file" \
-F "timecodes=$timecodes")
print_response_error "$response" "report_timecodes"
}
print_response_error() {
local resp="$1"
local sufx="$2"
local error="$(echo "$resp" | jq '.error')"
local message
if [ "$error" != "null" ]; then
message="$(echo "$resp" | jq '.message' | tr -d '"')"
error="$(echo "$error" | tr -d '"')"
echoerr "$sufx: $error ($message)"
fi
}
get_roi_file() {
if [ -n "${config[roi_file]}" ]; then
file="${config[roi_file]}"
if ! [[ "$file" =~ ^/.* ]]; then
file="$(dirname "$config_file")/$file"
fi
debug "get_roi_file: detected file $file"
[ -f "$file" ] || die "invalid roi_file: $file: no such file"
echo "$file"
fi
}
process_local() {
local recdir="$(get_recordings_dir)"
local tc
local words
local file
while read line; do
words=($line)
file=${words[0]}
debug "processing $file..."
tc=$(do_motion "${recdir}/$file")
debug "$file: timecodes=$tc"
report_timecodes "$file" "$tc"
done < <(get_recordings_list)
}
process_remote() {
local tc
local url
local words
local file
local size
pushd "${config[fs_root]}" >/dev/null || die "failed to change to ${config[fs_root]}"
touch tmp || die "directory '${config[fs_root]}' is not writable"
rm tmp
[ -f "video.mp4" ] && {
echowarn "video.mp4 already exists in ${config[fs_root]}, removing.."
rm "video.mp4"
}
while read line; do
words=($line)
file=${words[0]}
size=${words[1]}
if (( size > config[fs_max_filesize] )); then
echoerr "won't download $file, size exceedes fs_max_filesize ($size > ${config[fs_max_filesize]})"
report_failure "$file" "too large file"
continue
fi
url="${config[api_url]}/api/recordings/${config[camera]}/download/${file}"
debug "downloading $url..."
if ! download "$url" "video.mp4"; then
echoerr "failed to download $file"
report_failure "$file" "download error"
continue
fi
tc=$(do_motion "video.mp4")
debug "$file: timecodes=$tc"
report_timecodes "$file" "$tc"
rm "video.mp4"
done < <(get_recordings_list)
popd >/dev/null
}
do_motion() {
local input="$1"
local roi_file="$(get_roi_file)"
local tc
local timecodes=()
time_start
if [ -z "$roi_file" ]; then
timecodes+=($(do_dvr_scan "$input"))
else
echoinfo "using roi sets from file: ${BOLD}$roi_file"
while read line; do
if ! [[ "$line" =~ ^#.* ]]; then
tc="$(do_dvr_scan "$input" "$line")"
if [ -n "$tc" ]; then
timecodes+=("$tc")
fi
fi
done < <(cat "$roi_file")
fi
debug "do_motion: finished in $(time_elapsed)s"
timecodes="$(echo "${timecodes[@]}" | sed 's/ */ /g' | xargs)"
timecodes="${timecodes// /,}"
echo "$timecodes"
}
dvr_scan() {
"${config[dvr_scan_path]}" "$@"
}
do_dvr_scan() {
local input="$1"
local args=
if [ ! -z "$2" ]; then
args="-roi $2"
echoinfo "dvr_scan(${BOLD}${input}${RST}${CYAN}): roi=($2), mt=${config[threshold]}"
else
echoinfo "dvr_scan(${BOLD}${input}${RST}${CYAN}): no roi, mt=${config[threshold]}"
fi
dvr_scan -q -i "$input" -so \
--min-event-length ${config[min_event_length]} \
-df ${config[downscale_factor]} \
--frame-skip ${config[frame_skip]} \
-t ${config[threshold]} $args | tail -1
}
[[ $# -lt 1 ]] && usage
while [[ $# -gt 0 ]]; do
case $1 in
-c|--config)
config_file="$2"
shift; shift
;;
--allow-multiple)
allow_multiple=1
shift
;;
-v)
VERBOSE=1
shift
;;
-vx)
VERBOSE=1
set -x
shift
;;
*)
die "unrecognized argument '$1'"
exit 1
;;
esac
done
if [ -z "$allow_multiple" ] && pidof -o %PPID -x "$(basename "${BASH_SOURCE[0]}")" >/dev/null; then
die "process already running"
fi
read_config "$config_file" config
check_config config "api_url camera"
if [ -n "${config[remote]}" ]; then
check_config config "fs_root fs_max_filesize"
fi
[ -z "${config[threshold]}" ] && config[threshold]=1
[ -z "${conifg[min_event_length]}" ] && config[min_event_length]="3s"
[ -z "${conifg[frame_skip]}" ] && config[frame_skip]=2
[ -z "${conifg[downscale_factor]}" ] && config[downscale_factor]=3
[ -z "${conifg[dvr_scan_path]}" ] && config[dvr_scan_path]="dvr-scan"
if [ -z "${config[remote]}" ]; then
process_local
else
process_remote
fi
|