#!/bin/bash
# Runs every command published on the guide on the test file and on edge inputs; writes commands.log.
# Usage: mkcmd.sh <dir with out/cbr-6000.flv> ; needs ffmpeg 6.x, ffprobe, awk
D=$1; L=$D/commands.log; : > $L; cd $D; mkdir -p edge
C0='ffprobe -v error -show_entries format=duration,size,bit_rate -of default=nw=1 recording.flv'
C1='ffprobe -v error -show_entries packet=dts_time,size -of csv=p=0 recording.flv | awk -F, '"'"'$1 != "N/A" {v = $1 + 0; if (!seen || v < f) f = v; seen = 1; k = int(v); s[k] += $2*8/1e6; if (k > mx) mx = k} END {if (!seen) {print "no packets read: check the file name and that it is a video"; exit 1}; a = int(f); if (f - a > 0.1) a++; n = mx - a; for (i = a; i < mx; i++) {t += s[i]; if (s[i] > m) m = s[i]}; if (n >= 10) printf "whole seconds %d, average %.2f Mbps, busiest second %.2f Mbps\n", n, t/n, m; else print "recording too short: need at least 10 seconds"}'"'"
C2='ffmpeg -ss 5 -t 60 -i source.webm -vf scale=1920:1080:flags=lanczos,fps=60 -c:v libx264 -preset veryfast -b:v 6000k -minrate 6000k -maxrate 6000k -bufsize 12000k -x264-params nal-hrd=cbr:force-cfr=1 -g 120 -keyint_min 120 -pix_fmt yuv420p -c:a aac -b:a 128k -ar 44100 -f flv cbr-6000.flv'
run() { # name cmd file
  local cmd="${2//recording.flv/$3}"; echo "published: $2" >> $L; echo "as run: $cmd" >> $L
  out=$(bash -c "$cmd" 2>&1); echo "exit=$?" >> $L; echo "$out" | sed 's/^/  | /' >> $L; echo "edge: $1" >> $L; echo >> $L; }
# edge inputs, all made from the test file
ffmpeg -v error -y -i out/cbr-6000.flv -c copy -f mp4 edge/remux.mp4
ffmpeg -v error -y -i out/cbr-6000.flv -t 2 -c copy edge/short2s.flv
ffmpeg -v error -y -i out/cbr-6000.flv -t 20 -an -c:v copy -f matroska edge/noaudio.mkv
ffmpeg -v error -y -i out/cbr-6000.flv -t 20 -vn -c:a copy edge/audioonly.m4a
ffmpeg -v error -y -i out/cbr-6000.flv -c copy -output_ts_offset 100 edge/offset100.mkv
ffmpeg -v error -y -i out/cbr-6000.flv -ss 10 -t 20 -c copy edge/cut-from-10s.ts
ffmpeg -v error -y -i out/cbr-6000.flv -t 3.5 -c copy edge/short3-5s.flv
ffmpeg -v error -y -i out/cbr-6000.flv -t 12 -c copy edge/clip12s.flv
: > edge/empty.flv
ffmpeg -v error -y -i in/bbb-400.webm -t 10 -vf scale=1280:720,fps=24 -an -c:v libx264 -preset veryfast edge/src-720p24-silent.mkv
for c in C0 C1; do
  run "test file (60 s FLV, 6000 kbps CBR + 128 kbps AAC)" "${!c}" out/cbr-6000.flv
  run "same stream remuxed to MP4 (OBS-style container, B-frames)" "${!c}" edge/remux.mp4
  run "video only, Matroska, 20 s" "${!c}" edge/noaudio.mkv
  run "2-second clip (too short)" "${!c}" edge/short2s.flv
  run "3.5-second clip (too short; the old version accepted it)" "${!c}" edge/short3-5s.flv
  run "12-second clip (accepted)" "${!c}" edge/clip12s.flv
  run "timestamps start at 100 s (output_ts_offset, 60 s of video)" "${!c}" edge/offset100.mkv
  run "20 s cut from second 10 with stream copy (does not start at 0)" "${!c}" edge/cut-from-10s.ts
  run "empty file" "${!c}" edge/empty.flv
  run "audio only (no video packets)" "${!c}" edge/audioonly.m4a
  run "file that does not exist" "${!c}" edge/missing.flv
done
# the encode: published command on the real source, then on a short silent 720p24 source
runenc() { # name source
  local cmd="${C2//source.webm/$2}"; cmd="${cmd//cbr-6000.flv/edge/enc-test.flv}"
  echo "published: $C2" >> $L; echo "as run: $cmd" >> $L
  rm -f edge/enc-test.flv; bash -c "$cmd" > /dev/null 2>&1; echo "exit=$?" >> $L
  echo "  | video width,height,fps / duration,size: $(ffprobe -v error -select_streams v:0 -show_entries stream=width,height,r_frame_rate:format=duration,size -of csv=p=0 edge/enc-test.flv | tr '\n' ' ')" >> $L
  echo "edge: $1" >> $L; echo >> $L; }
runenc "real source in/bbb-400.webm (4K 60 fps film), 60 s output" in/bbb-400.webm
runenc "10 s 1280x720 24 fps source without audio: output is 5 s (-ss 5 skips half), still 1080p60, no audio track" edge/src-720p24-silent.mkv
