From 8af32661ebe1066edfcfd66dd3264650f2e9b083 Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 7 Sep 2024 15:29:01 -0600 Subject: [PATCH] thatll do --- .../movement_detection_via_api_ffmpeg.sh | 70 ++++++++++--------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/testdata/sentryshot.d/movement_detection_via_api_ffmpeg.sh b/testdata/sentryshot.d/movement_detection_via_api_ffmpeg.sh index 2ab0860..09cdb71 100644 --- a/testdata/sentryshot.d/movement_detection_via_api_ffmpeg.sh +++ b/testdata/sentryshot.d/movement_detection_via_api_ffmpeg.sh @@ -23,39 +23,7 @@ main() { 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 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.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 + if video_has_movement; then echo "$recording" >> ${SENTRYSHOT_DIR:-/tmp}/movement_detection_via_api_ffmpeg.sh.txt else echo no movement in "$recording" @@ -65,6 +33,42 @@ main() { done } +video_has_movement() { + 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 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 "has movement? $max_flattened_score > $threshold" >&2 + if awk "BEGIN { exit !($max_flattened_score > $threshold)}"; then + has_change=true + fi + fi + + $has_change +} + api() { local path="$1" shift