#!/bin/bash
# build_reel_v2.sh — Villarroel reel corregido
# Fixes: rotación correcta (autorotate), Roger hablando, Dilip trabajando, música ambient
# Secuencia 22.4s para encajar con el audio de voz

set -e
RAW="/root/instagram-insights/reels/villarroel/raw"
TMP="/root/instagram-insights/reels/villarroel/tmp_v2"
AUDIO_VOZ="/tmp/villarroel.mp3"
AUDIO_MUSIC="/tmp/musica_bg.mp3"
OUT="/root/instagram-insights/reels/villarroel/villarroel_v2.mp4"

mkdir -p "$TMP"

# Filtro de escala: autorotate (sin -noautorotate) + escala portrait sin barras negras
VF='scale=1080:1920:force_original_aspect_ratio=decrease,pad=1080:1920:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1,fps=30'

echo "=== PASO 1: Convertir clips ==="

echo "[1/8] IMG_2324 t=17 — Roger hablando (3s)..."
ffmpeg -y -ss 17 -t 3 -i "$RAW/IMG_2324 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg1.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg1.mp4 | cut -f1)"

echo "[2/8] IMG_2320 t=1 — Vista aérea calle (2s)..."
ffmpeg -y -ss 1 -t 2 -i "$RAW/IMG_2320 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg2.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg2.mp4 | cut -f1)"

echo "[3/8] IMG_2327 t=0 — Fachada Eixample (3s)..."
ffmpeg -y -ss 0 -t 3 -i "$RAW/IMG_2327 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg3.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg3.mp4 | cut -f1)"

echo "[4/8] IMG_2322 t=8 — Dilip trabajando (3s)..."
ffmpeg -y -ss 8 -t 3 -i "$RAW/IMG_2322 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg4.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg4.mp4 | cut -f1)"

echo "[5/8] IMG_2358 t=2 — Corredero + vertical (5s)..."
ffmpeg -y -ss 2 -t 5 -i "$RAW/IMG_2358 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg5.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg5.mp4 | cut -f1)"

echo "[6/8] IMG_2343 t=0 — Macro tejido microperforado (3s)..."
ffmpeg -y -ss 0 -t 3 -i "$RAW/IMG_2343 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg6.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg6.mp4 | cut -f1)"

echo "[7/8] IMG_2364 t=0 — Vista desde dentro (2s)..."
ffmpeg -y -ss 0 -t 2 -i "$RAW/IMG_2364 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg7.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg7.mp4 | cut -f1)"

echo "[8/8] IMG_2373 t=0 — Ático completo cierre (1.4s)..."
ffmpeg -y -ss 0 -t 1.4 -i "$RAW/IMG_2373 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg8.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg8.mp4 | cut -f1)"

echo ""
echo "=== PASO 2: Concat + audio ==="

cat > "$TMP/concat.txt" << EOF
file 'seg1.mp4'
file 'seg2.mp4'
file 'seg3.mp4'
file 'seg4.mp4'
file 'seg5.mp4'
file 'seg6.mp4'
file 'seg7.mp4'
file 'seg8.mp4'
EOF

# Mezclar: voz al 100% + música ambient al 25%, ajustar niveles
ffmpeg -y \
  -f concat -safe 0 -i "$TMP/concat.txt" \
  -i "$AUDIO_VOZ" \
  -i "$AUDIO_MUSIC" \
  -filter_complex "
    [1:a]volume=1.0[voz];
    [2:a]volume=0.25,atrim=0:22.4[musica];
    [voz][musica]amix=inputs=2:duration=first:dropout_transition=2[audio_out]
  " \
  -map 0:v -map "[audio_out]" \
  -c:v copy \
  -c:a aac -b:a 192k \
  -movflags +faststart \
  -shortest \
  "$OUT" 2>/dev/null

echo ""
echo "=== RESULTADO ==="
ls -lh "$OUT"
ffprobe -v quiet -show_entries stream=width,height,codec_name,codec_type -show_entries format=duration -of default=noprint_wrappers=1 "$OUT" 2>/dev/null | grep -v "^$"

echo ""
echo "Limpiando temporales..."
rm -rf "$TMP"
echo "✔ Listo: $OUT"
