#!/bin/bash
# Koldflux: SRT vs VTT test, October 27, 2026. Needs ffmpeg, node and Playwright (Chromium + Firefox).
# Run in the folder that holds the sample files from this page:
#   sample.srt, sample.vtt (the same three captions; cue 3 of the VTT has position settings),
#   vtt-short-timestamps.vtt (no hours, no cue numbers), vtt-comma-timestamps.vtt (SRT-style commas),
#   vtt-missing-header.vtt (no WEBVTT line), srt-dot-timestamps.srt (VTT-style dots).
set -u
echo "ffmpeg: $(ffmpeg -version | head -1)"
echo; echo "=== 1. Browser <track> loading (Chromium and Firefox, headless, Playwright)"
ffmpeg -v error -y -f lavfi -i testsrc2=size=320x180:rate=30:duration=9 -pix_fmt yuv420p -c:v libx264 clip.mp4
printf '<!doctype html><html><body></body></html>' > index.html
node track-test.cjs "$PWD"
echo; echo "=== 2. Conversion with ffmpeg"
ffmpeg -v error -y -i sample.srt ffmpeg-from-srt.vtt; echo "--- sample.srt -> ffmpeg-from-srt.vtt (exit $?)"; cat ffmpeg-from-srt.vtt
ffmpeg -v error -y -i sample.vtt ffmpeg-from-vtt.srt; echo "--- sample.vtt -> ffmpeg-from-vtt.srt (exit $?)"; cat ffmpeg-from-vtt.srt
ffmpeg -v error -y -i vtt-comma-timestamps.vtt ffmpeg-from-comma.srt; echo "--- vtt-comma-timestamps.vtt -> srt (exit $?), size $(wc -c < ffmpeg-from-comma.srt) bytes"
ffmpeg -v error -y -i vtt-missing-header.vtt ffmpeg-from-noheader.srt; echo "--- vtt-missing-header.vtt -> srt (exit $?), cues $(grep -c -- '-->' ffmpeg-from-noheader.srt)"
echo; echo "=== 3. Koldflux tools on the live site (Chromium)"
for f in sample.srt; do echo "--- /tools/subtitles/srt-to-vtt  $f"; node tool-probe.cjs https://koldflux.com/tools/subtitles/srt-to-vtt "$PWD/$f"; done
for f in sample.vtt vtt-comma-timestamps.vtt; do echo "--- /tools/subtitles/vtt-to-srt  $f"; node tool-probe.cjs https://koldflux.com/tools/subtitles/vtt-to-srt "$PWD/$f"; done
for f in sample.srt sample.vtt vtt-comma-timestamps.vtt vtt-missing-header.vtt srt-dot-timestamps.srt; do echo "--- /tools/subtitles/subtitle-stats  $f"; node tool-probe.cjs https://koldflux.com/tools/subtitles/subtitle-stats "$PWD/$f"; done

echo; echo "=== 4. Repairing a VTT written with commas: change the separator on timing lines only"
printf "WEBVTT\n\n00:00:01,000 --> 00:00:03,500\nAt 10:30,500 people came, 1,000 more later.\n" > comma-text.vtt
FIX='s/^([0-9:]+),([0-9]{3}) --> ([0-9:]+),([0-9]{3})/\1.\2 --> \3.\4/'
sed -E "$FIX" vtt-comma-timestamps.vtt > fixed.vtt
sed -E "$FIX" comma-text.vtt > fixed-text.vtt
echo "--- fixed-text.vtt (caption text keeps its commas)"; cat fixed-text.vtt
diff <(grep -- "-->" fixed.vtt) <(grep -- "-->" sample.vtt) && echo "fixed.vtt timing lines are identical to sample.vtt"
FILES=fixed.vtt,fixed-text.vtt node track-test.cjs "$PWD"

echo; echo "=== 5. Checker re-run after the tool fix"
for f in sample.srt sample.vtt vtt-comma-timestamps.vtt vtt-missing-header.vtt srt-dot-timestamps.srt; do node tool-probe.cjs https://koldflux.com/tools/subtitles/subtitle-stats "$PWD/$f" | grep -E "result text|page errors"; done
