#!/bin/zsh
# Part 2 final build: snaptoon recipe (prepare.py -> sfx.py), endcard concat, loudnorm -14.
# Run from /Users/sarthakrawool/Documents/Agency
set -e
cd /Users/sarthakrawool/Documents/Agency

ID=2026-12-07-the-argument-v2
D=products/restorebunny/reels/$ID
EDIT=$D/edit/${ID//-/_}_edit.mp4

echo "== 1. prepare (story-editor / Remotion)"
python3 tools/story-editor/prepare.py "$D" --accent FFE01B

echo "== 2. sfx mix (music_db 9, whisper gate)"
python3 tools/snaptoon/sfx.py "$D" --music-db 0 --qa --out "$D/edit/mix_sfx.mp4"

echo "== 3. concat endcard + loudnorm -14"
cat > "$D/edit/ffmpeg/join-final.txt" <<EOF
file '$PWD/$D/edit/mix_sfx.mp4'
file '$PWD/$D/edit/endcard.mp4'
EOF
ffmpeg -loglevel error -y -f concat -safe 0 -i "$D/edit/ffmpeg/join-final.txt" \
  -c:v libx264 -crf 17 -maxrate 9M -bufsize 18M -pix_fmt yuv420p -r 30 \
  -af loudnorm=I=-14:TP=-1.5:LRA=11 -c:a aac -b:a 192k -movflags +faststart \
  "$D/restorebunny_the_argument_v2_final.mp4"

echo "== done"
ffprobe -v error -show_entries format=duration,size -show_entries stream=codec_type,duration,nb_frames -of compact "$D/restorebunny_the_argument_v2_final.mp4"
