How to make left-right transition of overlay image ffmpeg
I have used the below command to make a left to right transition of overlay image over a video.
ffmpeg -i Test.mp4 -i transparent.png -filter_complex "overlay=x='if(gte(t,0), -w+(t)*100, 3)':y=450" out.mp4
The overlay image is moving from left to right continuously. I need it to stop at certain point, like a left to right transition. How to do that?
Solution 1:
Heh, interesting task. So I think solution is
ffmpeg -i 1.ts -i 2.ts -filter_complex "[0:v][1:v]overlay=x='if(lte(-w+(t)*100,w/2),-w+(t)*100,w/2)':y=0[out]" -map '[out]' -y out.mp4
This filter graph moves second picture from left to right until it reaches half of the screen (w/2). So all you need to modify is w/2 in this expression. The same for some static stop point (100 pixels):
ffmpeg -i 1.ts -i 2.ts -filter_complex "[0:v][1:v]overlay=x='if(lte(-w+(t)*100,100),-w+(t)*100,100)':y=0[out]" -map '[out]' -y out.mp4
Hope it helps.
Solution 2:
How to do sliding effects?
Generally, for slide effects, you have to:
- Create a background of a certain color (e.g., black) with the correct size and duration, on which the image will be shown, using the
color
source. - Read the image with the
movie
source - Overlay the image on top of the background with the
overlay
filter - Set the coordinates of the overlay according to the current frame number or timestamp
Example
Here is an example with an image saved as image.png
(size: 1920⨉1080, duration of the clip: 15 seconds):
ffmpeg -f lavfi -i "\
color=black:d=15:s=1920x1080[background]; \
movie=image.png[overlay]; \
[background][overlay]overlay='W-n:(H-h)/2' \
" output.mp4
The image will slide from right to left without stopping:
Here is an example with the image stopping at the center:
ffmpeg -f lavfi -i "\
color=black:d=15:s=1920x1080[background]; \
movie=image.png[overlay]; \
[background][overlay]overlay='min(W-n\,0):(H-h)/2' \
" output.mp4
How to control the sliding parameters
The position of the overlay at any given time is specified as an option of the overlay
filter. The position must be given as the coordinates of the top left corner of the image.
The X/Y coordinate pair in the first above example is W-n
and (H-h)/2
, where:
-
W
is the background width (1920) -
n
is the current frame number -
H
is the background height (1080) -
h
is the overlay height (1080 here, but could be different)
Therefore, at frame 0, the image will initially be placed at 1920 - 0 = 1920 pixels from the left, so the image will slide in from the right as n increases.
Some tips:
- If you want to increase the speed of the sliding image, you have to multiply n by a fixed number.
- If you want the image to stop at some point, the
x
expression has to evaluate to0
, so you may usemin(W-n, 0)
. In this case, even if the frame number is greater than the width, the image will not be moved to a negative x position, but stay at 0. - If you want to slide the image from left to right or bottom to top, just modify the expressions for the coordinates accordingly.