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:
![](https://cdn.statically.io/img/i.sstatic.net/HsNqH.gif)
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
![](https://cdn.statically.io/img/i.sstatic.net/A0P50.gif)
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 to 0
, so you may use min(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.