Ffmpeg Lens Perspective
Context: $ARGUMENTS
Quick start
- Undistort GoPro/fisheye barrel: → Step 1 → Step 2 (
lenscorrection=k1=-0.3) - Pincushion (telephoto) correction: → Step 2 (
lenscorrection=k1=0.3) - 4-corner keystone/perspective fix: → Step 2 (
perspective=...) - Add cinematic vignette: → Step 2 (
vignette=angle=PI/5) - Remove vignette: → Step 2 (
vignette=angle=PI/5:mode=backward) - Rotate by arbitrary degrees: → Step 2 (
rotate=A:ow=rotw(A):oh=roth(A)) - Shear (skew) correction: → Step 2 (
shear=shx=0.2:shy=0) - Camera+lens-aware correction: → Step 2 (
lensfun=...)
When to use
- Footage from action cams, fisheye, wide-angle phone lenses exhibits barrel distortion (straight lines bow outward).
- Telephoto lenses show pincushion distortion (lines bow inward).
- Architectural / document shots have keystone (trapezoid) distortion from off-axis shooting.
- You want to add or remove a lens vignette (dark corners).
- You need an arbitrary-angle rotation with a proper expanded canvas.
- You need to un-shear a tilted scan or sheared frame.
- Use
ffmpeg-video-filterinstead for simple 90/180/270 rotations (transpose) or axis flips. - Use
ffmpeg-360-3d(v360) instead for full 360/equirectangular reprojection.
Step 1 — Identify the distortion type
Preview with ffplay and inspect edges:
- Straight lines bow OUTWARD from center → barrel →
lenscorrectionwith NEGATIVE k1. - Straight lines bow INWARD toward center → pincushion →
lenscorrectionwith POSITIVE k1. - Verticals converge (trapezoid) → keystone →
perspective(4-corner warp). - Frame is rotated/skewed as a whole → rotate or shear.
- Dark corners → vignette (add or
mode=backwardto remove). - 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
lenscorrectionk1/k2: RADIAL coefficients. Typical k1 magnitude 0.05-0.5. Very lens-dependent. Start with k1 alone; add small k2 (~0.05) only if edges still curve after k1 is tuned.lenscorrectioncenter: defaults tocx=0.5:cy=0.5(normalized). Override for off-center sensors (rare; anamorphic or cropped sensors).perspective: 4 corners are the SOURCE quadrilateral mapped to the output rectangle. For keystone where verticals converge at the top, pushx0/x1inward (toward center). Usesense=source(default) orsense=destinationto flip the mapping direction.interpolation=linearorcubicfor smooth output.vignetteangle: cone half-angle in radians.PI/5≈ 36° (moderate). Smaller value = more intense darkening. Usex0/y0to offset the vignette center.rotateangle: RADIANS. For degrees useN*PI/180. Useow=rotw(a):oh=roth(a)to auto-expand canvas so corners are not clipped.shearshx/shy: skew factor (x-shift per y, y-shift per x). 0.05-0.2 typical. Addinterp=bilinearfor smoother output.lensfunmode:geometry(distortion only),tca(chromatic aberration only),vignetting(lens vignetting only),all. Default isgeometry.
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:
- Crop to safe area:
crop=iw*0.9:ih*0.9or a fixedcrop=W:H:X:Y. - Scale to standard output size: append
scale=1920:1080. - Check pixel format: add
-pix_fmt yuv420pfor broad playback compatibility. - Order matters: for rotate + scale, rotate FIRST, then scale. For lens correction + crop, lenscorrection FIRST, then crop.
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
scripts/lens.py— argparse dispatcher with subcommands:undistort-barrel,undistort-pincushion,lensfun,perspective,vignette,rotate,shear. Prints the ffmpeg command, supports--dry-runand--verbose. Stdlib only.
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