#!/bin/bash
# Reading-speed check and fix, tests for the guide "subtitles too fast". Run: bash test-run.sh (needs python3, ffmpeg, curl)
set -u
cd "$(dirname "$0")"
UA="koldflux-hub-research/1.0 (+https://koldflux.com)"
echo "date: $(date -u +%F)  python: $(python3 --version 2>&1)  ffmpeg: $(ffmpeg -version | head -1)"
curl -s -A "$UA" -o source.srt "https://commons.wikimedia.org/w/index.php?title=TimedText:4_-_1_-_Lecture_2.1_Expressions_(19_59).webm.en.srt&action=raw"
echo "source.srt sha256: $(sha256sum source.srt | cut -c1-16) bytes: $(wc -c < source.srt)"
echo "== CONTROL (known answers: cue 1 = 20 chars in 1 s = 20.00 not flagged; cue 2 = 21.00 flagged; cue 3 = 39 chars in 2 s = 19.50 not flagged; cue 4 = tags removed, 21 chars in 1 s = 21.00 flagged)"
python3 check_cps.py control.srt 20
echo "== SOURCE at 20 and 17"
python3 check_cps.py source.srt 20 | head -1
python3 check_cps.py source.srt 17 | head -1
echo "== NAIVE SPLIT at limit 20 (text halved, time halved in proportion)"
python3 split_cps.py source.srt split.srt 20
python3 check_cps.py split.srt 20 | head -1
echo "== FIX at limit 20"
python3 fix_cps.py source.srt fixed-20.srt 20
python3 check_cps.py fixed-20.srt 20 | head -1
echo "== FIX at limit 20, extend only (no borrowing)"
python3 fix_cps.py source.srt ext-only.srt 20 0
python3 check_cps.py ext-only.srt 20 | head -1
echo "== FIX at limit 17"
python3 fix_cps.py source.srt fixed-17.srt 17
python3 check_cps.py fixed-17.srt 17 | head -1
echo "== TIMING AFTER FIX (20)"
python3 - <<PY
from srtlib import parse
a=parse(open("source.srt",encoding="utf-8-sig").read()); b=parse(open("fixed-20.srt").read())
d=[(y["e"]-x["e"]) for x,y in zip(a,b)]; s=[(y["s"]-x["s"]) for x,y in zip(a,b)]
print("cues",len(a),len(b),"end moved earlier on",sum(1 for v in d if v<0),"cues; end moved later on",sum(1 for v in d if v>0),"cues, max",max(d),"ms; start moved later on",sum(1 for v in s if v>0),"cues, max",max(s),"ms")
print("durations min/max after fix (ms):",min(y["e"]-y["s"] for y in b),max(y["e"]-y["s"] for y in b))
print("overlaps after fix:",sum(1 for i in range(1,len(b)) if b[i]["s"]<b[i-1]["e"]),"; min gap (ms):",min(b[i]["s"]-b[i-1]["e"] for i in range(1,len(b))))
print("source durations min/max (ms):",min(x["e"]-x["s"] for x in a),max(x["e"]-x["s"] for x in a),"; source min gap (ms):",min(a[i]["s"]-a[i-1]["e"] for i in range(1,len(a))),"; source cues over 7 s:",sum(1 for x in a if x["e"]-x["s"]>7000))
print("text identical:",[x["lines"] for x in a]==[y["lines"] for y in b])
PY
echo "== STILL OVER 20 AFTER FIX: characters to cut at the new duration"
python3 check_cps.py fixed-20.srt 20 --cut | tail -n +2 | head -40
python3 check_cps.py fixed-20.srt 20 --cut | python3 -c "
import sys,re,statistics
v=[int(m.group(1)) for l in sys.stdin for m in [re.search(r'cut (\d+) chars',l)] if m]
print('remaining cues',len(v),'chars to cut: min',min(v),'median',statistics.median(v),'max',max(v),'total',sum(v))"
echo "== EDGE (BOM, CRLF, tags, 0.5 s cue, gap-0 pair)"
python3 check_cps.py edge.srt 20; python3 fix_cps.py edge.srt edge-fixed.srt 20; python3 check_cps.py edge-fixed.srt 20
echo "== ffprobe and ffmpeg read the fixed file"
ffprobe -v error -show_entries stream=codec_name -of csv=p=0 fixed-20.srt; echo "exit=$?"
ffmpeg -v error -y -i fixed-20.srt fixed-20.vtt; echo "exit=$? vtt cues: $(grep -c -- '-->' fixed-20.vtt)"
