From e27af38bd7c695c4a1c32f5bdd95eb37fbf7f674 Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 1 Sep 2024 11:15:49 -0600 Subject: [PATCH] wip --- .../.movement_detection_via_api_ffmpeg.sh.swp | Bin 0 -> 12288 bytes ...movement_detection_to_rclone_via_ffmpeg.sh | 57 ++++++++++++++++ .../movement_detection_via_api_ffmpeg.sh | 64 ++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 testdata/moonfire-nvr/.movement_detection_via_api_ffmpeg.sh.swp create mode 100644 testdata/moonfire-nvr/movement_detection_to_rclone_via_ffmpeg.sh create mode 100644 testdata/moonfire-nvr/movement_detection_via_api_ffmpeg.sh diff --git a/testdata/moonfire-nvr/.movement_detection_via_api_ffmpeg.sh.swp b/testdata/moonfire-nvr/.movement_detection_via_api_ffmpeg.sh.swp new file mode 100644 index 0000000000000000000000000000000000000000..5a24411f586a97ca0b472e2457f336695d9ef668 GIT binary patch literal 12288 zcmeI2O^@3|7{{l)wY-#9apN$XO`2pkwv!ZSo8o0lfwoc?s&0$KW~1QPp$EJ;kO5@-ybKvn6ZIuf4~uiQOKLkM&kkfK1&Q5XF`L=wz1<0 zW*N3EMuuQqIF_&sKMVuci!k|0ytgaP#UDx#^wxjd@17=i@ z6#+#cAutrKcKk3o+HBP2{c>T6zVh;A1xFE31QY>9KoL*`6ahs*5l{pkAp#;gKt4yj zk4<7;pPUcOohP^IqzEViihv@Z2q*%IfFhs>C<2OrBA^H;0uLbp%OT{eCkeUv6r9KZ z|JNrm{PzMOcc6cu8_*|E7utYI&}+~W(5>eQ`3@RD7og+N67(wc%V9!(hTerls46ht5N5kO>u_+fNhn1GEd3pj(G92Xq7a2J)c+ z^!GtRzJ$Jju0cM;ARFpIThJM34Z4H4|AlTtzd=7i35$ntEtSiPfFhs>JR$^Kk5Ec| z%L{0sxLzQhOWU}r7pQ6C{LZP(v)#=PFJCxQz@lEvFJAGuq~hKUeFJHruPO`6ncSRx*hssqqrAYqQ7j63+a@w#rvKsq|rfW zTLUVen}uSdK=7VgrAqfEQ#xV5WG@`KOWdVVBoE{Vv-=?pLeckv9g?*ov?bb>(e3%x zwXQp`gkS-4y4((9w!EUlp*8tb^kc^R;lN4TI{R^FmQpVl7%_{LWNsLlGtb-Woz@$z z;@K$K=x$YP~JE1uS=lkc^Ls*F7_HcQ4Isit<%VlCE@dFG<=Yt30jQIy$~j z_Vk(M$n0s(WKzzlMd4@m#|wj=&tkKZPtA0Lq$V<-3q>#BQTh_9%u8$c3N7zNF)9Ns ze(?S{NqOebGB@-U(=f`_oQtW&sR5bu=?kn>^Qa;G$Z&N(dYx(`7Z%t6^Oz_B6+d(; z!FH{T)rojXUm3$}ZSs6aunMA*BcR zk`+$r<<>Wk&PJHYE6u|585m_{a-P?0c5UnR zYjTF#>@zvWre!_zgtt40ysdL##p0Y(qxw!9?qWr7m9$*~rxW!nopRog4EjDXPpxNz zWWC6m)3Q17d9v}-{FA>bWP6@LHW?h9#5_UW?&vaktkV^I XR54CQl>Q!BUZO_N3yhwH_vilyuRZ)_ literal 0 HcmV?d00001 diff --git a/testdata/moonfire-nvr/movement_detection_to_rclone_via_ffmpeg.sh b/testdata/moonfire-nvr/movement_detection_to_rclone_via_ffmpeg.sh new file mode 100644 index 0000000..8f92666 --- /dev/null +++ b/testdata/moonfire-nvr/movement_detection_to_rclone_via_ffmpeg.sh @@ -0,0 +1,57 @@ +#! /bin/bash +disabled="false" +threshold="0.040" +vdir=/tmp/tmpfs +exec > >(tee "$vdir/backup.log") 2>&1 +if ! [ -d "$vdir" ]; then + echo $vdir does not exist >&2 + exit 1 +fi +if ps aux | \ + grep -v grep | \ + grep -E 'services.backups.rclone|local.ffprobe'; then + echo skipping colliding run >&2 + exit 0 +fi +echo ok at 1 +files=($(find $vdir -type f -name "*.m*")) +if [ "${#files[@]}" == "0" ]; then + echo no files found >&2 + exit 0 +fi +echo files=${files[@]} +includes=() +printf "reviewing"; printf " %s\n" "${files[@]}" +echo ok at 2 +for file in "${files[@]}"; do + frames="$( + nice -n 19 /var/services/homes/squeaky2x3/ffmpeg-local/ffprobe \ + -threads 1 -loglevel error -hide_banner -show_frames -of compact=p=0 \ + -f lavfi "movie=$file,select=gt(scene\\,$threshold)" 2> /dev/null \ + )" + ret=$? + if [ $ret -eq 0 ] && [ $(printf "%s\n" "$frames" | wc -l) -lt 2 ]; then + echo file has no movement $file >&2 + rm -f "$file" + elif [ $ret -ne 0 ]; then + echo failed scanning $file >&2 + files=("${files[@]/"$file"}") + else + echo file has movement $file + includes+=("$(basename "$file")") + fi +done +echo "pushing files ${includes[@]}" +if [ "${#includes[@]}" -gt 0 ]; then + if [ "$disabled" == "true" ] || \ + /volume1/homes/squeaky2x3/services/backups/rclone \ + --config /volume1/homes/squeaky2x3/services/backups/rclone.conf \ + copy \ + $vdir/ \ + blapointe-drive:/projects/cctv/ut/ \ + $(printf " --include %s" "${includes[@]}") ; then + rm -f "${files[@]}" + fi +fi +echo donezo +echo why diff --git a/testdata/moonfire-nvr/movement_detection_via_api_ffmpeg.sh b/testdata/moonfire-nvr/movement_detection_via_api_ffmpeg.sh new file mode 100644 index 0000000..8546bac --- /dev/null +++ b/testdata/moonfire-nvr/movement_detection_via_api_ffmpeg.sh @@ -0,0 +1,64 @@ +#! /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