How to Dynamically Adjust Video Exposure in FFmpeg
This article explains how to use FFmpeg’s exposure
filter to dynamically modify a video’s brightness over time. You will
learn how to write mathematical expressions using time-based variables
to create effects like gradual exposure fades, pulsing light variations,
and conditional exposure corrections.
Understanding the FFmpeg Exposure Filter
The exposure filter alters the exposure of a video using
logarithmic EV (Exposure Value) steps. A value of 0 leaves
the video unchanged, positive values brighten the video, and negative
values darken it.
To make this filter dynamic, you can leverage FFmpeg’s expression
evaluation. Instead of passing a static number to the filter, you pass a
mathematical formula that references time (t) or frame
number (n).
The basic syntax for the filter is:
-vf "exposure=exposure='EXPRESSION'"Practical Examples of Dynamic Exposure
1. Gradual Exposure Increase (Fade-In Effect)
To make a video start dark and gradually brighten over time, you can
multiply the time variable (t) by a multiplier.
ffmpeg -i input.mp4 -vf "exposure=exposure='t*0.5'" -c:a copy output.mp4- How it works: At 0 seconds, the exposure is
0. At 4 seconds, the exposure becomes2.0EV, making the video significantly brighter as time progresses.
2. Pulsing or Oscillating Exposure
You can create a pulsing light effect by using trigonometric
functions like sine (sin).
ffmpeg -i input.mp4 -vf "exposure=exposure='sin(t)*1.5'" -c:a copy output.mp4- How it works: The
sin(t)function oscillates between-1and1over time. Multiplying it by1.5causes the exposure to smoothly wave back and forth between-1.5EV and+1.5EV.
3. Triggering Exposure Changes at Specific Times
If you want to adjust the exposure only after a certain timestamp,
you can use the if() evaluation function.
ffmpeg -i input.mp4 -vf "exposure=exposure='if(gt(t,3), -1.5, 0)'" -c:a copy output.mp4- How it works: This expression checks if the current
time (
t) is greater than 3 seconds. If true, it drops the exposure to-1.5EV; otherwise, the exposure remains at0.
4. Smooth Exposure Transition Between Specific Timestamps
To smoothly transition exposure from one value to another between two
specific time frames, use a combination of clip and linear
interpolation formulas.
ffmpeg -i input.mp4 -vf "exposure=exposure='clip(-2 + (t-2)*1, -2, 1)'" -c:a copy output.mp4- How it works: This command starts the video at
-2EV. Between seconds 2 and 5, the exposure increases linearly by1EV per second, eventually capping at1EV due to theclipfunction limits.