#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
PROJECT_DIR=$(cd -- "$SCRIPT_DIR/.." && pwd)
AUDIO_FILE=${1:-$PROJECT_DIR/outputs/audio/monologue.mp3}
SUBTITLE_FILE=${2:-$PROJECT_DIR/outputs/subtitles/monologue.srt}
OUTPUT_FILE=${3:-$PROJECT_DIR/outputs/video/monologue.mp4}
SIZE=${VIDEO_SIZE:-1920x1080}
BG_COLOR=${VIDEO_BG_COLOR:-0x101418}
TITLE=${VIDEO_TITLE:-English Learning Monologue}
FONT=${VIDEO_FONT:-/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc}

mkdir -p "$(dirname "$OUTPUT_FILE")"

ffmpeg -y \
  -f lavfi -i "color=c=${BG_COLOR}:s=${SIZE}:r=30" \
  -i "$AUDIO_FILE" \
  -vf "drawtext=fontfile=${FONT}:text='${TITLE}':fontcolor=white:fontsize=64:x=(w-text_w)/2:y=110,drawtext=fontfile=${FONT}:text='Listen and follow the subtitles':fontcolor=#c7d0da:fontsize=30:x=(w-text_w)/2:y=190,subtitles='${SUBTITLE_FILE}'" \
  -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 192k -shortest "$OUTPUT_FILE"

echo "Saved video to $OUTPUT_FILE"
