#!/bin/bash
# Reruns the QR-on-video test and writes test-results.txt, commands.log and the two figures.
# Needs: ffmpeg 6+ with libx264, node 20+ with the packages qrcode, jsqr and playwright (QRLIB, JSQRLIB, PW name them if they are not installed
# here), python3 with opencv-python-headless (PY names that interpreter), curl.
# The full matrix (119 encodes, about 1.5 hours) runs only with RUN_MATRIX=1; without it the stored results.tsv is analysed.
cd "$(dirname "$0")" && S=$(pwd) && W=${WORK:-$(mktemp -d)} && PY=${PY:-python3} && export PY QRLIB JSQRLIB PW
cp -f *.cjs *.py *.sh "$W"/ 2>/dev/null; cp -f results.tsv "$W"/; cd "$W" || exit 2
[ -f hoverfly.webm ] || curl -sL -A "koldflux-research/1.0" -o hoverfly.webm https://upload.wikimedia.org/wikipedia/commons/9/94/Marmalade_Hoverfly_garden_31_7_2020_1b_4K_UHD.webm
{
echo "== date: $(date -u +%F) =="
echo "== $(ffmpeg -version | head -1) | node $(node --version) | python $($PY --version 2>&1) | opencv $($PY -c 'import cv2;print(cv2.__version__)') | jsqr $(node -p "require((process.env.JSQRLIB||'jsqr')+'/package.json').version" 2>/dev/null) | qrcode $(node -p "require((process.env.QRLIB||'qrcode')+'/package.json').version" 2>/dev/null) =="
echo "== clip: hoverfly.webm, $(stat -c %s hoverfly.webm 2>/dev/null || stat -f %z hoverfly.webm) bytes, sha256 $( (sha256sum hoverfly.webm 2>/dev/null || shasum -a 256 hoverfly.webm) | cut -c1-16) =="
echo "== 1. QR files from the same library and options as the Koldflux tool (gen-qr.cjs) =="
node gen-qr.cjs qr
echo "== 2. the live Koldflux QR tool in headless Chromium: PNG for each error level (tool-run.cjs) =="
mkdir -p toolout && node tool-run.cjs https://koldflux.com toolout
echo "== 3. tool PNG against library PNG, pixel by pixel (tool-compare.py) =="
$PY tool-compare.py .
if [ "${RUN_MATRIX:-0}" = 1 ]; then echo "== 4. matrix: overlay, x264 at 1.5 and 6 Mbps, decode every 5th frame (run-matrix.py) =="; $PY run-matrix.py "$W" > matrix.log; else echo "== 4. matrix not rerun; stored results.tsv analysed =="; fi
echo "== 5. tables (analyze.py) =="
$PY analyze.py results.tsv
echo "== 6. published commands on the test file and an edge input (mkcmd.sh) =="
bash mkcmd.sh "$W" && echo "commands.log written"
} 2>&1 | tee test-results.txt
mkdir -p enc; for w in 60 90 180; do f=enc/A-short_short-M_${w}_1.5M.mp4; [ -f $f ] || ffmpeg -y -v error -ss 3 -t 5 -i hoverfly.webm -i qr/short-M.png -filter_complex "[1:v]scale=$w:$w:flags=bicubic[q];[0:v][q]overlay=main_w-w-60:main_h-h-60,format=yuv420p" -an -c:v libx264 -preset medium -b:v 1500k -pix_fmt yuv420p $f; done
for w in 60 90 180; do ffmpeg -y -v error -i enc/A-short_short-M_${w}_1.5M.mp4 -vf "select=eq(n\,30),crop=400:400:1520:680" -frames:v 1 crop-$w.png 2>/dev/null; done
$PY figs.py results.tsv && node figshot.cjs fig-threshold.html fig-threshold.png 1000 && node figshot.cjs fig-frames.html fig-frames.png 560
cp -f test-results.txt commands.log results.tsv fig-*.png fig-*.html tool-screen.png "$S"/ 2>/dev/null; cp -f toolout/tool-screen.png "$S"/tool-screen.png 2>/dev/null
