#!/bin/bash
# Koldflux: the comparison video on /learn/video-editing/editing-basics/slow-motion-video.
# Input: "Slow Motion Hand-Feeding a Tufted Titmouse" (Wikimedia Commons, CC0), the same
# 3 second excerpt at 5 s that test-run.sh measures. ffmpeg 6.1.1.
# Cropped to the bird and the hand (960x540 from x=600, y=330) so the motion is visible on a phone.
# Row 1: the excerpt at its own speed, played 4 times so it lasts as long as the slowed rows.
# Row 2: slowed 4x and exported at 30 fps, so three of every four frames repeat.
# Row 3: slowed 4x with frames synthesized by minterpolate, exported at 30 fps.
set -euo pipefail
IN=${1:-titmouse.webm}; OUT=${2:-slow-motion-compare.mp4}
F=/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf
W=800; H=450
lab() { echo "pad=${W}:$((H+56)):0:56:color=0x111111,drawtext=fontfile=$F:text='$1':fontcolor=white:fontsize=30:x=20:y=13"; }
ffmpeg -v error -y -ss 5 -t 3 -i "$IN" -filter_complex "
[0:v]crop=960:540:600:330,scale=${W}:${H},fps=30,split=3[a][b][c];
[a]loop=loop=3:size=90:start=0,setpts=N/30/TB,$(lab 'Original speed, played 4 times')[r1];
[b]setpts=4*PTS,fps=30,$(lab '4x slower, frames repeated')[r2];
[c]setpts=4*PTS,minterpolate=fps=30:mi_mode=mci,$(lab '4x slower, frames interpolated')[r3];
[r1][r2][r3]vstack=inputs=3,trim=duration=11.9,format=yuv420p[v]" \
  -map "[v]" -an -c:v libx264 -crf 27 -preset slow -movflags +faststart "$OUT"
ffmpeg -v error -y -ss 6 -i "$OUT" -frames:v 1 -c:v libwebp -quality 85 "${OUT%.mp4}-poster.webp"
ffprobe -v error -show_entries stream=width,height,r_frame_rate,nb_frames -show_entries format=duration,size -of default=noprint_wrappers=1 "$OUT"
