Ffmpeg lens perspective

From damionrashford/media-os · View interactive version

Ffmpeg lens perspective is an AI agent skill from the GitHub repository damionrashford/media-os. Lens correction, perspective warp, and geometric transforms with ffmpeg: lenscorrection (barrel/pincushion), lensfun (post-processing lens DB), perspective (keystone fix), vignette (add/remove vignetting), shear, tiltandshift, rotate with interpolation. Use when the user asks to fix barrel or fishey

How to use this skill

Skill file

Ffmpeg Lens Perspective

Context: $ARGUMENTS

Quick start

When to use

Step 1 — Identify the distortion type

Preview with ffplay and inspect edges:

  1. Straight lines bow OUTWARD from center → barrel → lenscorrection with NEGATIVE k1.
  2. Straight lines bow INWARD toward center → pincushion → lenscorrection with POSITIVE k1.
  3. Verticals converge (trapezoid) → keystone → perspective (4-corner warp).
  4. Frame is rotated/skewed as a whole → rotate or shear.
  5. Dark corners → vignette (add or mode=backward to remove).
  6. You know camera + lens → lensfun (database-driven, most accurate; build-flag gated).

Step 2 — Pick filter and apply

Core recipes:

# Barrel correction (GoPro, fisheye, wide-phone). k1 negative = undistort barrel.
ffmpeg -i in.mp4 -vf "lenscorrection=k1=-0.3:k2=0" -c:a copy out.mp4

# Pincushion (telephoto). k1 positive.
ffmpeg -i in.mp4 -vf "lenscorrection=k1=0.3:k2=0" -c:a copy out.mp4

# Database-driven (needs --enable-liblensfun + lensfun DB).
ffmpeg -i in.mp4 -vf "lensfun=make=GoPro:model='HERO9 Black':lens_model='HERO9 Black':focal_length=3.0:mode=geometry" -c:a copy out.mp4

# 4-corner keystone fix. Corners = TL, TR, BL, BR of the SOURCE quadrilateral.
ffmpeg -i in.mp4 -vf "perspective=x0=100:y0=0:x1=1820:y1=10:x2=0:y2=1080:x3=1920:y3=1070:interpolation=linear" -c:a copy out.mp4

# Add cinematic vignette. angle is the cone half-angle (radians); smaller = harsher.
ffmpeg -i in.mp4 -vf "vignette=angle=PI/5" -c:a copy out.mp4

# Remove a vignette baked into the footage.
ffmpeg -i in.mp4 -vf "vignette=angle=PI/5:mode=backward" -c:a copy out.mp4

# Shear (skew). shx = x-shift per unit y (0.2 is strong).
ffmpeg -i in.mp4 -vf "shear=shx=0.2:shy=0" -c:a copy out.mp4

# Rotate by 15 degrees with auto-expanded canvas.
ffmpeg -i in.mp4 -vf "rotate=15*PI/180:ow=rotw(15*PI/180):oh=roth(15*PI/180):c=black" -c:a copy out.mp4

# Tilt + shift (rolling-shutter-style horizontal shift across frames).
ffmpeg -i in.mp4 -vf "tiltandshift=tilt=1:start=0:hold=30:pad=30" -c:a copy out.mp4

Step 3 — Tune params

Step 4 — Verify and (optionally) clean up edges

After any geometric warp, the frame may have black borders or transparent wedges. Verify with ffplay, then:

Example full chain (barrel-undistort a GoPro and crop the wobble):

ffmpeg -i gopro.mp4 \
  -vf "lenscorrection=k1=-0.25:k2=-0.02,crop=iw*0.92:ih*0.92,scale=1920:1080" \
  -c:v libx264 -crf 18 -pix_fmt yuv420p -c:a copy out.mp4

Available scripts

Workflow

# Barrel undistort
uv run ${CLAUDE_SKILL_DIR}/scripts/lens.py undistort-barrel --input in.mp4 --output out.mp4 --k1 -0.3

# Keystone fix (4 corners in pixel coordinates, TL;TR;BL;BR)
uv run ${CLAUDE_SKILL_DIR}/scripts/lens.py perspective --input in.mp4 --output out.mp4 \
  --corners "100,0;1820,10;0,1080;1920,1070"

# Rotate with auto-expanded canvas
uv run ${CLAUDE_SKILL_DIR}/scripts/lens.py rotate --input in.mp4 --output out.mp4 --degrees 15 --expand-canvas

Reference docs

Gotchas

Examples

Example 1: GoPro HERO9 barrel undistort

Input: gopro.mp4 shot in SuperView/wide mode, straight lines heavily bowed.

Steps:

ffmpeg -i gopro.mp4 \
  -vf "lenscorrection=k1=-0.28:k2=-0.03,crop=iw*0.92:ih*0.92" \
  -c:v libx264 -crf 18 -pix_fmt yuv420p -c:a copy out.mp4

Result: straight horizon, mild crop removes the curved black wedge at the edges.

Example 2: Architecture keystone fix

Input: 1920×1080 building shot, top of the building narrower than the bottom.

Steps: identify source corners where the rectangular building projects; map them to the output.

ffmpeg -i building.mp4 \
  -vf "perspective=x0=300:y0=0:x1=1620:y1=0:x2=0:y2=1080:x3=1920:y3=1080:interpolation=cubic" \
  -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a copy out.mp4

Result: verticals become parallel, building sits in an upright rectangle.

Example 3: Cinematic vignette

ffmpeg -i scene.mp4 -vf "vignette=angle=PI/6" -c:v libx264 -crf 18 -pix_fmt yuv420p -c:a copy graded.mp4

Example 4: Dutch angle (5° tilt) with expanded canvas

ffmpeg -i in.mp4 \
  -vf "rotate=5*PI/180:ow=rotw(5*PI/180):oh=roth(5*PI/180):c=black" \
  -c:v libx264 -crf 18 -pix_fmt yuv420p -c:a copy dutch.mp4

Troubleshooting

Error: `Option 'lensfun' not found`

Cause: ffmpeg was built without --enable-liblensfun.

Solution: rebuild (or brew reinstall ffmpeg --with-lensfun on systems that support it), or use lenscorrection + manual k1/k2 instead.

Error: `Unable to find camera/lens in database`

Cause: lensfun make/model/lens_model strings do not match any DB entry.

Solution: run lensfun-update-data, then lensfun-list and copy the exact strings.

Rotated output has clipped corners

Cause: rotate without ow/oh crops to input dimensions.

Solution: use rotate=A:ow=rotw(A):oh=roth(A).

Perspective output is mirrored or inverted

Cause: corners passed in wrong order (expected TL, TR, BL, BR).

Solution: reorder corners; or add sense=destination if your points describe output→source instead of source→output.

Barrel undistort still shows curved edges

Cause: k1 alone is insufficient; lens has higher-order radial distortion.

Solution: add a small k2 (~-0.05 to -0.1 same sign as k1) and iterate.

Vignette removal leaves a halo

Cause: angle too large (too soft); mode=backward divides by vignette function so an over-wide angle undercorrects.

Solution: tighten angle (e.g., PI/6 instead of PI/4) and re-run.

Output has green/black borders in MP4

Cause: yuva420p pixel format with alpha, MP4 players cannot show alpha.

Solution: append ,format=yuv420p at end of -vf chain, or add -pix_fmt yuv420p.

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.