From ddef839399d80aa456b2c9bb1914fffdf015d8be Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 7 Sep 2024 13:55:21 -0600 Subject: [PATCH] o that just werks --- .../movement_detection_via_api_ffmpeg.sh | 116 ++++++++---------- 1 file changed, 50 insertions(+), 66 deletions(-) diff --git a/testdata/sentryshot.d/movement_detection_via_api_ffmpeg.sh b/testdata/sentryshot.d/movement_detection_via_api_ffmpeg.sh index dba3f80..2ab0860 100644 --- a/testdata/sentryshot.d/movement_detection_via_api_ffmpeg.sh +++ b/testdata/sentryshot.d/movement_detection_via_api_ffmpeg.sh @@ -3,81 +3,65 @@ set -eo pipefail main() { - local nonrecent_unix="$(($(_date +%s) - 4 * 60 * 60))" # 4h ago - local nonrecent_ts="$(_date -d @$nonrecent_unix -u +%Y-%m-%dT%H:%M:%SZ)" + local nonrecent_unix="$(($(_date +%s) - 10 * 60))" # 10m ago + local nonrecent_ts="$(_date -d @$nonrecent_unix -u +%Y-%m-%d_%H-%M-%S)" - for uuid in $( - api /api/ | jq -r .cameras[].uuid + for recording in $( + api "/api/recording/query?recording-id=9999-12-28_23-59-59_x&limit=10000&reverse=false&include-data=false" \ + | jq -r .[].id \ + | sort ); do - local stream=main - local recordings=($( - api /api/cameras/$uuid/$stream/recordings \ - | jq -c '.recordings[] | [.startId, .growing, .startTime90k]' \ - | grep -v ,true, \ - | jq -r .[0] \ - | sort -n #-r - )) - for recording in "${recordings[@]}"; do - if grep "^$uuid.$recording$" ${MOONFIRE_DIR:-/tmp}/movement_detection_via_api_ffmpeg.sh.txt; then - continue - fi + if [[ "$recording" > "$nonrecent_ts" ]]; then + echo "$ts > $nonrecent_ts, so skipping" + continue + fi - local ts="$(_date -u -d @"$( - api "/api/cameras/$uuid/$stream/view.mp4.txt?s=$recording&ts=true" \ - | grep -A 1 last_modified: \ - | head -n 2 \ - | tail -n 1 \ - | awk '{print $NF}' \ - | tr -d ',' - )" +%Y-%m-%dT%H:%M:%SZ )" - if [ -z "$ts" ] || [[ "$ts" > "$nonrecent_ts" ]]; then - echo "$ts > $nonrecent_ts, so skipping" - continue - fi + if grep "^$recording$" ${SENTRYSHOT_DIR:-/tmp}/movement_detection_via_api_ffmpeg.sh.txt; then + continue + fi - echo uuid=$uuid stream=$stream recording=$recording ts=$ts - api "/api/cameras/$uuid/$stream/view.mp4?s=$recording&ts=true" > /tmp/movement_detection_via_api_ffmpeg.sh.mp4 + echo recording=$recording >&2 + api "/api/recording/video/$recording" > /tmp/movement_detection_via_api_ffmpeg.sh.mp4 - 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}') - 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 - -)" - local scores=($(echo "$inspection" | awk '{print $NF}' | sed 's/.*=//')) + 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}') + 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 - -)" + 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 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="$(_perl -e "print $threshold + 0.01")" - - local has_change=false - if [ "${#flattened_scores[@]}" -lt 10 ]; then - has_change=true - else - 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 - - if $has_change; then - echo "$uuid.$recording" >> ${MOONFIRE_DIR:-/tmp}/movement_detection_via_api_ffmpeg.sh.txt - else - echo no movement in "$recording" - api "/api/cameras/$uuid/$stream/view.mp4" \ - -X DELETE \ - -H 'Content-Type: application/json' \ - -d "$(printf '{"runStartId": %s}' "$recording")" + 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="$(_perl -e "print $threshold + 0.005")" + + local has_change=false + if [ "${#flattened_scores[@]}" -lt 10 ]; then + echo too short >&2 + has_change=true + else + local max_flattened_score=$(echo "${flattened_scores[@]:5:${#flattened_scores[@]}-10}" | tr ' ' '\n' | sort -n | tail -n 1) + echo "${max_flattened_score#*.} > ${threshold#*.}" + if [[ "${max_flattened_score#*.}" > "${threshold#*.}" ]]; then + has_change=true + fi + fi + + if $has_change; then + echo "$recording" >> ${SENTRYSHOT_DIR:-/tmp}/movement_detection_via_api_ffmpeg.sh.txt + else + echo no movement in "$recording" + api "/api/recording/delete/$recording" \ + -X DELETE + fi done }