From 867124d2fd77c058b6dfbd2954b657865e2a0c85 Mon Sep 17 00:00:00 2001 From: Urban Date: Thu, 13 Nov 2025 15:30:09 +0100 Subject: [PATCH] crontab working --- wcs-frame-match.sh | 63 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/wcs-frame-match.sh b/wcs-frame-match.sh index 6540a20..d13d7e5 100755 --- a/wcs-frame-match.sh +++ b/wcs-frame-match.sh @@ -24,7 +24,7 @@ ROI_GEOM="" # e.g., 800x200+50+100 usage() { cat <<'EOF' -Usage: wcs-frame-match.sh -s REF.png [options] [files.mp4...] +Usage: wcs-frame-match.sh -s REF.png [options] [files.mp4...] (supports WORKDIR via env) -s Path to reference image (PNG/JPG etc.) [required] -t Timestamp in seconds (float OK), default: 1.0 You can also provide a comma-separated list via --timestamps or --timestamp, e.g. 0.0,0.5,1.0 @@ -44,6 +44,8 @@ Usage: wcs-frame-match.sh -s REF.png [options] [files.mp4...] --black-pic-th FLOAT (default: 0.98) --black-min-d SEC (default: 0.05) -R ROI crop geometry on aligned images, e.g., 800x200+50+100 (focus only this region) + -W (env) WORKDIR: set working directory via environment (preferred for cron) + --workdir=/path or --workdir /path (inline alternative) -h Help Examples: @@ -55,6 +57,23 @@ Examples: EOF } +# --- WORKDIR bootstrap (cron-friendly) +# Use env WORKDIR or --workdir=/path or "--workdir /path" to change directory +PRE_WORKDIR="${WORKDIR:-}" +# Support --workdir=/path +for __arg in "$@"; do + case "$__arg" in + --workdir=*) PRE_WORKDIR="${__arg#--workdir=}" ;; + esac +done +# Support space-separated: --workdir /path (only if it's the first long arg form) +if [[ $# -ge 2 && "$1" == "--workdir" ]]; then + PRE_WORKDIR="$2" +fi +if [[ -n "$PRE_WORKDIR" ]]; then + cd "$PRE_WORKDIR" 2>/dev/null || { echo "Error: WORKDIR '$PRE_WORKDIR' not accessible" >&2; exit 1; } +fi + # ---- Arg parsing REF_IMAGE="" USER_SET_THRESHOLD="false" @@ -78,11 +97,9 @@ while getopts ":s:t:m:T:p:rkna:hdBR:R:-:" opt; do debug) DEBUG="true" ;; timestamps=*) TIMESTAMP_LIST="${OPTARG#timestamps=}" ;; timestamps) - # support space-separated: --timestamps 0,0.5,1 val="${!OPTIND}"; OPTIND=$((OPTIND+1)); TIMESTAMP_LIST="$val" ;; timestamp=*) TIMESTAMP_LIST="${OPTARG#timestamp=}" ;; timestamp) - # support space-separated: --timestamp 0,0.5,1 OR single value val="${!OPTIND}"; OPTIND=$((OPTIND+1)); TIMESTAMP_LIST="$val" ;; black-pic-th=*) BLACK_PIC_TH="${OPTARG#black-pic-th=}" ;; black-pic-th) @@ -90,6 +107,9 @@ while getopts ":s:t:m:T:p:rkna:hdBR:R:-:" opt; do black-min-d=*) BLACK_MIN_D="${OPTARG#black-min-d=}" ;; black-min-d) val="${!OPTIND}"; OPTIND=$((OPTIND+1)); BLACK_MIN_D="$val" ;; + workdir=*) PRE_WORKDIR="${OPTARG#workdir=}" ;; + workdir) + val="${!OPTIND}"; OPTIND=$((OPTIND+1)); PRE_WORKDIR="$val" ;; *) echo "Unknown long flag --$OPTARG" >&2; usage; exit 1 ;; esac ;; @@ -190,17 +210,46 @@ else REF_CROP="$REF_ALIGNED" fi -# Collect files +# Collect files (expand any globs AFTER WORKDIR cd) FILES=() if [[ "$RECURSIVE" == "true" ]]; then while IFS= read -r -d '' f; do FILES+=("$f"); done < <(find . -type f -iname "*.mp4" -print0) fi + +# Expand user-supplied patterns like *.mp4 relative to current (WORKDIR) now if [[ $# -gt 0 ]]; then - for f in "$@"; do FILES+=("$f"); done + shopt -s nullglob + tmp_expanded=() + for a in "$@"; do + # If argument contains glob chars, expand it now; otherwise keep literal + if [[ "$a" == *'*'* || "$a" == *'?'* || "$a" == *'['* ]]; then + matches=( $a ) + if (( ${#matches[@]} )); then + tmp_expanded+=("${matches[@]}") + else + # leave as-is if nothing matched (user might mean a literal) + tmp_expanded+=("$a") + fi + else + tmp_expanded+=("$a") + fi + done + shopt -u nullglob + for f in "${tmp_expanded[@]}"; do FILES+=("$f"); done +fi + +# If still empty and not recursive, fall back to current dir *.mp4 +if [[ ${#FILES[@]} -eq 0 ]]; then + shopt -s nullglob + default=(./*.mp4) + shopt -u nullglob + if (( ${#default[@]} )); then + FILES+=("${default[@]}") + fi fi if [[ ${#FILES[@]} -eq 0 ]]; then - echo "No MP4 files to process. Provide files or use -r." >&2 + echo "No MP4 files to process. Provide files, rely on default *.mp4 in WORKDIR, or use -r." >&2 exit 1 fi @@ -254,7 +303,7 @@ apply_roi_if_any() { black_end_time() { local mp4="$1" local out - out=$(ffmpeg -hide_banner -nostats -t 5 -i "$mp4" -vf "blackdetect=d=${BLACK_MIN_D}:pic_th=${BLACK_PIC_TH}" -an -f null - 2>&1 || true) + out=$(ffmpeg -hide_banner -loglevel error -nostats -t 5 -i "$mp4" -vf "blackdetect=d=${BLACK_MIN_D}:pic_th=${BLACK_PIC_TH}" -an -f null - 2>&1 || true) echo "$out" | awk -F'black_end:' '/black_end/ {gsub(/ .*/, "", $2); print $2; exit}' | awk '{print ($1+0)}' }