crontab working

This commit is contained in:
2025-11-13 15:30:09 +01:00
parent 3234a19be0
commit 867124d2fd

View File

@ -24,7 +24,7 @@ ROI_GEOM="" # e.g., 800x200+50+100
usage() { usage() {
cat <<'EOF' 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] -s Path to reference image (PNG/JPG etc.) [required]
-t Timestamp in seconds (float OK), default: 1.0 -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 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-pic-th FLOAT (default: 0.98)
--black-min-d SEC (default: 0.05) --black-min-d SEC (default: 0.05)
-R ROI crop geometry on aligned images, e.g., 800x200+50+100 (focus only this region) -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 -h Help
Examples: Examples:
@ -55,6 +57,23 @@ Examples:
EOF 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 # ---- Arg parsing
REF_IMAGE="" REF_IMAGE=""
USER_SET_THRESHOLD="false" USER_SET_THRESHOLD="false"
@ -78,11 +97,9 @@ while getopts ":s:t:m:T:p:rkna:hdBR:R:-:" opt; do
debug) DEBUG="true" ;; debug) DEBUG="true" ;;
timestamps=*) TIMESTAMP_LIST="${OPTARG#timestamps=}" ;; timestamps=*) TIMESTAMP_LIST="${OPTARG#timestamps=}" ;;
timestamps) timestamps)
# support space-separated: --timestamps 0,0.5,1
val="${!OPTIND}"; OPTIND=$((OPTIND+1)); TIMESTAMP_LIST="$val" ;; val="${!OPTIND}"; OPTIND=$((OPTIND+1)); TIMESTAMP_LIST="$val" ;;
timestamp=*) TIMESTAMP_LIST="${OPTARG#timestamp=}" ;; timestamp=*) TIMESTAMP_LIST="${OPTARG#timestamp=}" ;;
timestamp) timestamp)
# support space-separated: --timestamp 0,0.5,1 OR single value
val="${!OPTIND}"; OPTIND=$((OPTIND+1)); TIMESTAMP_LIST="$val" ;; val="${!OPTIND}"; OPTIND=$((OPTIND+1)); TIMESTAMP_LIST="$val" ;;
black-pic-th=*) BLACK_PIC_TH="${OPTARG#black-pic-th=}" ;; black-pic-th=*) BLACK_PIC_TH="${OPTARG#black-pic-th=}" ;;
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=*) BLACK_MIN_D="${OPTARG#black-min-d=}" ;;
black-min-d) black-min-d)
val="${!OPTIND}"; OPTIND=$((OPTIND+1)); BLACK_MIN_D="$val" ;; 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 ;; *) echo "Unknown long flag --$OPTARG" >&2; usage; exit 1 ;;
esac esac
;; ;;
@ -190,17 +210,46 @@ else
REF_CROP="$REF_ALIGNED" REF_CROP="$REF_ALIGNED"
fi fi
# Collect files # Collect files (expand any globs AFTER WORKDIR cd)
FILES=() FILES=()
if [[ "$RECURSIVE" == "true" ]]; then if [[ "$RECURSIVE" == "true" ]]; then
while IFS= read -r -d '' f; do FILES+=("$f"); done < <(find . -type f -iname "*.mp4" -print0) while IFS= read -r -d '' f; do FILES+=("$f"); done < <(find . -type f -iname "*.mp4" -print0)
fi fi
# Expand user-supplied patterns like *.mp4 relative to current (WORKDIR) now
if [[ $# -gt 0 ]]; then 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 fi
if [[ ${#FILES[@]} -eq 0 ]]; then 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 exit 1
fi fi
@ -254,7 +303,7 @@ apply_roi_if_any() {
black_end_time() { black_end_time() {
local mp4="$1" local mp4="$1"
local out 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)}' echo "$out" | awk -F'black_end:' '/black_end/ {gsub(/ .*/, "", $2); print $2; exit}' | awk '{print ($1+0)}'
} }