diff --git a/bin/omarchy-transcode b/bin/omarchy-transcode index 9912f34e2db1..e791cc33baf3 100755 --- a/bin/omarchy-transcode +++ b/bin/omarchy-transcode @@ -3,21 +3,22 @@ # omarchy:group=transcode # omarchy:name= # omarchy:summary=Transcode pictures and videos for sharing -# omarchy:args=[--path path] [input] [format] [resolution] [quality] -# omarchy:examples=omarchy transcode|omarchy transcode --path ~/Downloads|omarchy transcode ~/Videos/demo.mov mp4 1080p|omarchy transcode ~/Pictures/background.heic jpg medium|omarchy transcode ~/Videos/demo.mov mp4 1080p low +# omarchy:args=[--path path] [--target size] [input] [format] [resolution] [quality] +# omarchy:examples=omarchy transcode|omarchy transcode --path ~/Downloads|omarchy transcode ~/Videos/demo.mov mp4 1080p|omarchy transcode ~/Pictures/background.heic jpg medium|omarchy transcode ~/Videos/demo.mov mp4 1080p low|omarchy transcode ~/Videos/demo.mov mp4 1080p --target 25M set -euo pipefail usage() { cat <<'EOF' Usage: - omarchy transcode [--path path] [input] [format] [resolution] [quality] + omarchy transcode [--path path] [--target size] [input] [format] [resolution] [quality] With no input, pick a picture or video from ~/Pictures and ~/Videos, or only from --path when provided. Then pick the output format, resolution, and for videos the quality. Options: - --path path Limit interactive fuzzy file selection to this path + --path path Limit interactive fuzzy file selection to this path + --target size Target output size (e.g. 25M, 1.5G); bare number means MB Formats: Pictures: jpg, png @@ -230,7 +231,7 @@ estimate_label() { awk -v dur="$1" -v kbps="$2" -v audio="$3" -v src="$4" 'BEGIN { bytes = dur * (kbps + audio) * 125 # kbps * 1000 / 8 bytes per second if (src != "" && bytes > src) { print "larger than source"; exit } - mb = bytes / 1048576 # MiB, the scale the completion notification reports + mb = bytes / 1000000 # decimal MB -- matches file managers and upload limits if (mb < 1) { print "~1 MB"; exit } d = int(log(mb) / log(10)) - 1 # round to 2 significant figures if (d < -1) d = -1 @@ -240,13 +241,14 @@ estimate_label() { } # Builds the quality rows as "\t\t" argv (the leading tab is the -# empty glyph field), pre-highlights medium, and prints the picked tier. The -# "label\tsubtext" return is stripped at the first tab and re-validated here so -# a foreign label dies inside the helper, before the notification boundary -- -# never reaching output_path or ffmpeg. +# empty glyph field), pre-highlights medium, and prints the picked tier -- or +# "target:" when the mp4-only Custom size… row is picked, which main() +# unwraps into the --target path. The "label\tsubtext" return is stripped at the +# first tab and re-validated here so a foreign label dies inside the helper, +# before the notification boundary -- never reaching output_path or ffmpeg. select_quality() { local input="$1" format="$2" resolution="$3" - local duration="" src_bytes="" audio_kbps=192 tier subtext selection + local duration="" src_bytes="" audio_kbps=192 tier subtext selection custom_label="Custom size…" custom_subtext="Enter a size like 25M" bytes local rows=() if [[ $format == "mp4" ]]; then @@ -273,9 +275,20 @@ select_quality() { rows+=($'\t'"$tier"$'\t'"$subtext") done + # mp4 only: the sentinel row's label doubles as its key -- the menu returns + # "label\tsubtext", and the strip below hands the label back verbatim. + [[ $format == "mp4" ]] && rows+=($'\t'"$custom_label"$'\t'"$custom_subtext") + selection=$(omarchy-menu-select "Select quality" "${rows[@]}" -- --default-index 1) || return selection="${selection%%$'\t'*}" + + if [[ $format == "mp4" && $selection == "$custom_label" ]]; then + bytes=$(prompt_target_size) || return + printf 'target:%s' "$bytes" + return + fi + case "$selection" in high | medium | low) ;; *) @@ -293,14 +306,204 @@ output_size_label() { bytes=$(stat -c %s "$1" 2>/dev/null || true) [[ $bytes =~ ^[0-9]+$ ]] || return 1 awk -v b="$bytes" 'BEGIN { - if (b > 0 && b < 1048576) { printf "<1 MB"; exit } - printf "%.0f MB", b / 1048576 + if (b > 0 && b < 1000000) { printf "<1 MB"; exit } + printf "%.0f MB", b / 1000000 }' } +# Parses a free-text --target size ("25M", "1.5G", "500K", "25MB", bare "25" +# meaning MB) into integer bytes on stdout, or prints an error on stderr and +# returns 1. A pure validator -- callers own the exit code (the flag arm +# propagates 2) and Phase 9's Custom size row re-prompts on failure. The raw +# string is discarded here: only the integer bytes and the numfmt --to=si +# token derived from them ever reach a filename or ffmpeg argv. Units are +# decimal (SI): 25M means 25,000,000 bytes, matching file managers and +# upload limits like Discord's. +parse_target_size() { + local raw="$1" normalized bytes + + [[ $raw =~ ^[0-9]+(\.[0-9]+)?([kKmMgG][bB]?)?$ ]] || + { echo "Invalid target size: $raw" >&2; return 1; } + + normalized="${raw%[bB]}" # drop an optional trailing B: 25MB -> 25M + normalized="${normalized^^}" # uppercase the unit letter: 25m -> 25M + [[ $normalized =~ [KMG]$ ]] || normalized+="M" # a bare number means MB + + bytes=$(numfmt --from=si "$normalized") || + { echo "Invalid target size: $raw" >&2; return 1; } + (( bytes > 0 )) || + { echo "Invalid target size: $raw" >&2; return 1; } + + printf '%s' "$bytes" +} + +# Prompts for a free-text target size behind the Custom size… row. Re-prompts +# exactly once -- the second prompt carries an error hint so it reads as +# "invalid", not "didn't take" (D-02) -- then cancels. Esc cancels at either +# prompt like every sibling. Empty submits ride through parse_target_size with +# everything else: one validator for both entry points. Planner refusals are +# NOT this loop's business (D-01). +prompt_target_size() { + local prompt="Target size (e.g. 25M)" answer bytes attempt + for attempt in 1 2; do + answer=$(omarchy-menu-input "$prompt") || return 1 + if bytes=$(parse_target_size "$answer"); then + printf '%s' "$bytes" + return 0 + fi + prompt="Invalid size — e.g. 25M" + done + return 1 +} + +# Plans a --target run: refuses a target at or above the source size (a +# re-encode can only degrade it -- drop --target and pick a quality for format +# conversion), probes duration and audio, derives the video bitrate +# (target*8/duration/1000 with a ~2% container reserve, minus the audio +# stream), then walks the locked 2000/800/400 kbps floors stepping +# 4k -> 1080p -> 720p until the budget fits. Prints " " on +# stdout; every refusal prints to stderr and returns nonzero, and cheap +# refusals precede the probes. A negative or zero derived bitrate is not +# special-cased -- it rides the floor loop to the 720p refusal, which IS the +# audio-alone-exceeds-budget refusal, so no negative, zero, or -nan -b:v ever +# reaches ffmpeg argv. +plan_target() { + local input="$1" target_bytes="$2" res="$3" + local src_bytes duration audio_kbps video_kbps floor min_label + + src_bytes=$(stat -c %s "$input" 2>/dev/null || true) + if [[ $src_bytes =~ ^[0-9]+$ ]] && (( target_bytes >= src_bytes )); then + echo "Invalid --target: $(numfmt --to=si "$target_bytes") meets or exceeds the source size ($(numfmt --to=si "$src_bytes")); drop --target and pick a quality for format conversion" >&2 + return 1 + fi + + # The probed duration is mandatory -- there is no qualitative fallback under + # --target. Captures stay on their own lines so a nonzero status is never + # masked by local. + duration=$(video_duration "$input") || + { echo "Cannot determine duration for $input; cannot size to a target" >&2; return 1; } + audio_kbps=$(video_audio_kbps "$input") + + # Duration is gated inside awk because (( )) cannot compare floats ("60.033" + # is a syntax error there). %d truncates toward zero and may go negative; + # the floor loop converts that into the 720p refusal. + video_kbps=$(awk -v b="$target_bytes" -v d="$duration" -v a="$audio_kbps" 'BEGIN { + if (d <= 0) exit 1 + printf "%d", b * 8 / d / 1000 * 0.98 - a + }') || + { echo "Cannot determine duration for $input; cannot size to a target" >&2; return 1; } + + while :; do + case "$res" in + 4k) floor=2000 ;; + 1080p) floor=800 ;; + 720p) floor=400 ;; + *) echo "Invalid video resolution: $res" >&2; return 1 ;; + esac + (( video_kbps >= floor )) && break + case "$res" in + 4k) res=1080p ;; + 1080p) res=720p ;; + *) + # Below the 720p floor: refuse naming the achievable minimum at that + # floor -- (400k + audio) * duration * 125 bytes, in decimal MB. + min_label=$(awk -v a="$audio_kbps" -v d="$duration" \ + 'BEGIN { printf "%.0f MB", (400 + a) * d * 125 / 1000000 }') + echo "Cannot fit under $(numfmt --to=si "$target_bytes") -- smallest achievable is ~$min_label for this duration" >&2 + return 1 + ;; + esac + done + + printf '%s %s\n' "$res" "$video_kbps" +} + +# Two-pass encode to a size budget: pass 1 writes stats into a per-run mktemp +# passdir under TMPDIR (never the output dir -- a stale passlog would silently +# corrupt the next run) and pass 2 encodes at the derived -b:v. The passdir is +# removed whole by an EXIT trap plus an explicit rm at the end -- EXIT, not +# RETURN, because a failed pass aborts under set -e without firing +# function-return traps, and the -d guard keeps the persisted trap inert after +# return (this script has no other EXIT trap). -vf/-c:v/-preset/-b:v are +# identical across passes; -an only on pass 1, -movflags only on pass 2, and +# no -crf anywhere (the tier arms own that flag). The scale and codec tables +# deliberately duplicate transcode_video's per the quality_token convention -- +# a shared table would refactor that code inside the PR conflict window. +transcode_video_target() { + local input="$1" resolution="$2" output="$3" video_kbps="$4" target_bytes="$5" + local notify_id="${6:-}" desc="${7:-}" + local scale codec preset passdir actual retry_kbps + + case "$resolution" in + 4k) scale='scale=-2:2160' ;; + 1080p) scale='scale=-2:1080' ;; + 720p) scale='scale=-2:720' ;; + *) + echo "Invalid video resolution: $resolution" >&2 + return 1 + ;; + esac + + if [[ $resolution == "4k" ]]; then + codec=libx265 + preset=slow + else + codec=libx264 + preset=fast + fi + + # The trap bakes the resolved path in -- locals die with the function under + # errexit, so a '$passdir' trap would see the variable already unset. + passdir=$(mktemp -d "${TMPDIR:-/tmp}/transcode-2pass.XXXXXX") + trap "rm -rf -- $(printf '%q' "$passdir")" EXIT + + ffmpeg -i "$input" -vf "$scale" -c:v "$codec" -preset "$preset" \ + -b:v "${video_kbps}k" -pass 1 -passlogfile "$passdir/2pass" \ + -an -f null /dev/null + + # One toast, replaced in place: pass 1/2 -> pass 2/2. A real if keeps the + # skip deliberate -- a failing [[ ]] && cmd statement would be silently + # inert too, but if names the intent. + if [[ $notify_id =~ ^[0-9]+$ ]]; then + omarchy-notification-send -r "$notify_id" -g  "Transcoding video…" \ + "${desc:-$(basename -- "$input") to mp4 ($resolution}, pass 2/2)" || true + fi + + ffmpeg -i "$input" -vf "$scale" -c:v "$codec" -preset "$preset" \ + -b:v "${video_kbps}k" -pass 2 -passlogfile "$passdir/2pass" \ + -c:a aac -b:a 192k -movflags +faststart "$output" + + # Any byte-over target triggers exactly one pass-2-only retry at a + # tightened bitrate -- video_kbps scaled by target/actual, resolution and + # the 192k audio fixed (re-planning could silently move resolution after + # the filename and toasts committed to it). The retry encodes to a passdir + # sibling -- unique, so no -y is ever needed -- and mv's onto $output only + # on success, so a failed retry preserves the overshot-but-playable first + # output. The done toast reports the stat-measured actual size either way; + # that honest hit/miss report is the point of the feature. + actual=$(stat -c %s "$output" 2>/dev/null || true) + if [[ $actual =~ ^[0-9]+$ ]] && (( actual > target_bytes )); then + retry_kbps=$(awk -v k="$video_kbps" -v t="$target_bytes" -v a="$actual" \ + 'BEGIN { printf "%d", k * t / a }') + if [[ $retry_kbps =~ ^[0-9]+$ ]] && (( retry_kbps > 0 )); then + if ffmpeg -i "$input" -vf "$scale" -c:v "$codec" -preset "$preset" \ + -b:v "${retry_kbps}k" -pass 2 -passlogfile "$passdir/2pass" \ + -c:a aac -b:a 192k -movflags +faststart "$passdir/retry.mp4"; then + mv -f -- "$passdir/retry.mp4" "$output" + fi + fi + fi + + # The retry reuses the passlog, so the explicit cleanup waits until here; + # the EXIT trap stays armed as the failure-path backstop. + rm -rf "$passdir" +} + main() { local input="" format="" resolution="" search_path="" local type output quality size positional=() + local target_bytes="" target_token="" video_kbps="" requested_resolution="" + local notify_id="" plan="" target_desc="" while (( $# > 0 )); do case "$1" in @@ -309,6 +512,12 @@ main() { (( $# > 0 )) || { echo "Missing value for --path" >&2; return 2; } search_path="$1" ;; + --target) + shift + (( $# > 0 )) || { echo "Missing value for --target" >&2; return 2; } + target_bytes=$(parse_target_size "$1") || return 2 + target_token=$(numfmt --to=si "$target_bytes") + ;; --help | -h) usage return 0 @@ -346,6 +555,10 @@ main() { type=$(media_type "$input") if [[ -n $quality ]]; then + if [[ -n $target_bytes ]]; then + echo "Invalid argument: --target cannot be combined with a quality tier ($quality)" >&2 + return 1 + fi if [[ $type == "picture" ]]; then echo "Invalid argument: quality applies to videos only" >&2 return 1 @@ -360,6 +573,11 @@ main() { esac fi + if [[ -n $target_bytes && $type == "picture" ]]; then + echo "Invalid argument: --target applies to videos only" >&2 + return 1 + fi + if [[ -z $format ]]; then if [[ $type == "picture" ]]; then format=$(omarchy-menu-select "Select format" jpg png) @@ -384,6 +602,13 @@ main() { return 1 ;; esac + # Format may have come from the interactive menu, so the gif refusal + # lives here -- a menu-picked gif under --target dies on this check, not + # on the resolution case below. + if [[ -n $target_bytes && $format == "gif" ]]; then + echo "Invalid argument: --target is not supported for gif output" >&2 + return 1 + fi case "$resolution" in 4k | 1080p | 720p) ;; *) @@ -408,15 +633,48 @@ main() { esac fi - if [[ $type == "video" && -z $quality ]]; then + if [[ $type == "video" && -z $quality && -z $target_bytes ]]; then quality=$(select_quality "$input" "$format" "$resolution") + # A Custom size… pick returns target: -- unwrap into the same two + # variables --target sets, so the planner block, output_path, and the + # dispatch below are byte-identical to a CLI run (D-00f). + if [[ $quality == target:* ]]; then + target_bytes="${quality#target:}" + [[ $target_bytes =~ ^[0-9]+$ ]] || + { echo "Invalid target size: $target_bytes" >&2; return 1; } + target_token=$(numfmt --to=si "$target_bytes") + quality="" + fi fi - output=$(output_path "$input" "$format" "$resolution" "$quality") + # Under --target the planner runs after every menu and before the output + # name: it may step the requested resolution down to the highest rung whose + # bitrate floor fits, and writing the effective rung back into $resolution + # makes the filename and both toasts name it for free. + if [[ -n $target_bytes ]]; then + requested_resolution="$resolution" + plan=$(plan_target "$input" "$target_bytes" "$requested_resolution") + read -r resolution video_kbps <<<"$plan" + fi + + output=$(output_path "$input" "$format" "$resolution" "${target_token:-$quality}") if [[ $type == "video" ]]; then - omarchy-notification-send -g  "Transcoding video…" "$(basename -- "$input") to $format ($resolution)" - transcode_video "$input" "$format" "$resolution" "$output" "$quality" + if [[ -n $target_bytes ]]; then + # The start toast carries the effective resolution, the step-down + # disclosure, and the target; -p captures the daemon id so the pass-2 + # update inside transcode_video_target can replace it in place. + target_desc="$(basename -- "$input") to $format ($resolution" + if [[ $requested_resolution != "$resolution" ]]; then + target_desc+=" — stepped down from $requested_resolution" + fi + target_desc+=", target ≤$target_token" + notify_id=$(omarchy-notification-send -p -g  "Transcoding video…" "$target_desc, pass 1/2)") + transcode_video_target "$input" "$resolution" "$output" "$video_kbps" "$target_bytes" "$notify_id" "$target_desc" + else + omarchy-notification-send -g  "Transcoding video…" "$(basename -- "$input") to $format ($resolution)" + transcode_video "$input" "$format" "$resolution" "$output" "$quality" + fi copy_to_clipboard "$output" size=$(output_size_label "$output" || true) omarchy-notification-send -g  "Transcoded to $resolution $format" "Saved and copied to clipboard${size:+ ($size)}." diff --git a/test/shell.d/transcode-quality-test.sh b/test/shell.d/transcode-quality-test.sh index 3819323f755d..c756b843eecf 100644 --- a/test/shell.d/transcode-quality-test.sh +++ b/test/shell.d/transcode-quality-test.sh @@ -38,12 +38,66 @@ for command in ffmpeg magick; do printf '%s' "${0##*/}" printf ' %q' "$@" printf '\n' - printf 'out=%q\n' "${!#}" } >>"$CALLS" -if [[ -n ${FAKE_OUT_BYTES:-} ]]; then - truncate -s "$FAKE_OUT_BYTES" "${!#}" -fi -exit "${FAKE_ENCODE_RC:-0}" +case " $* " in +*" -pass 1 "*) + # Two-pass stats pass: the last positional is /dev/null on the null muxer, + # never an output -- record argv only. Synthesize the passlog artifacts + # (-0.log/.mbtree) from the -passlogfile arg so the script's passdir + # cleanup assertions have real files to count. + prev="" + for arg in "$@"; do + if [[ $prev == "-passlogfile" ]]; then + touch "$arg-0.log" "$arg-0.log.mbtree" + break + fi + prev="$arg" + done + exit "${FAKE_PASS1_RC:-${FAKE_ENCODE_RC:-0}}" + ;; +*" -pass 2 "*) + # A real encode pass: records out= and sizes the output. When a run makes + # more than one pass-2 call (the overshoot retry), the first consumes + # FAKE_OUT_BYTES and later ones FAKE_OUT_BYTES2; FAKE_PASS2_RC is a + # space-separated per-invocation RC list ("0 1" = first ok, retry dies) + # falling back to its last word, then FAKE_ENCODE_RC. The invocation + # number is the count of pass-2 lines in this run's CALLS, which resets + # per run with the log itself. A pass-2 call also requires the pass-1 + # artifacts at its own -passlogfile -- a missing flag or a wrong dir exits + # 90, so argv can never claim a two-pass encode the logs do not back. + printf 'out=%q\n' "${!#}" >>"$CALLS" + prev="" + passlog="" + for arg in "$@"; do + if [[ $prev == "-passlogfile" ]]; then + passlog="$arg" + break + fi + prev="$arg" + done + [[ -n $passlog && -f ${passlog}-0.log ]] || exit 90 + n=$(grep -c ' -pass 2 ' "$CALLS") + size="${FAKE_OUT_BYTES:-}" + if (( n > 1 )) && [[ -n ${FAKE_OUT_BYTES2:-} ]]; then + size="$FAKE_OUT_BYTES2" + fi + if [[ -n $size ]]; then + truncate -s "$size" "${!#}" + fi + if [[ -n ${FAKE_PASS2_RC:-} ]]; then + read -ra rcs <<<"$FAKE_PASS2_RC" + exit "${rcs[$(( n - 1 ))]:-${rcs[-1]}}" + fi + exit "${FAKE_ENCODE_RC:-0}" + ;; +*) + printf 'out=%q\n' "${!#}" >>"$CALLS" + if [[ -n ${FAKE_OUT_BYTES:-} ]]; then + truncate -s "$FAKE_OUT_BYTES" "${!#}" + fi + exit "${FAKE_ENCODE_RC:-0}" + ;; +esac SH done @@ -55,6 +109,9 @@ SH cat >"$STUB_DIR/omarchy-notification-send" <<'SH' #!/bin/bash printf 'notification: %s\n' "$*" >>"$CALLS" +case " $* " in +*" -p "* | *" --print-id "*) echo 7 ;; +esac SH # omarchy-menu-file stays a pure tripwire: no row below goes through the file @@ -86,6 +143,26 @@ fi printf '%s' "$FAKE_PICK" SH +# omarchy-menu-input mirrors the real binary's three states: a set FAKE_INPUT +# prints it and exits 0 (submit); a set-but-empty FAKE_INPUT exits 0 with +# empty stdout (empty submit -- NOT a cancel, the real binary writes "\n" so +# -s is true); unset exits 1 (Esc, and the tripwire for runs that must never +# reach the input prompt). Per-invocation knobs (FAKE_INPUT, FAKE_INPUT2, …) +# feed the re-prompt loop -- same convention as FAKE_OUT_BYTES/FAKE_OUT_BYTES2, +# with the invocation number counted off the just-logged menu-input: line +# exactly like the pass-2 counter above. The log line is written before the +# decision, so an unexpected call still leaves evidence; %s not %q so literal +# tabs in argv stay greppable. +cat >"$STUB_DIR/omarchy-menu-input" <<'SH' +#!/bin/bash +printf 'menu-input: %s\n' "$*" >>"$CALLS" +n=$(grep -c '^menu-input:' "$CALLS") +var=FAKE_INPUT +(( n > 1 )) && var="FAKE_INPUT$n" +[[ -z ${!var+x} ]] && exit 1 +printf '%s' "${!var}" +SH + # ffprobe dispatches on argv: the audio-presence probe carries # `stream=codec_type` glued to `-show_entries`, so ` stream=codec_type ` in # " $* " selects that arm -- a bare ` codec_type ` glob never matches the real @@ -124,7 +201,7 @@ run_transcode() { local status=0 : >"$calls" - HOME="$TMPDIR/home" PATH="$STUB_DIR:$PATH" CALLS="$calls" \ + HOME="$TMPDIR/home" PATH="$STUB_DIR:$PATH" CALLS="$calls" TMPDIR="$TMPDIR" \ "$ROOT/bin/omarchy-transcode" "$@" >"$TMPDIR/stdout" 2>"$TMPDIR/stderr" || status=$? grep '^ffmpeg' "$calls" >>"$ffmpeg_calls" 2>/dev/null || true @@ -132,7 +209,7 @@ run_transcode() { } # A sized fixture: stat reports 120 MiB, above the largest pinned estimate -# (~107 MiB at dur=157/1080p/high), so estimate rows render numbers instead of +# (~110 MB at dur=157/1080p/high), so estimate rows render numbers instead of # degrading to larger-than-source. Sparse, so it costs no real blocks. truncate -s 120M "$TMPDIR/in.mov" touch "$TMPDIR/img.png" @@ -377,13 +454,13 @@ pass "usage documents [quality] and the video tiers" # rows (leading tab = empty glyph field) carrying CRF N · ~N MB subtexts at the # locked 1080p midpoints plus the 192k audio term, and --default-index 1 # pre-highlighting medium. -FAKE_DURATION=60 FAKE_PICK=$'medium\tCRF 23 · ~23 MB' \ +FAKE_DURATION=60 FAKE_PICK=$'medium\tCRF 23 · ~24 MB' \ run_transcode "$TMPDIR/in.mov" mp4 1080p grep -F 'Select quality' "$calls" >/dev/null || fail "an unset video quality fires the Select quality menu" "$(cat "$calls")" grep -F -- '--default-index 1' "$calls" >/dev/null || fail "the quality menu pre-highlights medium" "$(cat "$calls")" -for row in $'\thigh\tCRF 18 · ~41 MB' $'\tmedium\tCRF 23 · ~23 MB' $'\tlow\tCRF 28 · ~11 MB'; do +for row in $'\thigh\tCRF 18 · ~43 MB' $'\tmedium\tCRF 23 · ~24 MB' $'\tlow\tCRF 28 · ~12 MB'; do grep -F "$row" "$calls" >/dev/null || fail "the quality menu offers a $row row" "$(cat "$calls")" done @@ -437,10 +514,10 @@ fi pass "an empty menu pick aborts before the notification" # Sig-fig rendering: a 157 s clip at 1080p rounds each estimate to 1-2 -# significant figures (~110 / ~60 / ~30 MB) -- never a decimal point. -FAKE_DURATION=157 FAKE_PICK=$'medium\tCRF 23 · ~60 MB' \ +# significant figures (~110 / ~63 / ~31 MB) -- never a decimal point. +FAKE_DURATION=157 FAKE_PICK=$'medium\tCRF 23 · ~63 MB' \ run_transcode "$TMPDIR/in.mov" mp4 1080p -for row in 'CRF 18 · ~110 MB' 'CRF 23 · ~60 MB' 'CRF 28 · ~30 MB'; do +for row in 'CRF 18 · ~110 MB' 'CRF 23 · ~63 MB' 'CRF 28 · ~31 MB'; do grep -F "$row" "$calls" >/dev/null || fail "a 157 s clip offers a $row row" "$(cat "$calls")" done @@ -449,17 +526,17 @@ if grep '^menu-select: ' "$calls" | grep -E '~[0-9]*\.[0-9]' >/dev/null; then fi pass "estimates render at 1-2 significant figures with no decimals" -# A 15 MiB source sits between the low (~11 MB) and medium (~23 MB) estimates +# A 15 MiB source sits between the low (~12 MB) and medium (~24 MB) estimates # at 60 s/1080p, so only the exceeding tiers degrade -- per-row, never the # whole menu. truncate -s 15M "$TMPDIR/small.mov" -FAKE_DURATION=60 FAKE_PICK=$'low\tCRF 28 · ~11 MB' \ +FAKE_DURATION=60 FAKE_PICK=$'low\tCRF 28 · ~12 MB' \ run_transcode "$TMPDIR/small.mov" mp4 1080p grep -F $'\thigh\tCRF 18 · larger than source' "$calls" >/dev/null || fail "the high row degrades when its estimate exceeds the source" "$(cat "$calls")" grep -F $'\tmedium\tCRF 23 · larger than source' "$calls" >/dev/null || fail "the medium row degrades when its estimate exceeds the source" "$(cat "$calls")" -grep -F $'\tlow\tCRF 28 · ~11 MB' "$calls" >/dev/null || +grep -F $'\tlow\tCRF 28 · ~12 MB' "$calls" >/dev/null || fail "the low row keeps its estimate under the source size" "$(cat "$calls")" rm -f "$TMPDIR/small.mov" pass "larger than source degrades per row, not per menu" @@ -506,10 +583,10 @@ grep '^ffmpeg ' "$calls" >/dev/null || pass "a failed probe degrades to qualitative rows without aborting" # A proven-audio-less source drops the 192k term: 60 s at 1080p renders -# ~39/~21/~10 MB instead of ~41/~23/~11. -FAKE_AUDIO=no FAKE_DURATION=60 FAKE_PICK=$'medium\tCRF 23 · ~21 MB' \ +# ~41/~23/~11 MB instead of ~43/~24/~12. +FAKE_AUDIO=no FAKE_DURATION=60 FAKE_PICK=$'medium\tCRF 23 · ~23 MB' \ run_transcode "$TMPDIR/in.mov" mp4 1080p -for row in $'\thigh\tCRF 18 · ~39 MB' $'\tmedium\tCRF 23 · ~21 MB' $'\tlow\tCRF 28 · ~10 MB'; do +for row in $'\thigh\tCRF 18 · ~41 MB' $'\tmedium\tCRF 23 · ~23 MB' $'\tlow\tCRF 28 · ~11 MB'; do grep -F "$row" "$calls" >/dev/null || fail "a source with no audio stream offers a $row row" "$(cat "$calls")" done @@ -522,6 +599,9 @@ for subtext in '15 fps' '10 fps' '5 fps'; do grep -F "$subtext" "$calls" >/dev/null || fail "the gif menu offers a $subtext row" "$(cat "$calls")" done +if grep -F 'Custom' "$calls" >/dev/null; then + fail "the gif menu never offers a Custom size row" "$(cat "$calls")" +fi if grep '^menu-select: ' "$calls" | grep -F '~' >/dev/null; then fail "gif rows never carry size estimates" "$(cat "$calls")" fi @@ -534,6 +614,19 @@ grep -Fx "out=$TMPDIR/in-720p-low.gif" "$calls" >/dev/null || fail "a low gif pick writes in-720p-low.gif" "$(cat "$calls")" pass "gif rows carry fps subtexts and never probe the input" +# A forged Custom size… pick on a gif run must die at the whitelist like any +# other foreign label — never minting target: or firing the input prompt. +status=0 +FAKE_PICK=$'Custom size…\tEnter a size like 25M' \ + run_transcode "$TMPDIR/in.mov" gif 720p || status=$? +[[ $status -ne 0 ]] || fail "a forged Custom pick on gif is refused" "exit=$status" +grep -F 'Invalid video quality' "$TMPDIR/stderr" >/dev/null || + fail "a forged gif sentinel dies at the whitelist" "$(cat "$TMPDIR/stderr")" +if grep -q '^menu-input:' "$calls"; then + fail "a forged gif sentinel never fires the input prompt" "$(cat "$calls")" +fi +pass "a forged Custom pick on gif dies at the whitelist" + # Pictures stop after the resolution prompt: no quality menu, no probe. run_transcode "$TMPDIR/img.png" jpg medium if grep -q '^menu-select:' "$calls" || grep -q '^ffprobe:' "$calls"; then @@ -574,21 +667,22 @@ fi pass "an unknown format fails before any menu or notification" # The done notification reports the output's real size: FAKE_OUT_BYTES makes -# the stub write a 38.00 MiB file, the script's own stat+awk chain measures -# it, and the body lands as "(38 MB)" -- the same MiB scale the ~N MB menu -# estimates use. Assertions filter on 'Transcoded to' because the start +# the stub write a 39,845,888-byte file, the script's own stat+awk chain +# measures it, and the body lands as "(40 MB)" -- the same decimal-MB scale +# the ~N MB menu estimates and file managers use. Assertions filter on +# 'Transcoded to' because the start # notification body carries its own parenthetical. The knob-created output # is this row's fixture -- rm -f it after asserting. FAKE_OUT_BYTES=39845888 run_transcode "$TMPDIR/in.mov" mp4 1080p medium grep 'notification:' "$calls" | grep -F 'Transcoded to 1080p mp4' | - grep -F 'Saved and copied to clipboard (38 MB).' >/dev/null || + grep -F 'Saved and copied to clipboard (40 MB).' >/dev/null || fail "the video done notification reports the output size" "$(cat "$calls")" rm -f "$TMPDIR/in-1080p.mp4" pass "the video done notification reports the output size" FAKE_OUT_BYTES=39845888 run_transcode "$TMPDIR/img.png" jpg medium grep 'notification:' "$calls" | grep -F 'Transcoded to medium jpg' | - grep -F 'Saved and copied to clipboard (38 MB).' >/dev/null || + grep -F 'Saved and copied to clipboard (40 MB).' >/dev/null || fail "the picture done notification reports the output size" "$(cat "$calls")" rm -f "$TMPDIR/img-medium.jpg" pass "the picture done notification reports the output size" @@ -623,7 +717,7 @@ pass "a failed encode sends zero done notifications" # cheap pin that no arm is left on the plain body. FAKE_OUT_BYTES=39845888 run_transcode "$TMPDIR/in.mov" gif 720p low grep 'notification:' "$calls" | grep -F 'Transcoded to 720p gif' | - grep -F 'Saved and copied to clipboard (38 MB).' >/dev/null || + grep -F 'Saved and copied to clipboard (40 MB).' >/dev/null || fail "the gif done notification reports the output size" "$(cat "$calls")" rm -f "$TMPDIR/in-720p-low.gif" pass "the gif done notification reports the output size" @@ -636,12 +730,12 @@ FAKE_OUT_BYTES=39845888 run_transcode "$TMPDIR/in.mov" mp4 1080p medium grep -Fx "out=$TMPDIR/in-1080p-2.mp4" "$calls" >/dev/null || fail "an existing output dedupes to -2 with FAKE_OUT_BYTES set" "$(cat "$calls")" grep 'notification:' "$calls" | grep -F 'Transcoded to 1080p mp4' | - grep -F 'Saved and copied to clipboard (38 MB).' >/dev/null || + grep -F 'Saved and copied to clipboard (40 MB).' >/dev/null || fail "a deduped output reports its own size" "$(cat "$calls")" rm -f "$TMPDIR/in-1080p.mp4" "$TMPDIR/in-1080p-2.mp4" pass "a deduped output reports the size of the file actually written" -# A non-empty output under 1 MiB reads "(<1 MB)", never "(0 MB)" -- a zero +# A non-empty output under 1 MB reads "(<1 MB)", never "(0 MB)" -- a zero # size on a real file would read as a lie next to the ~1 MB estimate floor. FAKE_OUT_BYTES=200000 run_transcode "$TMPDIR/in.mov" mp4 1080p medium grep 'notification:' "$calls" | grep -F 'Transcoded to 1080p mp4' | @@ -650,8 +744,8 @@ grep 'notification:' "$calls" | grep -F 'Transcoded to 1080p mp4' | rm -f "$TMPDIR/in-1080p.mp4" pass "a sub-1 MiB output reports <1 MB" -# Sub-10 MiB estimates round instead of flooring (WR-01): 18 s at 720p with -# 192k audio is 5.8/3.6/1.9 MiB -- %.0f renders ~6/~4/~2 where the old %d +# Sub-10 MB estimates round instead of flooring (WR-01): 18 s at 720p with +# 192k audio is 6.1/3.8/2.0 MB -- %.0f renders ~6/~4/~2 where the old %d # floored to ~5/~3/~1. All three sit far below the 120 MiB fixture, so no # larger-than-source degrade interferes. FAKE_DURATION=18 FAKE_PICK=$'medium\tCRF 23 · ~4 MB' \ @@ -661,3 +755,678 @@ for row in $'\thigh\tCRF 18 · ~6 MB' $'\tmedium\tCRF 23 · ~4 MB' $'\tlow\tCRF fail "an 18 s 720p clip offers a $row row" "$(cat "$calls")" done pass "sub-10 MiB estimates round instead of flooring" + +# --- --target: happy path ------------------------------------------------- +# 25M over a 60 s clip with 192k audio derives 25000000*8/60/1000*0.98-192 = +# 3074k, so 1080p holds its 800k floor and the run two-passes at the derived +# rate: pass 1 stats-only to /dev/null, pass 2 the real encode -- with +# -vf/-c:v/-preset/-b:v byte-identical across passes and no -crf anywhere. +FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 1080p --target 25M + +[[ $(grep -c '^ffmpeg ' "$calls") -eq 2 ]] || + fail "a --target run records exactly two ffmpeg lines" "$(cat "$calls")" +pass1_line=$(grep -m1 ' -pass 1 ' "$calls") +pass2_line=$(grep -m1 ' -pass 2 ' "$calls") +[[ -n $pass1_line && -n $pass2_line ]] || + fail "a --target run records a pass 1 and a pass 2 line" "$(cat "$calls")" +for frag in '-pass 1' '-passlogfile' '-an' '-f null /dev/null' '-b:v 3074k' \ + 'scale=-2:1080' '-c:v libx264' '-preset fast'; do + grep -F -- "$frag" <<<"$pass1_line" >/dev/null || + fail "pass 1 carries $frag" "$pass1_line" +done +for frag in '-crf' '-movflags' '-c:a'; do + if grep -F -- "$frag" <<<"$pass1_line" >/dev/null; then + fail "pass 1 never carries $frag" "$pass1_line" + fi +done +for frag in '-pass 2' '-passlogfile' '-b:v 3074k' '-c:a aac' '-b:a 192k' \ + '-movflags +faststart'; do + grep -F -- "$frag" <<<"$pass2_line" >/dev/null || + fail "pass 2 carries $frag" "$pass2_line" +done +if grep -F -- '-crf' <<<"$pass2_line" >/dev/null; then + fail "pass 2 never carries -crf" "$pass2_line" +fi +for field in '-vf' '-c:v' '-preset' '-b:v'; do + v1=$(grep -o -- "$field [^ ]*" <<<"$pass1_line") + v2=$(grep -o -- "$field [^ ]*" <<<"$pass2_line") + [[ -n $v1 && $v1 == "$v2" ]] || + fail "$field is identical across passes" "pass1: $v1 / pass2: $v2" +done +grep -Fx "out=$TMPDIR/in-1080p-25M.mp4" "$calls" >/dev/null || + fail "a --target run writes the -25M output name" "$(cat "$calls")" +pass "a --target run two-passes at the derived bitrate with byte-identical shared flags" + +# The start toast carries -p so the pass-2 update replaces it in place on the +# stub's id 7, and it still precedes the first ffmpeg call. +grep 'notification:' "$calls" | grep -F -- '-p ' | grep -F 'pass 1/2' >/dev/null || + fail "the --target start toast prints an id and names pass 1/2" "$(cat "$calls")" +grep 'notification:' "$calls" | grep -F -- '-r 7' | grep -F 'pass 2/2' >/dev/null || + fail "the pass-2 toast replaces the start toast in place" "$(cat "$calls")" +notify_line=$(grep -n 'Transcoding' "$calls" | head -n1 | cut -d: -f1 || true) +ffmpeg_line=$(grep -n '^ffmpeg ' "$calls" | head -n1 | cut -d: -f1 || true) +[[ -n $notify_line && -n $ffmpeg_line ]] || + fail "a --target run records the start toast and an ffmpeg call" "$(cat "$calls")" +(( notify_line < ffmpeg_line )) || + fail "the --target start toast precedes the encode" "$(cat "$calls")" +pass "a --target run scopes its toast per pass on one notification id" + +# --target probes exactly twice (duration + audio presence) -- the positive +# twin of the tier run's never-probes pin. +[[ $(grep -c '^ffprobe:' "$calls") -eq 2 ]] || + fail "a --target run probes exactly twice" "$(cat "$calls")" +pass "a --target run probes exactly twice" + +# All four slots supplied plus --target: zero menus, and the quality menu in +# particular is skipped entirely. +if grep -q 'Select quality' "$calls" || grep -q '^menu-select:' "$calls"; then + fail "a fully-positional --target run never prompts" "$(cat "$calls")" +fi +pass "a fully-positional --target run skips the quality menu" + +# Parse accepts: 25m / 25MB / bare 25 all canonicalize to the -25M filename +# token; 1.5G carries -1.5G. Each plans fine at 60 s/1080p -- the 1.5G row +# needs a 2G sparse source because a 1.5G target would refuse as >= source on +# the 120 MiB fixture. +for target in 25m 25MB 25; do + FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 1080p --target "$target" + grep -Fx "out=$TMPDIR/in-1080p-25M.mp4" "$calls" >/dev/null || + fail "--target $target canonicalizes to the -25M filename token" "$(cat "$calls")" +done +truncate -s 2G "$TMPDIR/big.mov" +FAKE_DURATION=60 run_transcode "$TMPDIR/big.mov" mp4 1080p --target 1.5G +grep -Fx "out=$TMPDIR/big-1080p-1.5G.mp4" "$calls" >/dev/null || + fail "--target 1.5G canonicalizes to the -1.5G filename token" "$(cat "$calls")" +rm -f "$TMPDIR/big.mov" +# 500K is accepted by the parser but cannot fit at 60 s -- the accept signal +# is reaching the planner (probe lines + the floor refusal), not an output. +if FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 1080p --target 500K; then + fail "a 500K target at 60 s refuses below every floor" +fi +grep -q '^ffprobe:' "$calls" || + fail "--target 500K parses and reaches the planner probes" "$(cat "$calls")" +grep -F 'smallest achievable' "$TMPDIR/stderr" >/dev/null || + fail "--target 500K refuses naming the achievable minimum" "$(cat "$TMPDIR/stderr")" +pass "size forms 25m/25MB/25/1.5G/500K parse and canonicalize" + +# A missing --target value exits 2 on stderr before any side effect. +status=0 +run_transcode "$TMPDIR/in.mov" mp4 1080p --target || status=$? +[[ $status -eq 2 ]] || + fail "a missing --target value exits 2" "exit=$status" +grep -F 'Missing value for --target' "$TMPDIR/stderr" >/dev/null || + fail "a missing --target value reports on stderr" "$(cat "$TMPDIR/stderr")" +if [[ -s $calls ]]; then + fail "a missing --target value precedes every side effect" "$(cat "$calls")" +fi +pass "a missing --target value exits 2 with an empty call log" + +# Step-down: a 4k request on a 10M/60s budget derives 1114k -- below the 4k +# floor (2000k) but above 1080p's (800k) -- so the run steps to 1080p and the +# filename and toasts all name the effective rung with the disclosure. +FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 4k --target 10M +grep -Fx "out=$TMPDIR/in-1080p-10M.mp4" "$calls" >/dev/null || + fail "a 10M target at 4k steps down to the -1080p-10M name" "$(cat "$calls")" +grep ' -pass 2 ' "$calls" | grep -F 'scale=-2:1080' >/dev/null || + fail "a stepped-down run encodes at 1080p" "$(cat "$calls")" +grep ' -pass 2 ' "$calls" | grep -F -- '-b:v 1114k' >/dev/null || + fail "a stepped-down run derives -b:v 1114k" "$(cat "$calls")" +grep 'notification:' "$calls" | grep -F '1080p' | + grep -F 'stepped down from 4k' | grep -F '10M' >/dev/null || + fail "the start toast names the effective rung, the step-down, and the target" "$(cat "$calls")" +grep 'notification:' "$calls" | grep -F 'Transcoded to 1080p mp4' >/dev/null || + fail "the done toast names the effective rung too" "$(cat "$calls")" +pass "a 4k request on a 10M budget steps down to 1080p and says so" + +# One rung further: 5M/60s derives 461k -- below 1080p's floor, above 720p's. +# numfmt --to=si canonicalizes exact-decimal targets at 3 significant figures, +# so 5M ships as the -5.0M token (the accepted 1.0M-style shape). +FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 4k --target 5M +grep -Fx "out=$TMPDIR/in-720p-5.0M.mp4" "$calls" >/dev/null || + fail "a 5M target at 4k steps down to the -720p-5.0M name" "$(cat "$calls")" +grep ' -pass 2 ' "$calls" | grep -F 'scale=-2:720' >/dev/null || + fail "a 5M target encodes at 720p" "$(cat "$calls")" +grep ' -pass 2 ' "$calls" | grep -F -- '-b:v 461k' >/dev/null || + fail "a 5M target derives -b:v 461k" "$(cat "$calls")" +pass "a 4k request on a 5M budget steps down to 720p" + +# Boundary: 25M/60s derives 3074k, at or above the 4k floor, so a 4k request +# stays at 4k on libx265 -preset slow. +FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 4k --target 25M +grep ' -pass 2 ' "$calls" | grep -F 'scale=-2:2160' >/dev/null || + fail "a 25M target at 4k keeps the 4k scale" "$(cat "$calls")" +grep ' -pass 2 ' "$calls" | grep -F -- '-c:v libx265 -preset slow' >/dev/null || + fail "a 25M target at 4k keeps the x265 codec" "$(cat "$calls")" +grep ' -pass 2 ' "$calls" | grep -F -- '-b:v 3074k' >/dev/null || + fail "a 25M target at 4k derives -b:v 3074k" "$(cat "$calls")" +grep -Fx "out=$TMPDIR/in-4k-25M.mp4" "$calls" >/dev/null || + fail "a 25M target at 4k writes the -4k-25M name" "$(cat "$calls")" +pass "a target at or above the 4k floor stays at 4k" + +# --- --target: refusal matrix --------------------------------------------- +# Parse rejects die in the arg loop -- exit 2, the rejected input on stderr, +# and a provably empty call log (before media_type, any menu, or any toast). +for bad in abc -5M 0 25.5.2M ""; do + status=0 + run_transcode "$TMPDIR/in.mov" mp4 1080p --target "$bad" || status=$? + [[ $status -eq 2 ]] || + fail "--target $bad exits 2" "exit=$status" + grep -F "Invalid target size: $bad" "$TMPDIR/stderr" >/dev/null || + fail "--target $bad names the rejected input" "$(cat "$TMPDIR/stderr")" + if [[ -s $calls ]]; then + fail "--target $bad precedes every side effect" "$(cat "$calls")" + fi +done +pass "invalid --target values exit 2 before any side effect" + +# --target and a positional quality tier are mutually exclusive: exit 1, +# stderr naming both, before every side effect. +status=0 +run_transcode "$TMPDIR/in.mov" mp4 1080p low --target 25M || status=$? +[[ $status -eq 1 ]] || + fail "--target plus a quality tier exits 1" "exit=$status" +grep -F -- '--target' "$TMPDIR/stderr" >/dev/null && + grep -F 'low' "$TMPDIR/stderr" >/dev/null || + fail "the conflict error names both --target and the tier" "$(cat "$TMPDIR/stderr")" +if [[ -s $calls ]]; then + fail "--target plus a quality tier precedes every side effect" "$(cat "$calls")" +fi +pass "--target plus a quality tier refuses naming both" + +# A positional gif under --target refuses before any notification or encode; +# the file stub writes no CALLS line, so the log stays fully empty here. +status=0 +run_transcode "$TMPDIR/in.mov" gif 720p --target 25M || status=$? +[[ $status -eq 1 ]] || + fail "--target gif exits 1" "exit=$status" +grep -F 'not supported for gif' "$TMPDIR/stderr" >/dev/null || + fail "--target gif reports the gif refusal" "$(cat "$TMPDIR/stderr")" +if grep -q 'notification:' "$calls" || grep -q '^ffmpeg ' "$calls"; then + fail "--target gif refuses before notification and encode" "$(cat "$calls")" +fi +pass "--target gif refuses pre-notification" + +# A menu-picked gif under --target hits the same refusal -- after the format +# and resolution menus, but before the resolution case, so the error names +# gif (never "Invalid video resolution") and the log holds menu lines only. +status=0 +FAKE_PICK=gif run_transcode "$TMPDIR/in.mov" --target 25M || status=$? +[[ $status -eq 1 ]] || + fail "a menu-picked gif under --target exits 1" "exit=$status" +grep -F 'not supported for gif' "$TMPDIR/stderr" >/dev/null || + fail "a menu-picked gif reports the gif refusal" "$(cat "$TMPDIR/stderr")" +if grep -F 'Invalid video resolution' "$TMPDIR/stderr" >/dev/null; then + fail "a menu-picked gif dies on the gif refusal, not the resolution case" \ + "$(cat "$TMPDIR/stderr")" +fi +grep -q '^menu-select:' "$calls" || + fail "a menu-picked gif run records its menu calls" "$(cat "$calls")" +if grep -q 'notification:' "$calls" || grep -q '^ffmpeg ' "$calls"; then + fail "a menu-picked gif refuses after menus but before notification" "$(cat "$calls")" +fi +pass "a menu-picked gif under --target refuses pre-notification" + +# Pictures refuse --target at the earliest point type is known -- before the +# format menu even -- with an empty call log. +status=0 +run_transcode "$TMPDIR/img.png" jpg medium --target 25M || status=$? +[[ $status -eq 1 ]] || + fail "--target on a picture exits 1" "exit=$status" +grep -F 'applies to videos only' "$TMPDIR/stderr" >/dev/null || + fail "--target on a picture reports the picture refusal" "$(cat "$TMPDIR/stderr")" +if [[ -s $calls ]]; then + fail "--target on a picture precedes every side effect" "$(cat "$calls")" +fi +pass "--target on a picture refuses before any menu or notification" + +# An unset resolution still prompts under --target -- the pick is the +# planner's ceiling, not a floor: a 4k pick on a 5M budget steps to 720p. +FAKE_PICK=4k FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 --target 5M +grep '^menu-select:' "$calls" | grep -F 'Select resolution' >/dev/null || + fail "an unset resolution still prompts under --target" "$(cat "$calls")" +grep -Fx "out=$TMPDIR/in-720p-5.0M.mp4" "$calls" >/dev/null || + fail "a 4k menu pick on a 5M budget steps down to -720p-5.0M" "$(cat "$calls")" +grep ' -pass 2 ' "$calls" | grep -F 'scale=-2:720' >/dev/null || + fail "a stepped-down menu pick encodes at 720p" "$(cat "$calls")" +pass "an unset resolution prompts as the planner ceiling under --target" + +# Below every floor: 4M and 2M over 60 s derive 330k and 69k -- under 720p's +# 400k floor -- so both refuse naming the computed achievable minimum +# ((400+192)*60*125 = 4,440,000 B, rendered ~4 MB). The probes ran, but no +# notification or encode did. +for t in 4M 2M; do + status=0 + FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 4k --target "$t" || status=$? + [[ $status -eq 1 ]] || + fail "--target $t below every floor exits 1" "exit=$status" + grep -F 'smallest achievable' "$TMPDIR/stderr" >/dev/null && + grep -F '4 MB' "$TMPDIR/stderr" >/dev/null || + fail "--target $t names the achievable minimum" "$(cat "$TMPDIR/stderr")" + grep -q '^ffprobe:' "$calls" || + fail "--target $t refused after probing" "$(cat "$calls")" + if grep -q 'notification:' "$calls" || grep -q '^ffmpeg ' "$calls"; then + fail "--target $t refuses before notification and encode" "$(cat "$calls")" + fi +done +pass "targets below every floor refuse naming the achievable minimum" + +# 1M over 60 s leaves no room even for the 192k audio stream (the derived +# -61k rides the floor loop to the same refusal) -- and no negative, zero, or +# -nan -b:v ever reaches ffmpeg argv on any accumulated line. +status=0 +FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 720p --target 1M || status=$? +[[ $status -eq 1 ]] || + fail "a target below the audio floor exits 1" "exit=$status" +grep -F 'smallest achievable' "$TMPDIR/stderr" >/dev/null || + fail "a target below the audio floor names the achievable minimum" "$(cat "$TMPDIR/stderr")" +if grep -q 'notification:' "$calls" || grep -q '^ffmpeg ' "$calls"; then + fail "a target below the audio floor refuses before the encode" "$(cat "$calls")" +fi +if grep -E -- '-b:v -|-b:v 0k|-nan' "$ffmpeg_calls" >/dev/null; then + fail "no ffmpeg line ever carries a negative, zero, or NaN bitrate" "$(cat "$ffmpeg_calls")" +fi +pass "a target too small for audio alone refuses; no bad -b:v reaches argv" + +# A target at or above the source refuses before even probing -- naming both +# sizes and pointing at the tier path for format-conversion intent. +status=0 +FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 1080p --target 200M || status=$? +[[ $status -eq 1 ]] || + fail "a target at or above the source exits 1" "exit=$status" +grep -F '200M' "$TMPDIR/stderr" >/dev/null && + grep -F '126M' "$TMPDIR/stderr" >/dev/null && + grep -F 'quality' "$TMPDIR/stderr" >/dev/null || + fail "a >=source refusal names both sizes and the tier path" "$(cat "$TMPDIR/stderr")" +if grep -q '^ffprobe:' "$calls"; then + fail "a >=source refusal precedes the probes" "$(cat "$calls")" +fi +pass "a target at or above the source refuses before probing" + +# A failed or degenerate duration probe refuses the same pre-notification way +# -- no qualitative fallback exists under --target. The probe line proves the +# run got past parsing and the >=source check. +status=0 +run_transcode "$TMPDIR/in.mov" mp4 1080p --target 25M || status=$? +[[ $status -eq 1 ]] || + fail "a failed duration probe exits 1" "exit=$status" +grep -F 'Cannot determine duration' "$TMPDIR/stderr" >/dev/null || + fail "a failed duration probe refuses" "$(cat "$TMPDIR/stderr")" +grep -q '^ffprobe:' "$calls" || + fail "a failed duration probe still records the probe" "$(cat "$calls")" +if grep -q 'notification:' "$calls" || grep -q '^ffmpeg ' "$calls"; then + fail "a failed duration probe refuses before notification" "$(cat "$calls")" +fi + +status=0 +FAKE_DURATION=N/A run_transcode "$TMPDIR/in.mov" mp4 1080p --target 25M || status=$? +[[ $status -eq 1 ]] || + fail "an N/A duration exits 1" "exit=$status" +grep -F 'Cannot determine duration' "$TMPDIR/stderr" >/dev/null || + fail "an N/A duration refuses" "$(cat "$TMPDIR/stderr")" +if grep -q 'notification:' "$calls" || grep -q '^ffmpeg ' "$calls"; then + fail "an N/A duration refuses before notification" "$(cat "$calls")" +fi + +status=0 +FAKE_DURATION=0 run_transcode "$TMPDIR/in.mov" mp4 1080p --target 25M || status=$? +[[ $status -eq 1 ]] || + fail "a zero duration exits 1" "exit=$status" +grep -F 'Cannot determine duration' "$TMPDIR/stderr" >/dev/null || + fail "a zero duration refuses" "$(cat "$TMPDIR/stderr")" +if grep -q 'notification:' "$calls" || grep -q '^ffmpeg ' "$calls"; then + fail "a zero duration refuses before notification" "$(cat "$calls")" +fi +pass "failed, N/A, and zero durations all refuse before any notification" + +# The probed audio term actually moves the math: a proven-audio-less source +# drops the 192k term, so 5M/60s at a 4k request derives 653k (not 461k). +FAKE_AUDIO=no FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 4k --target 5M +grep ' -pass 2 ' "$calls" | grep -F 'scale=-2:720' >/dev/null || + fail "a no-audio 5M target still steps to 720p" "$(cat "$calls")" +grep ' -pass 2 ' "$calls" | grep -F -- '-b:v 653k' >/dev/null || + fail "a no-audio 5M target derives -b:v 653k" "$(cat "$calls")" +pass "a proven-audio-less source drops the 192k term from the math" + +# The never-probes pin holds: a four-positional tier run never calls +# plan_target, so no ffprobe or menu lines appear. +run_transcode "$TMPDIR/in.mov" mp4 1080p medium +if grep -q '^ffprobe:' "$calls" || grep -q '^menu-select:' "$calls"; then + fail "a tier run still never probes or prompts" "$(cat "$calls")" +fi +pass "a four-positional tier run still never probes or prompts" + +# --- --target: overshoot retry and passlog hygiene -------------------------- +# One byte over triggers exactly one pass-2-only retry: 50M/60s derives 6341k, +# the first pass-2 output measures 52428801 B, so the retry bitrate is +# 6341*50000000/52428801 truncated to 6047k. The retry encodes to a passdir +# sibling and mv's onto $output, so the done toast and stat report the retry +# file's real ~4 MB. FAKE_OUT_BYTES feeds pass-2 #1, FAKE_OUT_BYTES2 #2; the +# knob-created files are this row's fixtures -- rm -f after asserting. +FAKE_OUT_BYTES=52428801 FAKE_OUT_BYTES2=4194304 FAKE_DURATION=60 \ + run_transcode "$TMPDIR/in.mov" mp4 720p --target 50M +[[ $(grep -c '^ffmpeg ' "$calls") -eq 3 ]] || + fail "an overshoot run makes exactly three ffmpeg calls" "$(cat "$calls")" +[[ $(grep -c '^ffmpeg .*retry\.mp4' "$calls") -eq 1 ]] || + fail "an overshoot triggers exactly one retry" "$(cat "$calls")" +grep '^ffmpeg .*retry\.mp4' "$calls" | grep -F -- '-b:v 6047k' >/dev/null || + fail "the retry tightens to -b:v 6047k" "$(cat "$calls")" +grep '^ffmpeg .*retry\.mp4' "$calls" | grep -F 'scale=-2:720' >/dev/null || + fail "the retry keeps the 720p resolution" "$(cat "$calls")" +grep '^ffmpeg .*retry\.mp4' "$calls" | grep -F -- '-c:v libx264 -preset fast' >/dev/null || + fail "the retry keeps the codec and preset" "$(cat "$calls")" +grep '^ffmpeg .*retry\.mp4' "$calls" | + grep -F -- '-c:a aac -b:a 192k -movflags +faststart' >/dev/null || + fail "the retry keeps the audio and faststart flags" "$(cat "$calls")" +[[ $(grep '^ffmpeg ' "$calls" | grep -o 'passlogfile [^ ]*' | sort -u | wc -l) -eq 1 ]] || + fail "all three calls share one passlogfile" "$(cat "$calls")" +[[ $(stat -c %s "$TMPDIR/in-720p-50M.mp4") -eq 4194304 ]] || + fail "the retry result lands on the output path" "$(stat -c %s "$TMPDIR/in-720p-50M.mp4")" +grep 'notification:' "$calls" | grep -F 'Transcoded to 720p mp4' | + grep -F '(4 MB)' >/dev/null || + fail "the done toast reports the retry's real size" "$(cat "$calls")" +grep '^ffmpeg ' "$calls" | head -n1 | + grep -o 'passlogfile [^ ]*' | grep -F "passlogfile $TMPDIR" >/dev/null || + fail "the passlog lives under the exported TMPDIR" "$(cat "$calls")" +if find "$TMPDIR" -name '*2pass*' -print -quit | grep -q .; then + fail "a successful retry leaves no passlog artifacts" \ + "$(find "$TMPDIR" -name '*2pass*')" +fi +rm -f "$TMPDIR/in-720p-50M.mp4" +pass "a byte-over target retries pass-2 once at the tightened bitrate" + +# At or under target: no retry at all. The boundary is inclusive -- exactly +# 90000000 B against a 90M target is a hit, and 79999999 B against 80M is a +# hit by one byte. +FAKE_OUT_BYTES=90000000 FAKE_DURATION=60 \ + run_transcode "$TMPDIR/in.mov" mp4 720p --target 90M +[[ $(grep -c '^ffmpeg ' "$calls") -eq 2 ]] || + fail "an exactly-at-target run makes two ffmpeg calls" "$(cat "$calls")" +grep 'notification:' "$calls" | grep -F 'Transcoded to 720p mp4' | + grep -F '(90 MB)' >/dev/null || + fail "an at-target run reports its real size" "$(cat "$calls")" +rm -f "$TMPDIR/in-720p-90M.mp4" + +FAKE_OUT_BYTES=79999999 FAKE_DURATION=60 \ + run_transcode "$TMPDIR/in.mov" mp4 720p --target 80M +[[ $(grep -c '^ffmpeg ' "$calls") -eq 2 ]] || + fail "a one-byte-under run makes two ffmpeg calls" "$(cat "$calls")" +grep 'notification:' "$calls" | grep -F 'Transcoded to 720p mp4' | + grep -F '(80 MB)' >/dev/null || + fail "an under-target run reports its real size" "$(cat "$calls")" +rm -f "$TMPDIR/in-720p-80M.mp4" +pass "an at-or-under target never retries" + +# A retry that lands over target still moves into place and the run stops -- +# never a second retry, and the honest overshot size is what gets reported. +FAKE_OUT_BYTES=62914561 FAKE_OUT_BYTES2=62914561 FAKE_DURATION=60 \ + run_transcode "$TMPDIR/in.mov" mp4 720p --target 60M +[[ $(grep -c '^ffmpeg ' "$calls") -eq 3 ]] || + fail "a still-over retry makes exactly three ffmpeg calls" "$(cat "$calls")" +[[ $(grep -c '^ffmpeg .*retry\.mp4' "$calls") -eq 1 ]] || + fail "a still-over result never triggers a second retry" "$(cat "$calls")" +grep 'notification:' "$calls" | grep -F 'Transcoded to 720p mp4' | + grep -F '(63 MB)' >/dev/null || + fail "a still-over retry reports the real overshot size" "$(cat "$calls")" +rm -f "$TMPDIR/in-720p-60M.mp4" +pass "an overshoot retries at most once" + +# A failed retry preserves the overshot-but-playable first output: no mv, the +# over-target file stays put, and the done toast still reports its real size. +FAKE_OUT_BYTES=73400321 FAKE_PASS2_RC="0 1" FAKE_DURATION=60 \ + run_transcode "$TMPDIR/in.mov" mp4 720p --target 70M +[[ $(grep -c '^ffmpeg .*retry\.mp4' "$calls") -eq 1 ]] || + fail "a failed retry still records its ffmpeg call" "$(cat "$calls")" +[[ -f $TMPDIR/in-720p-70M.mp4 ]] && + [[ $(stat -c %s "$TMPDIR/in-720p-70M.mp4") -eq 73400321 ]] || + fail "a failed retry preserves the first output" "$(ls -l "$TMPDIR")" +grep 'notification:' "$calls" | grep -F 'Transcoded to 720p mp4' | + grep -F '(73 MB)' >/dev/null || + fail "a failed retry still reports the real size" "$(cat "$calls")" +if find "$TMPDIR" -name '*2pass*' -print -quit | grep -q .; then + fail "a failed retry leaves no passlog artifacts" \ + "$(find "$TMPDIR" -name '*2pass*')" +fi +rm -f "$TMPDIR/in-720p-70M.mp4" +pass "a failed retry preserves the overshot first output" + +# A pass-1 failure aborts the run: exit 1, the start toast fired but the +# pass-2 replacement and done toast never did, and the EXIT trap still +# reaps the passdir. +status=0 +FAKE_PASS1_RC=1 FAKE_DURATION=60 \ + run_transcode "$TMPDIR/in.mov" mp4 720p --target 55M || status=$? +[[ $status -eq 1 ]] || + fail "a pass-1 failure exits 1" "exit=$status" +[[ $(grep -c '^ffmpeg ' "$calls") -eq 1 ]] || + fail "a pass-1 failure makes exactly one ffmpeg call" "$(cat "$calls")" +if grep -q ' -pass 2 ' "$calls"; then + fail "a pass-1 failure never reaches pass 2" "$(cat "$calls")" +fi +[[ $(grep -c 'notification:' "$calls") -eq 1 ]] || + fail "a pass-1 failure sends only the start toast" "$(cat "$calls")" +if find "$TMPDIR" -name '*2pass*' -print -quit | grep -q .; then + fail "a pass-1 failure leaves no passlog artifacts" \ + "$(find "$TMPDIR" -name '*2pass*')" +fi +pass "a pass-1 failure aborts before pass 2 with a clean passdir" + +# A pass-2 failure aborts after the replacement toast: exit 1, two ffmpeg +# calls, no retry, no done toast, passdir reaped. +status=0 +FAKE_PASS2_RC=1 FAKE_DURATION=60 \ + run_transcode "$TMPDIR/in.mov" mp4 720p --target 45M || status=$? +[[ $status -eq 1 ]] || + fail "a pass-2 failure exits 1" "exit=$status" +[[ $(grep -c '^ffmpeg ' "$calls") -eq 2 ]] || + fail "a pass-2 failure makes exactly two ffmpeg calls" "$(cat "$calls")" +[[ $(grep -c 'notification:' "$calls") -eq 2 ]] || + fail "a pass-2 failure sends the two pass toasts only" "$(cat "$calls")" +if grep 'notification:' "$calls" | grep -F 'Transcoded to' >/dev/null; then + fail "a pass-2 failure never sends the done toast" "$(cat "$calls")" +fi +if find "$TMPDIR" -name '*2pass*' -print -quit | grep -q .; then + fail "a pass-2 failure leaves no passlog artifacts" \ + "$(find "$TMPDIR" -name '*2pass*')" +fi +pass "a pass-2 failure aborts with no done toast and a clean passdir" + +# Dedupe composes with --target: a pre-existing in-1080p-25M.mp4 sends the +# target run to -2 -- the token joins the name before the dedupe counter. +touch "$TMPDIR/in-1080p-25M.mp4" +FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 1080p --target 25M +grep -Fx "out=$TMPDIR/in-1080p-25M-2.mp4" "$calls" >/dev/null || + fail "a colliding target output dedupes to -2" "$(cat "$calls")" +rm -f "$TMPDIR/in-1080p-25M.mp4" +pass "a --target output dedupes with the size token in place" + +# usage() documents the flag: --help renders it on stdout with status 0. +run_transcode --help +grep -F -- '--target' "$TMPDIR/stdout" >/dev/null || + fail "--help documents --target" "$(cat "$TMPDIR/stdout")" +pass "--help documents the --target flag" + +# --- Custom size… ------------------------------------------------------------- +# The mp4 menu carries a fourth, last row whose label doubles as the sentinel +# key: the pick returns "Custom size…\t", the strip hands the label to +# the sentinel branch, and the typed answer rides the exact --target path, so +# the ffmpeg argv and toast text are cmp-identical to `mp4 1080p --target 25M` +# modulo the per-run mktemp passdir that -passlogfile logs (sed-normalized on +# both greps). Rows after the first two runs read the saved copies only -- +# run_transcode truncates $calls and rewrites $TMPDIR/stderr on every +# invocation. +FAKE_INPUT=25M FAKE_DURATION=60 FAKE_PICK=$'Custom size…\tEnter a size like 25M' \ + run_transcode "$TMPDIR/in.mov" mp4 1080p +cp "$calls" "$TMPDIR/calls-custom" +cp "$TMPDIR/stderr" "$TMPDIR/stderr-custom" +grep '^menu-select: ' "$TMPDIR/calls-custom" | grep -F 'Select quality' >/dev/null || + fail "a Custom pick records the Select quality menu call" "$(cat "$TMPDIR/calls-custom")" +grep '^menu-select: ' "$TMPDIR/calls-custom" | + grep -F $'\tCustom size…\tEnter a size like 25M -- --default-index 1' >/dev/null || + fail "the Custom size row sits last with its subtext and medium stays pre-highlighted" \ + "$(cat "$TMPDIR/calls-custom")" +pass "the mp4 menu offers Custom size… last with a subtext and medium pre-highlighted" + +# The CLI twin of the parity pin: --target 25M never prompts and pays only the +# planner's two probes. +rm -f "$TMPDIR/in-1080p-25M.mp4" +FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 1080p --target 25M +grep -E '^(ffmpeg |notification:)' "$calls" | + sed 's|transcode-2pass\.[^/ ]*|transcode-2pass.X|' >"$TMPDIR/parity-cli" +[[ $(grep -c '^ffprobe:' "$calls") -eq 2 ]] || + fail "the --target baseline probes exactly twice" "$(cat "$calls")" +if grep -q '^menu-select:' "$calls" || grep -q '^menu-input:' "$calls"; then + fail "the --target baseline never prompts" "$(cat "$calls")" +fi +pass "a --target 25M run never prompts and probes exactly twice" + +# The Custom run's encode is byte-identical to the CLI run's -- same two-pass +# argv, same toast text; only the menu plumbing and the two menu-estimate +# probes differ upstream of it. +grep -E '^(ffmpeg |notification:)' "$TMPDIR/calls-custom" | + sed 's|transcode-2pass\.[^/ ]*|transcode-2pass.X|' >"$TMPDIR/parity-menu" +if ! cmp -s "$TMPDIR/parity-cli" "$TMPDIR/parity-menu"; then + fail "a Custom size pick produces the --target run's argv and toasts" \ + "$(diff -u "$TMPDIR/parity-cli" "$TMPDIR/parity-menu")" +fi +grep -Fx "out=$TMPDIR/in-1080p-25M.mp4" "$TMPDIR/calls-custom" >/dev/null || + fail "a Custom pick writes the -25M output name" "$(cat "$TMPDIR/calls-custom")" +[[ $(grep -c '^menu-input:' "$TMPDIR/calls-custom") -eq 1 ]] || + fail "a Custom pick fires the input prompt exactly once" "$(cat "$TMPDIR/calls-custom")" +grep -Fx 'menu-input: Target size (e.g. 25M)' "$TMPDIR/calls-custom" >/dev/null || + fail "the input prompt carries its locked text" "$(cat "$TMPDIR/calls-custom")" +[[ $(grep -c '^ffprobe:' "$TMPDIR/calls-custom") -eq 4 ]] || + fail "a Custom run probes 4 times (2 menu estimates + 2 planner)" \ + "$(cat "$TMPDIR/calls-custom")" +pass "a Custom size pick encodes byte-identically to --target 25M" + +# The sentinel label and the target: mint never reach the encoder, the output +# name, or the tier case -- the strip-plus-branch stays the only chokepoint. +if grep -E '^(ffmpeg |out=)' "$TMPDIR/calls-custom" | grep -F 'Custom' >/dev/null; then + fail "the Custom label never reaches an ffmpeg or out= line" \ + "$(cat "$TMPDIR/calls-custom")" +fi +if grep -E '^(ffmpeg |out=)' "$TMPDIR/calls-custom" | grep -F 'target:' >/dev/null; then + fail "the target: sentinel never reaches an ffmpeg or out= line" \ + "$(cat "$TMPDIR/calls-custom")" +fi +if grep -F 'Invalid video quality' "$TMPDIR/stderr-custom" >/dev/null; then + fail "the Custom pick never reaches the tier case" "$(cat "$TMPDIR/stderr-custom")" +fi +# A plain tier pick on the same menu never fires the input prompt at all. +FAKE_DURATION=60 FAKE_PICK=$'medium\tCRF 23 · ~24 MB' \ + run_transcode "$TMPDIR/in.mov" mp4 1080p +if grep -q '^menu-input:' "$calls"; then + fail "a tier pick never fires the input prompt" "$(cat "$calls")" +fi +pass "the sentinel label and the target: mint never leak to argv or the tier case" + +# An empty submit is exit-0-with-empty, NOT a cancel: it rides through +# parse_target_size like any garbage and buys exactly one hinted re-prompt. +FAKE_INPUT="" FAKE_INPUT2=25M FAKE_DURATION=60 \ + FAKE_PICK=$'Custom size…\tEnter a size like 25M' \ + run_transcode "$TMPDIR/in.mov" mp4 1080p +[[ $(grep -c '^menu-input:' "$calls") -eq 2 ]] || + fail "an empty submit re-prompts exactly once" "$(cat "$calls")" +grep '^menu-input:' "$calls" | tail -n1 | + grep -F 'Invalid size — e.g. 25M' >/dev/null || + fail "the re-prompt carries the Invalid size hint" "$(cat "$calls")" +grep -q '^ffmpeg ' "$calls" || + fail "an empty-then-valid run still encodes" "$(cat "$calls")" +pass "an empty submit re-prompts once with the hint, then accepts" + +# Unparseable input gets the same single hinted re-prompt. +FAKE_INPUT=bogus FAKE_INPUT2=25M FAKE_DURATION=60 \ + FAKE_PICK=$'Custom size…\tEnter a size like 25M' \ + run_transcode "$TMPDIR/in.mov" mp4 1080p +[[ $(grep -c '^menu-input:' "$calls") -eq 2 ]] || + fail "an unparseable answer re-prompts exactly once" "$(cat "$calls")" +grep '^menu-input:' "$calls" | tail -n1 | + grep -F 'Invalid size — e.g. 25M' >/dev/null || + fail "the unparseable re-prompt carries the hint" "$(cat "$calls")" +grep -F 'Invalid target size: bogus' "$TMPDIR/stderr" >/dev/null || + fail "the parse failure still reaches stderr" "$(cat "$TMPDIR/stderr")" +grep -q '^ffmpeg ' "$calls" || + fail "a garbage-then-valid run still encodes" "$(cat "$calls")" +pass "an unparseable answer re-prompts once with the hint, then accepts" + +# The for 1 2 bound caps the budget at one re-prompt: a second bad answer +# cancels like Esc -- nonzero, pre-notification, never a third prompt. +status=0 +FAKE_INPUT=bogus FAKE_INPUT2=still-bogus FAKE_DURATION=60 \ + FAKE_PICK=$'Custom size…\tEnter a size like 25M' \ + run_transcode "$TMPDIR/in.mov" mp4 1080p || status=$? +[[ $status -ne 0 ]] || + fail "two bad answers cancel the run" "exit=$status" +[[ $(grep -c '^menu-input:' "$calls") -eq 2 ]] || + fail "the re-prompt budget never exceeds two prompts" "$(cat "$calls")" +if grep -q 'notification:' "$calls" || grep -q '^ffmpeg ' "$calls"; then + fail "an exhausted re-prompt budget cancels before the notification" "$(cat "$calls")" +fi +pass "a second bad answer cancels like Esc, before the notification" + +# Esc at the re-prompt cancels through the same chain as Esc at the first. +status=0 +FAKE_INPUT=bogus FAKE_DURATION=60 \ + FAKE_PICK=$'Custom size…\tEnter a size like 25M' \ + run_transcode "$TMPDIR/in.mov" mp4 1080p || status=$? +[[ $status -ne 0 ]] || + fail "Esc at the re-prompt cancels the run" "exit=$status" +[[ $(grep -c '^menu-input:' "$calls") -eq 2 ]] || + fail "Esc at the re-prompt still records both prompts" "$(cat "$calls")" +grep '^menu-input:' "$calls" | tail -n1 | + grep -F 'Invalid size — e.g. 25M' >/dev/null || + fail "the Esc'd re-prompt carried the hint" "$(cat "$calls")" +if grep -q 'notification:' "$calls" || grep -q '^ffmpeg ' "$calls"; then + fail "Esc at the re-prompt cancels before the notification" "$(cat "$calls")" +fi +pass "Esc at the hinted re-prompt cancels before the notification" + +# Esc at the first input prompt aborts pre-notification like every sibling: +# the two estimate probes ran but the planner never did, and nothing else +# fired. +status=0 +FAKE_DURATION=60 FAKE_PICK=$'Custom size…\tEnter a size like 25M' \ + run_transcode "$TMPDIR/in.mov" mp4 1080p || status=$? +[[ $status -ne 0 ]] || + fail "Esc at the input prompt cancels the run" "exit=$status" +[[ $(grep -c '^menu-input:' "$calls") -eq 1 ]] || + fail "Esc at the first prompt records exactly one input call" "$(cat "$calls")" +[[ $(grep -c '^ffprobe:' "$calls") -eq 2 ]] || + fail "an input-prompt Esc aborts before the planner probes" "$(cat "$calls")" +if grep -q 'notification:' "$calls" || grep -q '^ffmpeg ' "$calls"; then + fail "Esc at the input prompt cancels before the notification" "$(cat "$calls")" +fi +pass "Esc at the first input prompt cancels before the notification" + +# D-01: a parseable-below-floor answer refuses byte-identically to the CLI -- +# the re-prompt budget wraps the parser only, never plan_target. +status=0 +FAKE_DURATION=60 run_transcode "$TMPDIR/in.mov" mp4 4k --target 4M || status=$? +[[ $status -ne 0 ]] || + fail "the --target 4M baseline refuses" "exit=$status" +cp "$TMPDIR/stderr" "$TMPDIR/stderr-cli-4M" +status=0 +FAKE_INPUT=4M FAKE_DURATION=60 FAKE_PICK=$'Custom size…\tEnter a size like 25M' \ + run_transcode "$TMPDIR/in.mov" mp4 4k || status=$? +[[ $status -ne 0 ]] || + fail "a below-floor Custom answer refuses" "exit=$status" +grep -F 'smallest achievable' "$TMPDIR/stderr" >/dev/null && + grep -F '4 MB' "$TMPDIR/stderr" >/dev/null || + fail "a below-floor Custom answer names the achievable minimum" "$(cat "$TMPDIR/stderr")" +if ! cmp -s "$TMPDIR/stderr-cli-4M" "$TMPDIR/stderr"; then + fail "the Custom refusal is byte-identical to --target 4M" \ + "$(diff -u "$TMPDIR/stderr-cli-4M" "$TMPDIR/stderr")" +fi +[[ $(grep -c '^menu-input:' "$calls") -eq 1 ]] || + fail "a planner refusal never spends a re-prompt" "$(cat "$calls")" +if grep -q 'notification:' "$calls" || grep -q '^ffmpeg ' "$calls"; then + fail "a below-floor Custom answer refuses before the notification" "$(cat "$calls")" +fi +pass "a below-floor Custom answer refuses byte-identically to --target 4M" + +# The accumulated ffmpeg argv across every run carries no overwrite flag -- +# dedupe and the passdir-sibling retry make -y and -n unnecessary. +if grep -E '(^|[[:space:]])-[yn]([[:space:]]|$)' "$ffmpeg_calls" >/dev/null; then + fail "no ffmpeg line ever carries -y or -n" "$(cat "$ffmpeg_calls")" +fi +pass "no ffmpeg line carries -y or -n"