cctv/testdata/moonfire-nvr/movement_detection_via_api_...

65 lines
2.3 KiB
Bash

#! /bin/bash
main() {
local one_hour_ago_unix="$(($(date +%s) - 60 * 60))"
local one_hour_ago_ts="$(date -d @$one_hour_ago_unix -u +%Y-%m-%dT%H:%M:%SZ)"
for uuid in $(
api /api/ | jq -r .cameras[].uuid
); do
local stream=main
for recording in $(
api /api/cameras/$uuid/$stream/recordings \
| jq -c '.recordings[] | [.startId, .growing, .startTime90k]' \
| grep -v ,true, \
| jq -r .[0] \
| sort -n #-r
); do
api "/api/cameras/$uuid/$stream/view.mp4?s=$recording&ts=true" > /tmp/f.mp4
local ts="$(ffprobe /tmp/f.mp4 2>&1 | grep creation_time | tail -n 1 | awk '{print $NF}' | sed 's/\.000000Z/Z/')"
if [[ "$ts" > "$one_hour_ago_ts" ]]; then
echo "$ts > $one_hour_ago_ts, skipping"
continue
fi
echo uuid=$uuid stream=$stream recording=$recording ts=$ts
local fps=$(ffprobe -i /tmp/f.mp4 2>&1 | grep -o '[0-9]*\.*[0-9]* fps' | head -n 1 | awk '{print $1}')
local inspection="$(ffmpeg -i /tmp/f.mp4 -vf 'select=not(mod(n\,'${fps%.*}')),select=gte(scene\,0),metadata=print:file=-' -an -f null - 2> /dev/null | paste - -)"
local scores=($(echo "$inspection" | awk '{print $NF}' | sed 's/.*=//'))
local flattened_scores=()
for i in $(seq 1 ${#scores[@]}); do
flattened_scores+=($(echo ${scores[@]:i-2:3} | tr ' ' '\n' | sort -n | head -n 2 | tail -n 1))
done
local threshold=1
local last_n=(${flattened_scores[@]:0:7})
for i in $(seq 1 ${#flattened_scores[@]}); do
last_n[$(( (i-1)%7 ))]=${flattened_scores[$((i-1))]}
max_of_last_n=$(echo "${last_n[@]}" | tr ' ' '\n' | sort | tail -n 1)
if [[ "$max_of_last_n" < "$threshold" ]]; then
threshold="$max_of_last_n"
fi
done
threshold="$(echo "$threshold + 0.01" | bc)"
local max_flattened_score=$(echo "${flattened_scores[@]:5:${#flattened_scores[@]}-10}" | tr ' ' '\n' | sort -n | tail -n 1)
if ! [[ "${max_flattened_score#*.}" > "${threshold#*.}" ]]; then
echo nothing
fi
done
done
}
api() {
local path="$1"
shift
curl -sS "${CAMS_URL:-https://cams.inhome.blapointe.com}/${path#/}" "$@"
}
if [ "$0" == "$BASH_SOURCE" ]; then
main "$@"
fi