#!/usr/bin/env bash
# Tests the ffmpeg commands on the Koldflux page "Why is TikTok cropping my video?" on real CC0 clips from Wikimedia Commons.
# Usage: bash test-run.sh <workdir that holds hay.webm and linden.webm>   (ffmpeg 6.1.1, ffprobe 6.1.1)
set -u
cd "${1:-.}"
echo "date: $(date -u +%FT%TZ)"; ffmpeg -version | head -1
probe() { ffprobe -v error -select_streams v:0 -show_entries stream=width,height,sample_aspect_ratio,display_aspect_ratio,codec_name,pix_fmt -of csv=p=0 "$1"; }
aud() { ffprobe -v error -select_streams a -show_entries stream=codec_name -of csv=p=0 "$1" | tr '\n' ' '; }
# cropdetect reports w:h:x:y; only height and y are used (the clip has dark left and right edges that make the detected width unreliable)
bars() { ffmpeg -v info -i "$1" -vf cropdetect=limit=24:round=2 -frames:v 40 -f null - 2>&1 | grep -o 'crop=[0-9:]*' | tail -1 | awk -F'[=:]' '{print "picture height " $3 ", top offset " $5}'; }
ssim() { ffmpeg -v info -i "$1" -i "$2" -lavfi ssim -f null - 2>&1 | grep -o 'All:[0-9.]*' | tail -1; }
yavg_top() { ffmpeg -v error -i "$1" -vf "crop=1080:100:0:0,signalstats,metadata=print:key=lavfi.signalstats.YAVG:file=/dev/stdout" -f null - | grep -o 'YAVG=[0-9.]*' | cut -d= -f2 | awk '{s+=$1;n++} END{printf "%.1f", s/n}'; }

echo; echo "## sources (CC0, Wikimedia Commons)"
echo "hay.webm   (Heuernte bei Wergzahna im September 2017, Cmuelle8): $(probe hay.webm) | audio: $(aud hay.webm)"
echo "linden.webm (Lipy na navsi, Pohled 111):                          $(probe linden.webm) | audio: $(aud linden.webm)"

echo; echo "## derived sources (made by us from hay.webm, 8 s from 00:05, H.264 + AAC)"
ffmpeg -y -v error -ss 5 -t 8 -i hay.webm -c:v libx264 -crf 18 -pix_fmt yuv420p -c:a aac -b:a 128k src-16x9.mp4
ffmpeg -y -v error -i src-16x9.mp4 -vf "crop=1080:1080" -c:v libx264 -crf 18 -pix_fmt yuv420p -c:a copy src-1x1.mp4
ffmpeg -y -v error -i src-16x9.mp4 -vf "crop=864:1080,scale=1080:1350" -c:v libx264 -crf 18 -pix_fmt yuv420p -c:a copy src-4x5.mp4
for s in 16x9 1x1 4x5; do echo "src-$s.mp4: $(probe src-$s.mp4) | audio: $(aud src-$s.mp4) | picture height on source: $(bars src-$s.mp4)"; done

CROP_OLD='crop=trunc(ih*9/32)*2:ih,scale=1080:1920'
CROP_NEW='scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920'
PAD='scale=1080:-2,pad=1080:1920:(ow-iw)/2:(oh-ih)/2'
BLUR='[0:v]scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920,boxblur=20[bg];[0:v]scale=1080:-2[fg];[bg][fg]overlay=(W-w)/2:(H-h)/2'

echo; echo "## page commands on each source: output size, SAR, DAR, audio, detected picture height and top offset (cropdetect)"
for s in 16x9 1x1 4x5; do
  ffmpeg -y -v error -i src-$s.mp4 -vf "$CROP_OLD" -c:a copy out-$s-crop-old.mp4
  ffmpeg -y -v error -i src-$s.mp4 -vf "$CROP_NEW" -c:a copy out-$s-crop-new.mp4
  ffmpeg -y -v error -i src-$s.mp4 -vf "$PAD" -c:a copy out-$s-pad.mp4
  ffmpeg -y -v error -i src-$s.mp4 -filter_complex "$BLUR" -c:a copy out-$s-blur.mp4
  for m in crop-old crop-new pad blur; do f=out-$s-$m.mp4; echo "$s $m: $(probe $f) | audio: $(aud $f) | $(bars $f)"; done
done

echo; echo "## stretch of the old center-crop command on a 16:9 source"
ffmpeg -y -v error -i src-16x9.mp4 -vf "crop=trunc(ih*9/32)*2:ih" -c:a copy crop-only.mp4
echo "crop-only size: $(probe crop-only.mp4)"

echo; echo "## which part of the picture each crop keeps: SSIM against a reference crop (608x1080 at x 656, scaled to 1080x1920)"
ffmpeg -y -v error -i src-16x9.mp4 -vf "crop=608:1080:656:0,scale=1080:1920:flags=lanczos" -an ref-center.mp4
ffmpeg -y -v error -i src-16x9.mp4 -vf "crop=608:1080:0:0,scale=1080:1920:flags=lanczos" -an ref-left.mp4
echo "CONTROL ref-center vs itself (must be 1.0): $(ssim ref-center.mp4 ref-center.mp4)"
echo "CONTROL ref-left vs ref-center (different region, must be clearly lower): $(ssim ref-left.mp4 ref-center.mp4)"
echo "old command vs ref-center: $(ssim out-16x9-crop-old.mp4 ref-center.mp4)"
echo "new command vs ref-center: $(ssim out-16x9-crop-new.mp4 ref-center.mp4)"

echo; echo "## top 100 rows brightness (YAVG, 16 = black in limited range): pad must be near 16, blur and crop must not be"
for m in pad blur crop-new; do echo "16x9 $m: $(yavg_top out-16x9-$m.mp4)"; done

echo; echo "## real file with a non-square pixel flag: linden.webm"
echo "source: $(probe linden.webm)"
ffmpeg -y -v error -i linden.webm -vf "$PAD" -c:a copy out-linden-pad-asis.mp4 2>&1 | tail -2
echo "pad command as written: $(probe out-linden-pad-asis.mp4) | audio: $(aud out-linden-pad-asis.mp4) | $(bars out-linden-pad-asis.mp4)"
ffmpeg -y -v error -i linden.webm -vf "scale=trunc(iw*sar/2)*2:ih,setsar=1,$PAD" -c:a aac -b:a 128k out-linden-pad-fixed.mp4
echo "square pixels first, AAC audio: $(probe out-linden-pad-fixed.mp4) | audio: $(aud out-linden-pad-fixed.mp4) | $(bars out-linden-pad-fixed.mp4)"
echo "DONE"
