#!/bin/bash
# Runs every command printed on the page, on the test file and on one edge input. Writes commands.log.
cd "$(dirname "$0")"; PY=./venv/bin/python; L=commands.log; : > $L
mkdir -p cmd; rm -f cmd/*.png cmd/output*.mp4 cmd/*.ass
ffmpeg -hide_banner -loglevel error -y -f lavfi -i color=c=0x1c2b3a:s=1280x720:r=30:d=6 -f lavfi -i sine=frequency=440:duration=6 -c:v libx264 -pix_fmt yuv420p -c:a aac -shortest cmd/input.mp4
ffmpeg -hide_banner -loglevel error -y -f lavfi -i color=c=0x3a2b1c:s=720x1280:r=30:d=4 -c:v libx264 -pix_fmt yuv420p cmd/input-edge.mp4
mkdir -p cmd/fonts; cp fonts/*.ttf cmd/fonts/; rm -f cmd/fonts/Inter-Bold.ttf
cp captions.srt cmd/captions.srt
printf '\xef\xbb\xbf1\r\n00:00:00,000 --> 00:00:03,000\r\nआज १२३ ॐ नमस्ते।\r\nहिन्दी कैप्शन\r\n\r\n' > cmd/captions-edge.srt
cd cmd; cp ../check-font.py .
log(){ echo "$1" >> ../$L; }
run(){ # published | actual | edge-note
  log "published: $1"; log "run: $2"; eval "$2" >out.txt 2>&1; c=$?; log "exit=$c"; [ -n "$3" ] && log "edge: $3"; sed 's/^/  | /' out.txt | tail -4 >> ../$L; log ""; }
CHK='python3 check-font.py Mukta-Regular.ttf captions.srt'
run "$CHK" "../$PY check-font.py fonts/Mukta-Regular.ttf captions.srt" ""
run "$CHK" "../$PY check-font.py fonts/Mukta-Regular.ttf captions-edge.srt" "BOM + CRLF SRT with Devanagari digits, danda and OM sign"
run "$CHK" "../$PY check-font.py ../fonts/Inter-Bold.ttf captions.srt" "Latin-only font: exit 1 is the expected answer"
FC='fc-scan --format "%{family}\n" Mukta-Regular.ttf'
run "$FC" 'fc-scan --format "%{family}\n" fonts/Mukta-Regular.ttf' ""
run "$FC" 'fc-scan --format "%{family}\n" fonts/TiroDevanagariHindi-Regular.ttf' "family name with spaces"
run "$FC" 'fc-scan --format "%{family}\n" fonts/Baloo2.ttf' "variable font lists named instances, one family name repeated"
S1='ffmpeg -i captions.srt captions.ass'
run "$S1" 'ffmpeg -hide_banner -loglevel error -y -i captions.srt captions.ass' ""
run "$S1" 'ffmpeg -hide_banner -loglevel error -y -i captions-edge.srt captions-edge.ass' "BOM + CRLF SRT converts cleanly"
S2="sed 's/^Style: Default,[^,]*,[0-9]*,/Style: Default,Mukta,24,/' captions.ass > styled.ass"
run "$S2" "$S2" ""
run "$S2" "sed 's/^Style: Default,[^,]*,[0-9]*,/Style: Default,Tiro Devanagari Hindi,24,/' captions-edge.ass > styled-edge.ass" "family name with spaces"
S3='ffmpeg -i input.mp4 -vf "ass=styled.ass:fontsdir=fonts" -c:a copy output.mp4'
run "$S3" 'ffmpeg -hide_banner -loglevel error -y -i input.mp4 -vf "ass=styled.ass:fontsdir=fonts" -c:a copy output.mp4' ""
run "$S3" 'ffmpeg -hide_banner -loglevel error -y -i input-edge.mp4 -vf "ass=styled-edge.ass:fontsdir=fonts" -c:a copy output-edge.mp4' "portrait 720x1280 input with no audio stream, BOM+CRLF SRT, family name with spaces, fonts folder holding 5 families"
S4='ffmpeg -i input.mp4 -vf "ass=styled.ass:fontsdir=fonts" -frames:v 1 -ss 1 test-frame.png'
run "$S4" 'ffmpeg -hide_banner -loglevel error -y -i input.mp4 -vf "ass=styled.ass:fontsdir=fonts" -ss 1 -frames:v 1 test-frame.png' ""
run "$S4" 'ffmpeg -hide_banner -loglevel error -y -i input-edge.mp4 -vf "ass=styled-edge.ass:fontsdir=fonts" -ss 1 -frames:v 1 test-frame-edge.png' "portrait input, BOM+CRLF SRT"
S5='ffmpeg -loglevel verbose -i input.mp4 -vf "ass=styled.ass:fontsdir=fonts" -frames:v 1 -ss 1 test-frame.png 2>&1 | grep -E "fontselect:|Glyph"'
run "$S5" 'ffmpeg -hide_banner -loglevel verbose -y -i input.mp4 -vf "ass=styled.ass:fontsdir=fonts" -ss 1 -frames:v 1 -update 1 test-frame.png 2>&1 | grep -E "fontselect:|Glyph"' ""
run "$S5" 'ffmpeg -hide_banner -loglevel verbose -y -i input.mp4 -vf "ass=styled.ass" -ss 1 -frames:v 1 -update 1 test-frame-nodir.png 2>&1 | grep -E "fontselect:|Glyph"' "fonts folder left out: the log shows the fallback to Noto Sans and Noto Sans Devanagari on this server"
run "ffmpeg -i input.mp4 -vf \"subtitles=styled.ass:fontsdir=fonts\" ... (control, broken conjuncts)" 'ffmpeg -hide_banner -loglevel error -y -i input.mp4 -vf "subtitles=styled.ass:fontsdir=fonts" -ss 1 -frames:v 1 test-frame-subtitles-filter.png' "same .ass through the subtitles filter: exit 0 but conjuncts are drawn broken, see the image"
log "== streams (sound kept?)"
for f in input.mp4 output.mp4 output-edge.mp4; do log "$f: $(ffprobe -v error -show_entries stream=codec_type,codec_name,width,height,duration -of csv=p=0 $f | tr '\n' ' ')"; done
