---
name: ffmpeg-contact-sheet
description: Detect scene cuts, extract lossless PNG opening and closing keyframes, and render tiled visual contact sheets from AI video clips using local ffmpeg and ffprobe for visual QA and I2V continuity.
---

# FFmpeg Scene Detection, Keyframe Extraction & Contact Sheet Protocol

Use these verified local FFmpeg patterns whenever inspecting generated video takes, extracting exact transition frames, or auditing multi-shot edits:

## 1. Build a Visual Inspection Contact Sheet (Single Image QA)
Sample frames evenly across a 5–10s clip into one crisp tiled PNG so a human or vision-capable agent can spot morphing, warped hands, or drift in one glance:
```bash
# Example: 2 fps across a 3s clip -> 6 tiles (3x2 grid) at 320px wide each
ffmpeg -y -i clip.mp4 \
  -vf "fps=2,scale=320:-2:flags=lanczos,tile=3x2:padding=4:margin=4:color=black" \
  -frames:v 1 contact_sheet.png
```
For longer 8–10s clips, use `fps=1,scale=360:-2:flags=lanczos,tile=4x3:padding=4:margin=4:color=black`.

## 2. Extract Exact Lossless Opening & Closing Frames (PNG)
Never JPEG-compress keyframes intended for Image-to-Video or First/Last Frame transitions:
```bash
# First frame (frame 0)
ffmpeg -y -i clip.mp4 -vf "select=eq(n\,0)" -vframes 1 first_frame.png

# Final frame (fast reverse-seek from EOF)
ffmpeg -y -sseof -0.15 -i clip.mp4 -update 1 -frames:v 1 last_frame.png
```

## 3. Detect Hard Scene Cuts Programmatically
Log timestamps where scene score crosses `0.35` (raise to `0.45` for fast action, lower to `0.25` for subtle studio cuts):
```bash
ffmpeg -i edit.mp4 -vf "select='gt(scene,0.35)',showinfo" -f null - 2>&1 \
  | grep "pts_time:"
```

## 4. Continuity Rule Warning
If extracting `last_frame.png` from an MP4 to extend a sequence, warn the user that chaining compressed MP4 frames more than 2 hops compounds VAE softening—prefer branching from pristine Level-0 PNG stills whenever character identity matters.
