Adjust Video Contrast Over Time with FFmpeg eq Filter
This guide explains how to use the FFmpeg eq (equalizer)
filter to dynamically adjust video contrast over time. By leveraging
FFmpeg’s mathematical expressions and time-based variables, you can
create smooth contrast transitions, dramatic shifts, and periodic
fluctuations directly from the command line.
The eq filter in FFmpeg allows you to adjust brightness,
contrast, saturation, and gamma. To change contrast over time, you must
assign an expression to the contrast parameter that
utilizes the time variable t (representing the current
timestamp in seconds).
The Basic Syntax
To apply a dynamic contrast adjustment, use the -vf
(video filter) flag with the eq filter:
ffmpeg -i input.mp4 -vf "eq=contrast='EXPRESSION'" -c:a copy output.mp4The default contrast value is 1.0. A value of
0.0 results in a completely gray image, while values
greater than 1.0 increase the contrast.
Example 1: Linear Contrast Increase
To gradually increase the contrast as the video plays, you can add a
multiplier to the time variable t. The following command
starts with a default contrast of 1.0 and increases it by
0.1 every second:
ffmpeg -i input.mp4 -vf "eq=contrast='1+0.1*t'" -c:a copy output.mp4Example 2: Pulsing Contrast (Sine Wave)
To make the contrast oscillate smoothly over time, use the sine
function sin(). The following command oscillates the
contrast between 0.5 and 1.5 over a repeating
5-second cycle:
ffmpeg -i input.mp4 -vf "eq=contrast='1+0.5*sin(2*PI*t/5)'" -c:a copy output.mp4Example 3: Sudden Contrast Shift at a Specific Time
If you want the contrast to change instantly at a specific second,
use the if and lt (less than) evaluation
functions. The following command keeps the contrast at a normal
1.0 for the first 5 seconds, and then doubles it to
2.0 for the rest of the video:
ffmpeg -i input.mp4 -vf "eq=contrast='if(lt(t,5),1.0,2.0)'" -c:a copy output.mp4Example 4: Gradual Transition Between Set Times
To smoothly transition contrast from 1.0 to
2.0 starting at second 3 and ending at second 8, use the
clip() function to set limits:
ffmpeg -i input.mp4 -vf "eq=contrast='clip(1.0 + (t-3)*(2.0-1.0)/(8-3), 1.0, 2.0)'" -c:a copy output.mp4In this expression, the formula calculates the linear transition,
while clip() ensures the contrast does not drop below
1.0 before second 3, nor exceed 2.0 after
second 8.