# ffmpeg Recipes for YouTube Automation

## Audio Ducking

Lower background audio when narration plays (sidechain compression):

```bash
ffmpeg -stream_loop -1 -i bg.mp4 -i narration.mp3 \
  -filter_complex "[1:a]volume=1.0[narr];[0:a]volume=0.15[bg];[narr][bg]amix=inputs=2:duration=first:dropout_transition=2[a]" \
  -map 0:v -map "[a]" -shortest -c:v libx264 output.mp4
```

*Note: Simple amix drops background to 15% constant. True ducking needs `sidechaincompress` filter (ffmpeg >= 4.0).*

## Text Overlay (Subtitles/Script)

```bash
ffmpeg -i video.mp4 -vf \
  "drawtext=text='YOUR TEXT':fontcolor=white:fontsize=48:x=(w-text_w)/2:y=h-th-60:box=1:boxcolor=black@0.6:boxborderw=10" \
  -c:a copy output.mp4
```

## Concatenate Multiple TTS Segments

When narration is generated in parts:

```bash
# Create segments.txt:
# file 'part1.mp3'
# file 'part2.mp3'
# file 'part3.mp3'

ffmpeg -f concat -safe 0 -i segments.txt -c copy full_narration.mp3
```

## Trim Background Video to Exact Duration

```bash
# Get narration duration
DUR=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 narration.mp3)

# Trim + loop combo
ffmpeg -stream_loop -1 -i bg.mp4 -i narration.mp3 \
  -shortest -t $DUR -c:v libx264 output.mp4
```

## Fade In/Out

```bash
ffmpeg -stream_loop -1 -i bg.mp4 -i narration.mp3 -shortest \
  -vf "fade=t=in:st=0:d=2,fade=t=out:st=4:d=2" \
  -af "afade=t=in:st=0:d=2,afade=t=out:st=4:d=2" \
  -c:v libx264 output.mp4
```

Adjust `st` (start time in seconds) based on total duration minus fade length.
