How to Modulate FFmpeg Contrast Based on Time

This article explains how to dynamically adjust video contrast over time using the FFmpeg eq filter. You will learn how to write mathematical expressions using time-based variables, understand the syntax requirements, and explore practical examples such as creating pulsating contrast effects or gradual transitions.

Understanding the FFmpeg eq Filter

The FFmpeg eq (equalizer) filter is used to adjust video brightness, contrast, saturation, and gamma. Most of these parameters accept expressions that are evaluated for each frame.

To modulate contrast based on the current time of the video, you must use the contrast parameter in conjunction with the t variable, which represents the current timestamp of the frame in seconds.

Key Variables for Time-Based Equations

Syntax for Modulating Contrast

The 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.

To apply a time-based equation, define the expression inside single quotes within the eq filter:

-vf "eq=contrast='EXPRESSION'"

Practical Examples

1. Pulsating Contrast (Sine Wave Modulation)

To make the contrast rise and fall continuously like a wave, you can use the sin() function.

The following command oscillates the contrast between 0.5 and 1.5 every few seconds:

ffmpeg -i input.mp4 -vf "eq=contrast='1.0+0.5*sin(2*PI*0.5*t)'" -c:a copy output.mp4

2. Gradual Contrast Fade-In (Linear Increase)

To gradually increase the contrast from 0.0 (flat gray) to 1.5 over the first 5 seconds of the video, you can use a linear equation combined with the clip() function to cap the maximum value.

ffmpeg -i input.mp4 -vf "eq=contrast='clip(0.3*t, 0.0, 1.5)'" -c:a copy output.mp4

3. Contrast Flash Effect (Sudden Spike)

If you want to create a periodic flash effect where the contrast suddenly spikes and decays, you can combine the absolute value of a cosine wave or use exponential decay.

ffmpeg -i input.mp4 -vf "eq=contrast='1.0+2.0*exp(-mod(t,2))'" -c:a copy output.mp4