Correct Camera Lens Distortion with FFmpeg
Wide-angle lenses and action cameras often produce “barrel
distortion,” a visual warping where straight lines curve outward near
the edges of the frame. This guide provides a direct, step-by-step
tutorial on how to use FFmpeg’s built-in lenscorrection
filter to fix this distortion and flatten your video footage.
The lenscorrection Filter Syntax
The lenscorrection video filter in FFmpeg uses a
mathematical model to adjust pixels based on their distance from the
center of the image. The basic syntax for the filter is:
ffmpeg -i input.mp4 -vf "lenscorrection=cx=0.5:cy=0.5:k1=-0.1:k2=0.05" output.mp4Understanding the Parameters
To effectively correct your footage, you need to adjust four key parameters:
cx(Center X): The horizontal center of distortion, expressed as a fraction of the image width (ranging from0.0to1.0). For most cameras, the distortion center is the exact middle of the frame, so this is set to0.5.cy(Center Y): The vertical center of distortion, expressed as a fraction of the image height (ranging from0.0to1.0). This is also typically set to0.5.k1(Correction coefficient 1): The primary factor for correcting the innermost distortion. This parameter is the most important for fixing barrel distortion.k2(Correction coefficient 2): The secondary factor for correcting distortion near the outer edges of the frame.
How to Find the Right Values
Correcting barrel distortion requires finding the right negative
values for k1 and k2.
- For Barrel Distortion (curving outward): Set
k1to a negative value (e.g.,-0.1to-0.3). - For Pincushion Distortion (curving inward): Set
k1to a positive value (e.g.,0.1to0.3). - Fine-Tuning: Start by setting
k2=0and adjustingk1until the lines near the center-middle of the screen are straight. Once the center is corrected, adjustk2(usually a small negative or positive value) to straighten the lines at the extreme outer edges of the frame.
Practical Examples
Standard GoPro/Action Camera Correction: Action cameras often require moderate correction. The following settings are a common starting point for wide-angle 1080p footage:
ffmpeg -i input.mp4 -vf "lenscorrection=cx=0.5:cy=0.5:k1=-0.227:k2=0.022" output.mp4Heavy Fisheye Correction: For extreme fisheye
lenses, you will need a stronger negative k1 value:
ffmpeg -i input.mp4 -vf "lenscorrection=cx=0.5:cy=0.5:k1=-0.350:k2=0.050" output.mp4Workflow Tip
Because finding the perfect coefficients requires trial and error, test your settings on a short, 5-second clip first to save processing time:
ffmpeg -ss 00:00:10 -i input.mp4 -t 5 -vf "lenscorrection=cx=0.5:cy=0.5:k1=-0.18:k2=0.012" test.mp4Adjust the k1 and k2 values in the command
until straight architectural lines (like walls or doors) in your test
clip appear perfectly straight.