#!/usr/bin/env bash # detect_pink_intro_hsv.sh [duration_s] [fps] [hue_min] [hue_max] [sat_min] [val_min] [min_ratio] # # Detect a magenta/pink intro by averaging frames to 1x1 and classifying in HSV. # # Defaults: # duration_s = 1.0 # fps = 6 # hue_min = 285 # degrees, magenta band start # hue_max = 330 # degrees, magenta band end # sat_min = 0.35 # require some saturation # val_min = 0.35 # avoid very dark frames # min_ratio = 0.6 # % of sampled frames that must be pink-ish # # Exit codes: 0 detected, 1 not detected, 2 usage error set -euo pipefail file="${1:-}" duration="${2:-1.0}" sample_fps="${3:-6}" HMIN="${4:-285}" HMAX="${5:-330}" SMIN="${6:-0.35}" VMIN="${7:-0.35}" MIN_RATIO="${8:-0.6}" if [[ -z "$file" ]]; then echo "Usage: $0 [duration_s] [fps] [hue_min] [hue_max] [sat_min] [val_min] [min_ratio]" >&2 exit 2 fi [[ -f "$file" ]] || { echo "File not found: $file" >&2; exit 2; } # Stream 1x1 RGB for sampled frames; each frame = 3 bytes. Parse with od+awk. summary="$( LC_ALL=C ffmpeg -hide_banner -v error \ -i "$file" -t "$duration" \ -vf "fps=${sample_fps},scale=1:1:flags=area,format=rgb24" \ -f rawvideo - 2>/dev/null \ | od -An -tu1 -w3 \ | awk -v HMIN="$HMIN" -v HMAX="$HMAX" -v SMIN="$SMIN" -v VMIN="$VMIN" ' function max(a,b){return a>b?a:b} function min(a,b){return aM) M=g; if(b>M) M=b; m=r; if(g=HMIN && H<=HMAX && S>=SMIN && V>=VMIN) pink++; # save last metrics to show a couple of examples (optional) if (total<=10) printf "DBG frame%02d: RGB=(%3d,%3d,%3d) HSV=(%6.1f°, %4.2f, %4.2f)\n", total,R,G,B,H,S,V > "/dev/stderr" } END { if (total==0) { printf "TOTAL=0 PINK=0 RATIO=0\n"; exit; } printf "TOTAL=%d PINK=%d RATIO=%.3f\n", total, pink, pink/total; }' )" if ! printf "%s" "$summary" | grep -q 'TOTAL='; then echo "No frames parsed (clip too short or pipeline issue)." exit 1 fi eval "$summary" # sets TOTAL,PINK,RATIO echo "File: $file" echo "Analyzed: first ${duration}s @ ${sample_fps} fps → frames: ${TOTAL}" echo "Pink-ish frames (hue in ${HMIN}-${HMAX}°, S>=${SMIN}, V>=${VMIN}): ${PINK}" echo "Ratio: ${RATIO} (threshold: ${MIN_RATIO})" awk -v r="$RATIO" -v thr="$MIN_RATIO" 'BEGIN{ exit !(r+0 >= thr+0) }' \ && { echo "DETECTED: Pink intro present"; exit 0; } echo "Not detected"; exit 1