Welcome folks today in this blog post we will be looking on how to overlay
one video onto another at different positions
using the ffmpeg
library. All the full source code of the application is shown below.
Get Started
In order to get started you need to execute the below ffmpeg
command as shown below
Top Right
Position
1 |
ffmpeg -i main_video.mp4 -i overlay_video.mp4 -filter_complex "[0:v][1:v] overlay=x=50:y=50:enable='between(t,10,20)'" -pix_fmt yuv420p -c:a copy output.mp4 |
This command will overlay overlay_video.mp4
on top of main_video.mp4
starting at the coordinates (50, 50) for the duration of 10-20 seconds
The overlay
filter is used to overlay one video on top of another. The enable='between(t,10,20)'
option tells FFmpeg to only enable the overlay for the specified time range.
The -pix_fmt yuv420p
option is used to ensure compatibility with most video players, and the -c:a copy
option preserves the audio from the main video. Finally, the output is saved to a file named output.mp4
.
Changing Positions of Video Dynamically at Certain Time
Now we will be changing the position of the video
at certain times at certain positions as shown below
1 |
ffmpeg -i main_video.mp4 -i overlay_video.mp4 -filter_complex "[0:v][1:v] overlay=x=10:y=10:enable='between(t,0,10)' [tmp]; [tmp][1:v] overlay=x=(main_w-overlay_w)/2:y=10:enable='between(t,10,20)' [tmp2]; [tmp2][1:v] overlay=x=main_w-overlay_w-10:y=10:enable='between(t,20,30)'" -pix_fmt yuv420p -c:a copy output.mp4 |
This command will overlay overlay_video.mp4
on top of main_video.mp4
starting at the coordinates (10, 10) for the duration of 0-10 seconds, then it will move to the center of the main video for the duration of 10-20 seconds, and finally it will move to the right corner of the main video for the duration of 20-30 seconds. The overlay
filter is used to overlay one video on top of another. The enable='between(t,0,10)'
option tells FFmpeg to only enable the overlay for the specified time range and the x=10:y=10
option is used to position the overlay video on the top-left corner of the main video, x=(main_w-overlay_w)/2:y=10
option is used to position the overlay video on the center of the main video, and x=main_w-overlay_w-10:y=10
option is used to position the overlay video on the top-right corner of the main video. The -pix_fmt yuv420p
option is used to ensure compatibility with most video players, and the -c:a copy
option preserves the audio from the main video. Finally, the output is saved to a file named output.mp4