Correct Fisheye Lens Distortion in FFmpeg
This article provides a step-by-step guide on how to remove fisheye
distortion from video footage using the FFmpeg
lenscorrection filter. You will learn the syntax of the
filter, understand how its key parameters work, and see practical
command-line examples to help you flatten your video and achieve a
natural, rectilinear perspective.
Understanding the lenscorrection Filter
The lenscorrection filter in FFmpeg corrects radial
distortion—commonly known as barrel distortion (fisheye) or pincushion
distortion—using a mathematical model based on the distance from the
center of the image.
The basic syntax for the filter is:
lenscorrection=cx=cx_val:cy=cy_val:k1=k1_val:k2=k2_valParameter Breakdown
cx: The relative horizontal center of correction. It ranges from0.0to1.0. The default is0.5(the exact center of the image).cy: The relative vertical center of correction. It ranges from0.0to1.0. The default is0.5(the exact center of the image).k1: The coefficient of the quadratic correction term. This is the primary parameter for correcting fisheye distortion.k2: The coefficient of the double quadratic correction term. This is used for more severe distortion, especially near the edges of the frame.
To correct barrel (fisheye) distortion, you must use
negative values for k1 and
k2.
Step-by-Step Command Examples
1. Basic Fisheye Correction
For standard action cameras (like GoPros or dashcams), the optical
center is usually the exact center of the frame (cx=0.5 and
cy=0.5). You only need to adjust k1 and
k2.
Run the following command to apply a moderate correction:
ffmpeg -i input.mp4 -vf "lenscorrection=cx=0.5:cy=0.5:k1=-0.22:k2=-0.022" -c:a copy output.mp42. Strong Fisheye Correction
If your camera has an ultra-wide 170-degree lens, you will need stronger negative coefficients:
ffmpeg -i input.mp4 -vf "lenscorrection=cx=0.5:cy=0.5:k1=-0.35:k2=-0.05" -c:a copy output.mp43. Real-Time Preview and Tuning
Because every camera lens is different, finding the perfect
k1 and k2 values requires some trial and
error. You can use ffplay to preview the results in
real-time without rendering a new file:
ffplay -i input.mp4 -vf "lenscorrection=k1=-0.25:k2=-0.03"Adjust the k1 and k2 values in the command
until straight lines in the real world (like walls, horizons, or
pillars) appear straight in your video preview. Once you find the ideal
values, apply them to the ffmpeg export command.