on hold because i destroyed db oops

master
bel 2024-09-03 14:01:58 -06:00
parent 00097d3357
commit 5b6ca6b534
1 changed files with 71 additions and 30 deletions

View File

@ -1,61 +1,94 @@
#! /bin/bash #! /bin/bash
main() { main() {
local nonrecent_unix="$(($(date +%s) - 15 * 60))" local nonrecent_unix="$(($(_date +%s) - 24 * 60 * 60))" # 1d ago
local nonrecent_ts="$(date -d @$nonrecent_unix -u +%Y-%m-%dT%H:%M:%SZ)" local nonrecent_ts="$(_date -d @$nonrecent_unix -u +%Y-%m-%dT%H:%M:%SZ)"
for uuid in $( for uuid in $(
api /api/ | jq -r .cameras[].uuid api /api/ | jq -r .cameras[].uuid
); do ); do
local stream=main local streams=(main sub)
for recording in $( local recordings=($(
api /api/cameras/$uuid/$stream/recordings \ api /api/cameras/$uuid/$streams/recordings \
| jq -c '.recordings[] | [.startId, .growing, .startTime90k]' \ | jq -c '.recordings[] | [.startId, .growing, .startTime90k]' \
| grep -v ,true, \ | grep -v ,true, \
| jq -r .[0] \ | jq -r .[0] \
| sort -n #-r | sort -n #-r
); do ))
local ts="$(date -u -d @"$( for recording_idx in $(seq 1 ${#recordings[@]}); do
api "/api/cameras/$uuid/$stream/view.mp4.txt?s=$recording&ts=true" \ local recording=${recordings[recording_idx-1]}
if grep "^$uuid.$recording$" /tmp/movement_detection_via_api_ffmpeg.sh.txt; then
continue
fi
local ts="$(_date -u -d @"$(
api "/api/cameras/$uuid/$streams/view.mp4.txt?s=$recording&ts=true" \
| grep -A 1 last_modified: \ | grep -A 1 last_modified: \
| head -n 2 \ | head -n 2 \
| tail -n 1 \ | tail -n 1 \
| awk '{print $NF}' \ | awk '{print $NF}' \
| tr -d ',' | tr -d ','
)" +%Y-%m-%dT%H:%M:%SZ )" )" +%Y-%m-%dT%H:%M:%SZ )"
if [[ "$ts" > "$nonrecent_ts" ]]; then if [ -z "$ts" ] || [[ "$ts" > "$nonrecent_ts" ]]; then
echo "$ts > $nonrecent_ts, so skipping" echo "$ts > $nonrecent_ts, so skipping"
continue continue
fi fi
echo uuid=$uuid stream=$stream recording=$recording ts=$ts echo uuid=$uuid stream=$streams recording=$recording ts=$ts
api "/api/cameras/$uuid/$stream/view.mp4?s=$recording&ts=true" > /tmp/f.mp4 api "/api/cameras/$uuid/$streams/view.mp4?s=$recording&ts=true" > /tmp/movement_detection_via_api_ffmpeg.sh.mp4
local fps=$(ffprobe -i /tmp/f.mp4 2>&1 | grep -o '[0-9]*\.*[0-9]* fps' | head -n 1 | awk '{print $1}') local has_change=false
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 cksum="$(cksum /tmp/movement_detection_via_api_ffmpeg.sh.mp4)"
local scores=($(echo "$inspection" | awk '{print $NF}' | sed 's/.*=//')) if grep "^$cksum$" /tmp/movement_detection_via_api_ffmpeg.sh.txt; then
has_change=true
fi
local flattened_scores=() if ! $has_change; then
for i in $(seq 1 ${#scores[@]}); do local fps=$(ffprobe -i /tmp/movement_detection_via_api_ffmpeg.sh.mp4 2>&1 | grep -o '[0-9]*\.*[0-9]* fps' | head -n 1 | awk '{print $1}')
flattened_scores+=($(echo ${scores[@]:i-2:3} | tr ' ' '\n' | sort -n | head -n 2 | tail -n 1)) local inspection="$(ffmpeg -i /tmp/movement_detection_via_api_ffmpeg.sh.mp4 -vf 'select=not(mod(n\,'${fps%.*}')),select=gte(scene\,0),metadata=print:file=-' -an -f null - 2> /dev/null | paste - -)"
done local scores=($(echo "$inspection" | awk '{print $NF}' | sed 's/.*=//'))
local threshold=1 local flattened_scores=()
local last_n=(${flattened_scores[@]:0:7}) for i in $(seq 1 ${#scores[@]}); do
for i in $(seq 1 ${#flattened_scores[@]}); do flattened_scores+=($(echo ${scores[@]:i-2:3} | tr ' ' '\n' | sort -n | head -n 2 | tail -n 1))
last_n[$(( (i-1)%7 ))]=${flattened_scores[$((i-1))]} done
max_of_last_n=$(echo "${last_n[@]}" | tr ' ' '\n' | sort | tail -n 1)
if [[ "$max_of_last_n" < "$threshold" ]]; then local threshold=1
threshold="$max_of_last_n" 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
has_change=true
fi fi
done fi
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 $has_change; then
if ! [[ "${max_flattened_score#*.}" > "${threshold#*.}" ]]; then echo "$uuid.$recording" >> /tmp/movement_detection_via_api_ffmpeg.sh.txt
echo nothing echo "$cksum" >> /tmp/movement_detection_via_api_ffmpeg.sh.txt
else
local next_recording=$(( ${recordings[recording_idx]} - 1 ))
local span="$recording"
if [ "$recording" != "$next_recording" ]; then
span="$recording-$next_recording"
fi
echo no movement in "$span"
for stream in ${streams[@]}; do
api "/api/cameras/$uuid/$stream/view.mp4" \
-X DELETE \
-H 'Content-Type: application/json' \
-d "$(printf '{"s": "%s"}' "$span")"
done
fi fi
done done
break
done done
} }
@ -65,6 +98,14 @@ api() {
curl -sS "${CAMS_URL:-https://cams.inhome.blapointe.com}/${path#/}" "$@" curl -sS "${CAMS_URL:-https://cams.inhome.blapointe.com}/${path#/}" "$@"
} }
_date() {
if which gdate &> /dev/null; then
gdate "$@"
else
date "$@"
fi
}
if [ "$0" == "$BASH_SOURCE" ]; then if [ "$0" == "$BASH_SOURCE" ]; then
main "$@" main "$@"
fi fi