#!/usr/bin/env bash
# Video podcast file size per hour: test encodes (Koldflux, 2026-12-01).
# Inputs (free licence, real interviews, downloaded from Wikimedia Commons):
#   caruso-1080p.webm  "Interview with a Teacher - Glen Caruso", 1920x1080 30 fps VP9 + Opus, CC0 (Chase Bohannon)
#   kythnos-2160p.webm "Discussion in Kythnos", 3840x2160 50 fps VP8 + Opus, CC BY-SA 4.0 (AthanasiadisDim)
# Every output is a 60 s cut starting at 60 s (the length control is 300 s), H.264 (libx264, preset medium) + AAC-LC
# 48 kHz stereo in MP4 with faststart. The 4K clip is converted to 25 fps (every second frame dropped) so it sits in
# YouTube's standard frame rate row. Usage: bash test-run.sh <input folder> <output folder>
set -euo pipefail
IN="${1:?usage: test-run.sh <input folder> <output folder>}"
OUT="${2:?usage: test-run.sh <input folder> <output folder>}"
C="$IN/caruso-1080p.webm"; K="$IN/kythnos-2160p.webm"
for f in "$C" "$K"; do [ -s "$f" ] || { echo "missing or empty input: $f" >&2; exit 1; }; done
mkdir -p "$OUT"
command -v ffmpeg >/dev/null || { echo "ffmpeg not found" >&2; exit 1; }

# YouTube-style video settings from its upload page: High profile, 2 B-frames, closed GOP of half the frame rate.
yt_gop() { echo "-profile:v high -bf 2 -g $1 -keyint_min $1 -flags +cgop"; }

# two-pass VBR at a target bitrate: twopass <in> <out> <seconds> <vf> <gop> <video kbps> <audio kbps>
twopass() {
  local in="$1" out="$2" t="$3" vf="$4" gop="$5" vk="$6" ak="$7" log="$OUT/$(basename "$2" .mp4)-pass"
  # shellcheck disable=SC2046
  ffmpeg -v error -y -ss 60 -t "$t" -i "$in" -vf "$vf" -c:v libx264 -preset medium $(yt_gop "$gop") -b:v "${vk}k" \
    -pass 1 -passlogfile "$log" -an -f mp4 /dev/null
  # shellcheck disable=SC2046
  ffmpeg -v error -y -ss 60 -t "$t" -i "$in" -vf "$vf" -c:v libx264 -preset medium $(yt_gop "$gop") -b:v "${vk}k" \
    -pass 2 -passlogfile "$log" -c:a aac -b:a "${ak}k" -ar 48000 -ac 2 -movflags +faststart "$out"
  rm -f "$log"*
}
# constant bitrate (Spotify asks for CBR): cbr <in> <out> <vf> <gop> <video kbps> <audio kbps>
cbr() {
  ffmpeg -v error -y -ss 60 -t 60 -i "$1" -vf "$3" -c:v libx264 -preset medium -profile:v high -g "$4" \
    -b:v "${5}k" -minrate "${5}k" -maxrate "${5}k" -bufsize "${5}k" -x264-params nal-hrd=cbr \
    -c:a aac -b:a "${6}k" -ar 48000 -ac 2 -movflags +faststart "$2"
}
# quality target (CRF), YouTube-style GOP: crf <in> <out> <vf> <gop> <crf> <audio kbps>
crf() {
  # shellcheck disable=SC2046
  ffmpeg -v error -y -ss 60 -t 60 -i "$1" -vf "$3" -c:v libx264 -preset medium $(yt_gop "$4") -crf "$5" \
    -c:a aac -b:a "${6}k" -ar 48000 -ac 2 -movflags +faststart "$2"
}

V1080="scale=1920:1080,format=yuv420p"; V720="scale=1280:720,format=yuv420p"
V4K="fps=25,scale=3840:2160,format=yuv420p"; V4K1080="fps=25,scale=1920:1080,format=yuv420p"

# 1080p30 interview
twopass "$C" "$OUT/c1080-yt8000-a384.mp4" 60 "$V1080" 15 8000 384     # YouTube 1080p SDR standard rate + stereo audio
twopass "$C" "$OUT/c1080-yt8000-a128.mp4" 60 "$V1080" 15 8000 128     # same, only the audio bitrate changed
twopass "$C" "$OUT/c720-yt5000-a384.mp4" 60 "$V720" 15 5000 384       # YouTube 720p SDR standard rate
twopass "$C" "$OUT/c1080-yt8000-a384-300s.mp4" 300 "$V1080" 15 8000 384  # length control: 300 s instead of 60 s
cbr "$C" "$OUT/c1080-sp25000cbr-a128.mp4" "$V1080" 30 25000 128          # Spotify 1080p: 25 Mbit/s CBR, AAC 128 kbps, 1 keyframe/s
crf "$C" "$OUT/c1080-crf18-a384.mp4" "$V1080" 15 18 384
crf "$C" "$OUT/c1080-crf23-a384.mp4" "$V1080" 15 23 384

# 4K interview (25 fps)
twopass "$K" "$OUT/k2160-yt35000-a384.mp4" 60 "$V4K" 12 35000 384     # YouTube 4K SDR standard rate, low end
twopass "$K" "$OUT/k2160-yt45000-a384.mp4" 60 "$V4K" 12 45000 384     # YouTube 4K SDR standard rate, high end
cbr "$K" "$OUT/k2160-sp35000cbr-a128.mp4" "$V4K" 25 35000 128            # Spotify 4K: 35 Mbit/s CBR
crf "$K" "$OUT/k2160-crf18-a384.mp4" "$V4K" 12 18 384
crf "$K" "$OUT/k2160-crf23-a384.mp4" "$V4K" 12 23 384
crf "$K" "$OUT/k1080-crf18-a384.mp4" "$V4K1080" 12 18 384               # same 4K footage, only the resolution changed
crf "$K" "$OUT/k1080-crf23-a384.mp4" "$V4K1080" 12 23 384
echo "ENCODES-DONE"
