#!/usr/bin/env bash
# Dal video YouTube originale (1280x720, 39 s) ricava un loop di 14 s senza audio, sotto 1,5 MB.
set -euo pipefail
cd "$(dirname "$0")/.."
SRC="_assets_originali/video/hero_background_39s_H81_c2oSi0E.mp4"
OUT="public/video"
mkdir -p "$OUT"
START=4
LEN=14
ffmpeg -y -ss $START -t $LEN -i "$SRC" -an -vf "scale=1280:-2,fps=24" \
  -c:v libx264 -profile:v high -crf 31 -preset slow -pix_fmt yuv420p -movflags +faststart "$OUT/hero.mp4"
if ffmpeg -hide_banner -encoders 2>/dev/null | grep -q libsvtav1; then
  ffmpeg -y -ss $START -t $LEN -i "$SRC" -an -vf "scale=1280:-2,fps=24" -c:v libsvtav1 -crf 42 -preset 6 "$OUT/hero.webm"
else
  ffmpeg -y -ss $START -t $LEN -i "$SRC" -an -vf "scale=1280:-2,fps=24" -c:v libvpx-vp9 -crf 38 -b:v 0 -row-mt 1 "$OUT/hero.webm"
fi
ffmpeg -y -ss $START -i "$SRC" -frames:v 1 -vf "scale=1280:-2" -q:v 4 "$OUT/hero-poster.jpg"
ls -la "$OUT"
for f in "$OUT"/hero.mp4 "$OUT"/hero.webm; do
  [ "$(stat -f%z "$f")" -le 1572864 ] || { echo "✗ $f supera 1,5 MB"; exit 1; }
done
echo "✓ video ok"
