Correct Lens Distortion with FFmpeg Lenscorrection

Wide-angle lenses and action cameras often introduce barrel or pincushion distortion, making straight lines appear curved. This article provides a straightforward guide on how to use the FFmpeg lenscorrection filter to correct these optical distortions, explaining the key parameters and providing practical command-line examples to help you achieve flat, natural-looking video footage.

Understanding the lenscorrection Filter

The lenscorrection filter in FFmpeg uses a mathematical model to correct radial distortion. It adjusts pixels based on their distance from the optical center of the image. The filter is controlled by four primary parameters:

Adjusting k1 and k2

Basic Command Syntax

The basic syntax for applying the lenscorrection filter in an FFmpeg command is as follows:

ffmpeg -i input.mp4 -vf "lenscorrection=cx=0.5:cy=0.5:k1=-0.1:k2=-0.05" output.mp4

Practical Examples

Because every camera lens is unique, finding the perfect values requires some trial and error. Below are common starting points for different correction needs.

1. Correcting Mild GoPro / Action Camera Barrel Distortion

Action cameras typically suffer from significant barrel distortion. To flatten the image, start with small negative values for both coefficients:

ffmpeg -i input.mp4 -vf "lenscorrection=k1=-0.15:k2=-0.05" output.mp4

2. Correcting Strong Fisheye Distortion

For extreme wide-angle or circular fisheye lenses, you will need more aggressive negative values:

ffmpeg -i input.mp4 -vf "lenscorrection=k1=-0.22:k2=-0.12" output.mp4

3. Correcting Pincushion Distortion

If your lens zooms in and causes straight lines to bend inward toward the center, use positive values:

ffmpeg -i input.mp4 -vf "lenscorrection=k1=0.1:k2=0.05" output.mp4

4. Adjusting an Off-Center Optical Lens

If your camera sensor is slightly misaligned with the lens, you can shift the correction center away from the default center (0.5, 0.5):

ffmpeg -i input.mp4 -vf "lenscorrection=cx=0.48:cy=0.52:k1=-0.1:k2=-0.05" output.mp4

Tips for Finding the Right Values

  1. Test on a Still Frame: Instead of rendering the whole video, export a single frame containing straight lines (like a wall, doorway, or grid) to test your values quickly:

    ffmpeg -i input.mp4 -ss 00:00:05 -vframes 1 -vf "lenscorrection=k1=-0.12:k2=-0.03" test_frame.png
  2. Focus on k1 First: Set k2 to 0 and adjust k1 until the center of the image looks straight. Once the center is aligned, adjust k2 to fix the distortion near the outer edges of the frame.