#!/bin/bash
# FFmpeg video overlay wrapper
# Args: input output overlay_image [position] [start_time] [end_time]
# position: topleft, topright, bottomleft, bottomright, center (default: bottomright)
INPUT="$1"
OUTPUT="$2"
OVERLAY="$3"
POSITION="${4:-bottomright}"
START="${5:-}"
END="${6:-}"

case "$POSITION" in
  topleft)        POS="x=10:y=10" ;;
  topright)       POS="x=main_w-overlay_w-10:y=10" ;;
  bottomleft)     POS="x=10:y=main_h-overlay_h-10" ;;
  center)         POS="x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2" ;;
  *)              POS="x=main_w-overlay_w-10:y=main_h-overlay_h-10" ;; # bottomright
esac

# Build overlay filter with optional timing
TIMING=""
if [ -n "$START" ] && [ -n "$END" ]; then
  TIMING=":enable='gte(t,${START})*lte(t,${END})'"
elif [ -n "$START" ]; then
  TIMING=":enable='gte(t,${START})'"
fi

ffmpeg -y -i "$INPUT" -i "$OVERLAY" \
  -filter_complex "overlay=${POS}${TIMING}" \
  -c:v libx264 -crf 18 -preset slow -c:a copy \
  "$OUTPUT"
