Use the
overlay filter with a time-based expression to place a green video overlay at specific intervals. By calculating the modulo of the current timestamp against the interval duration, you can conditionally enable or disable the overlay stream. This approach ensures precise timing without needing to split the original video file into multiple segments.
Understanding the Overlay Filter Logic
The core mechanism for this task relies on the
overlay video filter in FFmpeg. Unlike simple concatenation, which joins clips end-to-end,
overlay allows one video stream to be drawn on top of another at any position and time. To make the overlay appear only every minute, we must control its visibility using an expression that evaluates to zero (transparent) or non-zero (visible).
FFmpeg expressions support mathematical operations and conditional logic. The most common function used for periodic events is the modulo operator (
%). When you divide the current time by the interval period, the remainder resets to zero exactly when the interval begins. For example, if the interval is 60 seconds,
t % 60 will equal 0 at 0:00, 1:00, 2:00, and so on. However, relying solely on equality can be tricky due to floating-point precision issues in timestamps. A more robust method involves checking if the time falls within a specific window relative to the start of each interval.
Constructing the Time Expression
To create a reliable trigger, you need an expression that returns 1 (true) during the overlay's active duration and 0 (false) otherwise. Let us assume your main video is
input.mp4 and your green screen overlay is
green_overlay.mp4. You want the overlay to last for 5 seconds every minute.
The expression structure generally looks like this:
(t - mod(t, 60)) / 5 <= mod(t, 60) / 5 + 1
This is complex. A simpler and often more effective strategy is to use the
enable parameter directly on the overlay filter instance. You can define the enable condition as:
enable='between(t\,mod(t\,60)\,mod(t\,60)+5)'
Here,
mod(t, 60) gives the offset within the current minute. If the current time
t is between the start of the minute (
mod(t, 60)) and 5 seconds past that start (
mod(t, 60) + 5), the overlay is enabled. This handles the wrap-around at the end of each minute automatically because
mod(t, 60) resets to 0 at 60 seconds.
If you prefer integer arithmetic to avoid potential floating-point drift, you can use:
enable='gte(mod(t\,60)\,0) * lt(mod(t\,60)\,5)'
This checks if the remainder of the time divided by 60 is greater than or equal to 0 AND less than 5. Since the remainder is always non-negative, the first part is always true, effectively simplifying to "is the time within the first 5 seconds of the minute?".
Positioning the Overlay
Once the timing is correct, you must specify where the green overlay appears. The
overlay filter accepts
x and
y coordinates. These can be static values or dynamic expressions. For a standard placement, such as the top-left corner with a 10-pixel margin, you would use:
overlay=x=10:y=10
If you want the overlay to fade in and out smoothly rather than appearing abruptly, you can add an alpha channel transition. However, the basic
overlay filter does not support opacity changes natively in all versions without additional filters like
colorkey or pre-processing the overlay with an alpha mask. For simple green screen replacement, ensure your
green_overlay.mp4 has an alpha channel or is processed through a chroma key filter beforehand.
For dynamic positioning, you can use expressions like
x=(W-w)/2 to center the overlay horizontally, where
W is the width of the main video and
w is the width of the overlay. Similarly,
y=(H-h)/2 centers it vertically.
Complete Command Example
Combining these elements, here is a complete FFmpeg command. This example assumes you have a main video and a green overlay video that should appear for 3 seconds every minute, centered on the screen.
ffmpeg -i input.mp4 -i green_overlay.mp4 \
-filter_complex "[0:v][1:v]overlay=x=(W-w)/2:y=(H-h)/2:enable='between(t\,mod(t\,60)\,mod(t\,60)+3)'[out]" \
-map "[out]" output.mp4
In this command:
-
-filter_complex is used because we are combining two input streams.
-
[0:v] refers to the video stream of the first input (
input.mp4).
-
[1:v] refers to the video stream of the second input (
green_overlay.mp4).
- The
overlay filter draws
[1:v] on
[0:v].
- The
enable parameter uses the
between function to activate the overlay for 3 seconds starting at the beginning of every minute.
-
[out] labels the resulting filtered stream.
-
-map "[out]" selects this stream for the output file.
If your green overlay needs to be converted to have transparency before overlaying, you might need to include a
chromakey filter in the complex chain. For instance, if
green_overlay.mp4 is a solid green rectangle, you would first apply
chromakey=green:0.1:0.2 to make the green parts transparent, then overlay the result.
Performance Considerations
Using real-time expressions in filter chains can increase CPU usage compared to static filters. FFmpeg must evaluate the expression for every frame. For short videos, this is negligible. For long-form content, consider testing with a small segment first using the
-t flag to limit duration.
Additionally, ensure that your input files are compatible. Mismatched resolutions or frame rates can cause scaling artifacts. Use the
scale filter if necessary, e.g.,
scale=1920:1080, before the overlay operation to normalize dimensions.
For related techniques, see
Understanding the Total Duration with Xfade in FFmpeg to learn how transitions affect timing calculations, or check
Split Videos into Smaller Files using File Size with FFmpeg if you prefer processing segments individually instead of using complex filter graphs.
Frequently Asked Questions
Why does my overlay flicker or glitch?
Flickering usually occurs due to frame rate mismatches between the main video and the overlay. Ensure both inputs have the same frame rate or let FFmpeg handle resampling by adding
-r 30 to the output command. Also, verify that the
enable expression does not have syntax errors, as malformed expressions can cause undefined behavior.
Can I change the color of the green screen dynamically?
No, the
overlay filter simply places pixels on top. To change the color, you must modify the source overlay file itself using a separate
colorchannelmixer or
hue filter before applying the overlay. Alternatively, use the
chromakey filter on the main video to remove a different color, but this requires careful lighting conditions.
How do I make the overlay fade in and out?
The
overlay filter does not support opacity transitions natively. You must pre-process the overlay video to include an alpha channel with fade effects. Tools like ImageMagick or FFmpeg's
drawtext with alpha masks can generate such assets. Then, use the standard
overlay filter with the pre-faded asset.
What if the overlay duration is longer than the interval?
If the overlay lasts longer than the interval (e.g., 70 seconds overlay, 60-second interval), the next instance will overlap the previous one. This creates visual stacking. To prevent this, ensure your
enable window is shorter than the interval period. Adjust the upper bound in the
between function accordingly.
Is there a way to sync the overlay with audio beats?
Syncing with audio beats requires analyzing the audio track for peaks, which is complex in pure FFmpeg filter graphs. It is easier to generate a list of timestamps from an external script based on audio analysis, then use those timestamps to construct multiple
overlay instances or use a custom filter module.