diff --git a/entrypoint.sh b/entrypoint.sh index 562bdcc..f68bedc 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,47 +1,94 @@ #! /bin/bash +SEG="${SEG:-300}" + function main() { if [ "$#" -eq 0 ]; then - echo "USAGE: $0 ip ip ip..." >&2 + log "USAGE: $0 ip ip ip..." >&2 return 1 fi - function clean() { kill -9 $(jobs -p) } - trap clean EXIT ERR - mkdirs & + if [ "$(hostname)" == "Scratch" ]; then + trap clean EXIT ERR + fi + watchman "$@" & for ip in "$@"; do - echo recording from $ip... + log recording from $ip... record "$ip" & done for i in "$@"; do if ! wait -n 1; then - echo "Something died: $?" >&2 + log "Something died: $?" exit 1 fi exit 0 done } +function log() { + echo "$(date): $@" >&2 +} + function mkdirs() { + mkdir -p "${OUT_DIR:-/tmp/ffmpeg_cap}/$(date +%Y)/$(date +%m)/$(date +%d)" +} + +function watchman() { + log watchman starting on $@ + local max_last=$SEG + max_last=$((max_last*3)) + function finder() { + local suffix="${1:-"*"}" + find "${OUT_DIR:-/tmp/ffmpeg_cap}" -type f -name "*_${suffix}.m*" + } + function mark_last_file() { + local now="$(date +%s)" + local suffix="$1" + local min=$((max_last-1)) + for f in $(finder "$suffix"); do + local ts="$(stat -c '%X' "$f")" + if ((now-tsmax_last)); then + log "no new videos in $seconds seconds for $suffix, panicking" + exit 1 + fi + done + until check_disk; do + local f="$(finder | sort | head -n 1)" + log "high disk usage, pruning $f" + rm -f "$f" + done done } function record() { - while true; do - echo "starting record for $@" - _record "$@" - sleep 10 - done + log "starting record for $@" + _record "$@" + tail -n 50 /tmp/ffmpegs.log } function _record() { - local out="${OUT_DIR:-/tmp/ffmpeg_cap}/%Y/%m/%d/%H-%M-%S_$1.mp4" + local out="${OUT_DIR:-/tmp/ffmpeg_cap}/%Y/%m/%d/%H-%M-%S_${1}.mp4" + mkdirs ffmpeg \ -threads 0 \ -nostdin \ @@ -52,7 +99,7 @@ function _record() { -map 0 \ -force_key_frames "expr:gte(t,n_forced*9)" \ -f segment \ - -segment_time ${SEG:-300} \ + -segment_time $SEG \ -segment_format mp4 \ -strftime 1 \ -minrate .05k \ diff --git a/testdata/ffmpeg/ffmpegs.log b/testdata/ffmpeg/ffmpegs.log old mode 100644 new mode 100755 diff --git a/testdata/ffmpeg/log b/testdata/ffmpeg/log old mode 100644 new mode 100755 diff --git a/testdata/ffmpeg/run.sh b/testdata/ffmpeg/run.sh old mode 100644 new mode 100755 diff --git a/testdata/motion/cameras/wyze0.conf b/testdata/motion/cameras/wyze0.conf old mode 100644 new mode 100755 diff --git a/testdata/motion/cameras/wyze1.conf b/testdata/motion/cameras/wyze1.conf old mode 100644 new mode 100755 diff --git a/testdata/motion/cameras/wyze2.conf b/testdata/motion/cameras/wyze2.conf old mode 100644 new mode 100755 diff --git a/testdata/motion/cameras/wyze3.conf b/testdata/motion/cameras/wyze3.conf old mode 100644 new mode 100755 diff --git a/testdata/motion/motion.conf b/testdata/motion/motion.conf old mode 100644 new mode 100755 diff --git a/testdata/motion/run.sh b/testdata/motion/run.sh old mode 100644 new mode 100755 diff --git a/testdata/openrtsp/install.sh b/testdata/openrtsp/install.sh old mode 100644 new mode 100755 diff --git a/testdata/openrtsp/run.sh b/testdata/openrtsp/run.sh old mode 100644 new mode 100755