#!/usr/bin/env bash
# Koldflux test for the guide "SDH subtitles": a constructed two-voice scene (synthetic flite voices, synthetic sound effects,
# ffmpeg mandelbrot picture), a good SDH file written from the measured audio, and a bad copy with 7 injected faults.
# Run in an empty folder that holds this script, gen.py, sdh-lint.py, sdh-to-plain.py, audio-check.py, bad.srt, edge-mixed.srt, tool.cjs.
set -u
export PATH=$HOME/.nvm/versions/node/v24.21.0/bin:$PATH
LOG=test-results.txt; : > $LOG; CL=commands.log; : > $CL
say() { echo "$@" | tee -a $LOG; }
cmd() { # cmd <command...> : runs and logs
  say "\$ $*"; "$@" > .o 2>&1; local e=$?; tail -n ${TAIL:-12} .o | tee -a $LOG; say "exit=$e"; say; return 0; }
pub() { # pub <published text> <edge note> <command string run via bash -c>
  { echo "published: $1"; echo "run: $3"; bash -c "$3" > .o 2>&1; e=$?; tail -n 14 .o; echo "exit=$e"; echo "edge: $2"; echo; } >> $CL; }
say "=== 0. versions ($(date -u +%F)) ==="; ffmpeg -version | head -1 | tee -a $LOG; python3 --version | tee -a $LOG
say; say "=== 1. build the scene and the good SDH file ==="; python3 gen.py | tee -a $LOG; ffprobe -v error -show_entries stream=codec_type,codec_name,duration -of csv=p=0 demo.mp4 | tee -a $LOG
say; say "=== 2. sdh.srt (good file) ==="; cat sdh.srt | tee -a $LOG
say "=== 3. sdh-lint.py: control (good file must give no findings) and bad.srt (7 injected faults, so 7 findings) ==="
TAIL=20 cmd python3 sdh-lint.py sdh.srt; TAIL=20 cmd python3 sdh-lint.py bad.srt
say "=== 4. do the cue windows hold sound? (controls: two windows where nothing was placed) ==="; python3 audio-check.py sdh.srt | tee -a $LOG
say; say "=== 5. Koldflux subtitle checker (live, headless Chromium); sdh-short.srt = sdh.srt with the first cue ended at 1.500 s instead of 1.900 s ==="; python3 - <<PY
t=open("sdh.srt").read().replace("00:00:00,500 --> 00:00:01,900","00:00:00,500 --> 00:00:01,500"); open("sdh-short.srt","w").write(t)
PY
node tool.cjs sdh.srt sdh-short.srt bad.srt 2>&1 | tee -a $LOG
say; say "=== 6. published commands, each run on the test file and on one edge input (details in commands.log) ==="
pub "python3 sdh-lint.py sdh.srt" "bad.srt with 7 injected faults: findings printed, exit 1" "python3 sdh-lint.py sdh.srt; python3 sdh-lint.py bad.srt"
pub "ffmpeg -i sdh.srt sdh.vtt" "edge-mixed.srt: UTF-8 BOM, CRLF line endings, a music note and a curly apostrophe" "ffmpeg -y -v error -i sdh.srt sdh.vtt; ffmpeg -y -v error -i edge-mixed.srt edge.vtt; head -8 sdh.vtt; echo ---; cat edge.vtt"
pub 'ffmpeg -i demo.mp4 -vf "subtitles=sdh.srt" -c:a copy burned.mp4' "demo-noaudio.mp4 (same picture, audio removed with -an): -c:a copy with no audio stream" "ffmpeg -y -v error -an -i demo.mp4 -c:v copy demo-noaudio.mp4; ffmpeg -y -v error -i demo.mp4 -vf 'subtitles=sdh.srt' -c:a copy burned.mp4; ffmpeg -y -v error -i demo-noaudio.mp4 -vf 'subtitles=sdh.srt' -c:a copy burned-noaudio.mp4; for f in burned.mp4 burned-noaudio.mp4; do echo \$f; ffprobe -v error -show_entries stream=codec_type,codec_name -of csv=p=0 \$f; done"
pub 'ffmpeg -i demo.mp4 -i sdh.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:s:0 language=eng -disposition:s:0 +hearing_impaired sdh-soft.mp4' "same command into .mkv with -c:s srt, and a demo without audio (-map 0 has no audio to carry)" "ffmpeg -y -v error -i demo.mp4 -i sdh.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:s:0 language=eng -disposition:s:0 +hearing_impaired sdh-soft.mp4; ffmpeg -y -v error -i demo.mp4 -i sdh.srt -map 0 -map 1 -c copy -c:s srt -metadata:s:s:0 language=eng -disposition:s:0 +hearing_impaired sdh-soft.mkv; ffmpeg -y -v error -i demo-noaudio.mp4 -i sdh.srt -map 0 -map 1 -c copy -c:s mov_text -disposition:s:0 +hearing_impaired soft-noaudio.mp4; for f in sdh-soft.mp4 sdh-soft.mkv soft-noaudio.mp4; do echo \$f; ffprobe -v error -show_entries stream=index,codec_type,codec_name:stream_disposition=hearing_impaired:stream_tags=language -of compact=p=0 \$f; done"
pub "python3 sdh-to-plain.py sdh.srt plain.srt" "edge-mixed.srt: a cue with a sound effect and dialogue, a cue that is only a sound effect, a sung line with an inline label" "python3 sdh-to-plain.py sdh.srt plain.srt; python3 sdh-to-plain.py edge-mixed.srt edge-plain.srt; cat edge-plain.srt"
cat $CL | tee -a $LOG >/dev/null
say "=== 7. frame from burned.mp4 at 9.5 s (two-speaker cue) ==="; cmd ffmpeg -y -v error -ss 9.5 -i burned.mp4 -frames:v 1 -q:v 3 caption-frame.jpg
say "done $(date -u +%FT%TZ)"
