Ffmpeg quality

From damionrashford/media-os · View interactive version

Ffmpeg quality is an AI agent skill from the GitHub repository damionrashford/media-os. Measure encode quality with ffmpeg reference-vs-distorted filters: libvmaf (Netflix VMAF), psnr, ssim, vmafmotion, identity, msad, blockdetect, blurdetect. Use when the user asks to measure VMAF, run VMAF scoring, compute PSNR, compute SSIM, compare encoded vs source quality, benchmark a codec, find

How to use this skill

Skill file

Ffmpeg Quality

Context: $ARGUMENTS

Reference-vs-distorted (full-reference) objective quality metrics with ffmpeg filters. VMAF is the gold standard for perceptual quality; PSNR and SSIM are older but universally understood. All full-reference filters require the two inputs to be pixel-aligned: same resolution, same frame rate, same pixel format, same length.

Quick start

When to use

Step 1 — Ensure alignment (prerequisite)

Full-reference metrics (VMAF, PSNR, SSIM) require both inputs to match exactly on:

  1. Resolution — scale both to a target res with scale=W:H:flags=bicubic.
  2. Frame rate — resample with fps=R (or minterpolate for fractional conversions).
  3. Pixel format — convert with format=yuv420p (or yuv422p10le, matching the model bit-depth).
  4. Length — pad the shorter with tpad, or truncate with -t.
  5. Pixel offset — if cropped, pre-crop or the metric will collapse (esp. PSNR, 1-pixel shift wrecks it).

Canonical prefilter (both upscaled to 1080p, 24 fps, yuv420p):

[0:v]scale=1920:1080:flags=bicubic,fps=24,format=yuv420p[dist];
[1:v]scale=1920:1080:flags=bicubic,fps=24,format=yuv420p[ref];

Check alignment first: ffprobe -v error -select_streams v:0 -show_entries stream=width,height,r_frame_rate,pix_fmt,nb_frames <file>.

Step 2 — Pick a metric and run

VMAF (preferred; perceptual, 0–100)

Default model (vmaf_v0.6.1) — 1080p desktop viewing:

ffmpeg -i distorted.mp4 -i reference.mp4 -lavfi \
  "[0:v]scale=1920:1080:flags=bicubic[dist];[1:v]scale=1920:1080:flags=bicubic[ref];[dist][ref]libvmaf=model=version=vmaf_v0.6.1:log_path=vmaf.json:log_fmt=json:n_threads=8" \
  -f null -

Order matters: [distorted][reference] — distorted first.

Model selection:

PSNR (simple dB; sensitive to any pixel shift)

ffmpeg -i distorted.mp4 -i reference.mp4 -lavfi "[0:v][1:v]psnr=stats_file=psnr.log" -f null -

Stats file per-frame: n:X mse_avg:Y mse_y:A mse_u:B mse_v:C psnr_avg:Z psnr_y:... psnr_u:... psnr_v:....

SSIM (structural similarity, 0–1)

ffmpeg -i distorted.mp4 -i reference.mp4 -lavfi "[0:v][1:v]ssim=stats_file=ssim.log" -f null -

Stats file per-frame: n:X Y:a U:b V:c All:d (dB).

Single-input QC (no reference)

ffmpeg -i in.mp4 -vf "blockdetect=period_min=3:period_max=24:planes=7" -f null -
ffmpeg -i in.mp4 -vf "blurdetect" -f null -

Both emit per-frame metadata; higher lavfi.block / lavfi.blur means more artifacts.

Other full-reference filters

Step 3 — Parse and aggregate

VMAF JSON summary (pooled mean):

jq '.pooled_metrics.vmaf.mean' vmaf.json
jq '.pooled_metrics.vmaf | {min, max, mean, harmonic_mean}' vmaf.json
jq '.frames[] | {n: .frameNum, vmaf: .metrics.vmaf}' vmaf.json | head

PSNR / SSIM averages print to stderr at the end:

[Parsed_psnr_0 @ ...] PSNR y:42.13 u:45.22 v:45.88 average:43.21
[Parsed_ssim_0 @ ...] SSIM Y:0.973 U:0.988 V:0.989 All:0.978 (16.54dB)

Rate-distortion sweep (use scripts/quality.py sweep, Step 4):

Step 4 — Interpret

VMAF (0–100):

PSNR (dB): content-dependent ceiling. Rough bands:

SSIM (0–1):

Per-frame dips matter more than the mean — sort frames ascending and look at the 1st percentile for worst-case scenes.

Available scripts

Examples:

uv run ${CLAUDE_SKILL_DIR}/scripts/quality.py vmaf --reference ref.mp4 --distorted enc.mp4 --model vmaf_v0.6.1 --threads 8 --log-path vmaf.json
uv run ${CLAUDE_SKILL_DIR}/scripts/quality.py psnr --reference ref.mp4 --distorted enc.mp4 --stats-file psnr.log
uv run ${CLAUDE_SKILL_DIR}/scripts/quality.py ssim --reference ref.mp4 --distorted enc.mp4 --stats-file ssim.log
uv run ${CLAUDE_SKILL_DIR}/scripts/quality.py sweep --reference ref.mp4 --crfs 18,22,26,30 --encoder libx264 --preset medium

Reference docs

Gotchas

Examples

Example 1: VMAF a 4K encode

ffmpeg -i encoded_4k.mp4 -i source_4k.mov -lavfi \
  "[0:v]scale=3840:2160:flags=bicubic,format=yuv420p[d];[1:v]scale=3840:2160:flags=bicubic,format=yuv420p[r];[d][r]libvmaf=model=version=vmaf_4k_v0.6.1:log_path=vmaf_4k.json:log_fmt=json:n_threads=16" \
  -f null -
jq '.pooled_metrics.vmaf.mean' vmaf_4k.json

Example 2: Find best CRF for VMAF >= 93

uv run ${CLAUDE_SKILL_DIR}/scripts/quality.py sweep --reference source.mov --crfs 18,20,22,24,26 --encoder libx264 --preset slow
# Review the printed table; pick the highest CRF whose VMAF mean still >= 93.

Example 3: Per-frame PSNR to spot worst frames

ffmpeg -i enc.mp4 -i ref.mov -lavfi "[0:v][1:v]psnr=stats_file=psnr.log" -f null -
sort -t: -k7 -n psnr.log | head -20   # 20 worst frames by psnr_avg

Troubleshooting

Error: `No such filter: 'libvmaf'`

Cause: ffmpeg not built with libvmaf support.

Solution: reinstall a full build (brew reinstall ffmpeg on macOS, or build with --enable-libvmaf). Verify with ffmpeg -filters | grep vmaf.

Error: `Could not parse framerate` / scores look absurd

Cause: resolution or framerate mismatch between inputs.

Solution: add explicit scale=W:H,fps=R,format=yuv420p to both inputs before the metric filter.

Error: VMAF value is NaN

Cause: clip too short (< 13 frames) or all-black frames confusing motion features.

Solution: score a longer range; trim out leading/trailing black with select='gt(scene\,0.001)' or preroll.

Error: `unable to load model`

Cause: older ffmpeg build expecting an external .json model.

Solution: download the model from the Netflix/vmaf repo and pass model=path=/abs/path/vmaf_v0.6.1.json.

PSNR is 20 dB lower than expected

Cause: sub-pixel or 1-pixel spatial offset between inputs.

Solution: verify ffprobe dimensions on both files; ensure no crop applied to one side; rerun with matched scale.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.