#!/usr/bin/env bash
# OBS recording size test (koldflux.com, 2026-12-01).
# Builds OBS-like recordings with ffmpeg's libx264 (preset veryfast, CBR with filler data, which is what
# OBS Studio 33.0.3 sets for x264 CBR in plugins/obs-x264/obs-x264.c) and AAC audio, in MKV.
# OBS itself was NOT run: these files copy its documented x264 settings, they are not OBS captures.
# Input: Caminandes 3: Llamigos (Blender Foundation, CC BY 3.0, Wikimedia Commons), 1920x1080 24 fps VP8 + Vorbis.
# Usage: bash test-run.sh <llamigos.webm> <outdir>
set -euo pipefail
SRC="${1:?usage: test-run.sh <llamigos.webm> <outdir>}"
OUT="${2:?usage: test-run.sh <llamigos.webm> <outdir>}"
[ -s "$SRC" ] || { echo "input missing or empty: $SRC" >&2; exit 1; }
mkdir -p "$OUT"
FONT=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf
X264="-c:v libx264 -preset veryfast -pix_fmt yuv420p"
AAC="-c:a aac -b:a 160k -ar 48000 -ac 2"

cbr() { # name, kbps, fps, size, audio tracks, seconds, start
  local n=$1 k=$2 f=$3 s=$4 t=$5 d=$6 ss=$7 maps=""
  for i in $(seq 1 "$t"); do maps="$maps -map 1:a:0"; done
  ffmpeg -nostdin -v error -y -ss "$ss" -t "$d" -i "$SRC" -ss "$ss" -t "$d" -i "$SRC" -map 0:v:0 $maps \
    -vf "fps=$f,scale=$s" $X264 -b:v ${k}k -maxrate ${k}k -bufsize ${k}k -x264-params filler=1 \
    $AAC "$OUT/$n.mkv"
}

# A: bitrate ladder, 1080p30, one 160 kbps AAC track, 60 s (film 0:30-1:30)
for k in 2500 6000 12000 20000 50000; do cbr "a-cbr${k}-1080p30-1trk" $k 30 1920:1080 1 60 30; done
# B: same 6000 kbps, only frame size or frame rate changed
cbr b-cbr6000-1080p60-1trk 6000 60 1920:1080 1 60 30
cbr b-cbr6000-720p30-1trk  6000 30 1280:720  1 60 30
# C: same 6000 kbps 1080p30, 2 and 6 audio tracks (each a copy of the film's stereo mix at 160 kbps)
cbr c-cbr6000-1080p30-2trk 6000 30 1920:1080 2 60 30
cbr c-cbr6000-1080p30-6trk 6000 30 1920:1080 6 60 30
# D: length control: the whole film (150.1 s) at 6000 kbps, to check that bytes grow in proportion to time
cbr d-cbr6000-1080p30-1trk-full 6000 30 1920:1080 1 150.104 0

# E: content x rate control. Film vs a synthetic mostly-still screen (slide text + running clock), same audio.
#    CBR 6000 with filler vs CRF 23 and CRF 16 (the values OBS 33.0.3 Simple mode passes to x264 for
#    "High Quality" and "Indistinguishable" at 1080p; SimpleOutput.cpp CalcCRF).
ffmpeg -nostdin -v error -y -f lavfi -i "color=c=0x1e2430:s=1920x1080:r=30:d=60" \
  -vf "drawtext=fontfile=$FONT:text='Quarterly review - slide 3':fontsize=72:fontcolor=white:x=120:y=160,drawtext=fontfile=$FONT:text='Revenue by region':fontsize=48:fontcolor=0xc8d0e0:x=120:y=300,drawtext=fontfile=$FONT:text='%{pts\:hms}':fontsize=40:fontcolor=white:x=1560:y=980" \
  -c:v libx264 -preset veryslow -crf 0 -pix_fmt yuv444p "$OUT/screen-src.mkv"
for kind in film screen; do
  if [ $kind = film ]; then VIN=(-ss 30 -t 60 -i "$SRC"); VF="fps=30"; else VIN=(-i "$OUT/screen-src.mkv"); VF="format=yuv420p"; fi
  ffmpeg -nostdin -v error -y "${VIN[@]}" -ss 30 -t 60 -i "$SRC" -map 0:v:0 -map 1:a:0 -vf "$VF" $X264 \
    -b:v 6000k -maxrate 6000k -bufsize 6000k -x264-params filler=1 $AAC -t 60 "$OUT/e-$kind-cbr6000.mkv"
  for crf in 23 16; do
    ffmpeg -nostdin -v error -y "${VIN[@]}" -ss 30 -t 60 -i "$SRC" -map 0:v:0 -map 1:a:0 -vf "$VF" $X264 \
      -crf $crf $AAC -t 60 "$OUT/e-$kind-crf$crf.mkv"
  done
done
rm -f "$OUT/screen-src.mkv"

# F: lossless preset copy (OBS Simple mode "Lossless" = utvideo + PCM 16-bit in AVI), 5 s only to stay small
ffmpeg -nostdin -v error -y -ss 30 -t 5 -i "$SRC" -vf fps=30 -c:v utvideo -pix_fmt yuv420p -c:a pcm_s16le -ar 48000 -ac 2 "$OUT/f-lossless-utvideo-5s.avi"

# G: crash case. A 6000 kbps recording killed after ~20 s (like a power cut), MKV vs MP4.
for ext in mkv mp4; do
  timeout -s KILL 20 ffmpeg -nostdin -v error -y -re -ss 30 -t 60 -i "$SRC" -vf fps=30 $X264 \
    -b:v 6000k -maxrate 6000k -bufsize 6000k -x264-params filler=1 $AAC "$OUT/g-killed.$ext" || true
done
echo DONE
