Here's a detailed explanation of how to rotate and bounce an overlay image across a video using FFmpeg, addressing the issue of jerky motion:
Prerequisites
Before diving into the explanation, make sure you have FFmpeg installed on your system. If not, you can download it from the official FFmpeg website (https://ffmpeg.org/download.html) and install it according to your operating system.
Problem Analysis
The problem lies in the current filter_complex command used to bounce the logo across the video, which rotates the image but produces a jerky motion.
Solution
To achieve smoother motion, we'll use the zoompan filter to create a more natural movement for the overlay image. Additionally, we'll utilize the rotate and scale filters to rotate and resize the image as needed.
Here's the modified filter_complex command:
ffmpeg -i input.mp4 -i logo.png -filter_complex "[1]scale=w=min(ih,iw):h=min(ih,iw),zoompan=z='if(gte(a,10),10,a+0.005)':x='if(gte(a,10),10,a+0.01)':y='if(gte(a,10),10,a+0.01)':d=10:s=1000:r=60:zmin=1:zmax=1000[logo];[0][logo]overlay" -c:v libx264 -crf 23 -preset slow -movflags +faststart output.mp4
Let's break down the changes:
-
We've replaced the
bouncefilter with thezoompanfilter, which provides a more natural movement for the overlay image. -
The
scalefilter is used to resize the logo to maintain its aspect ratio. -
The
zoompanfilter parameters have been adjusted to control the movement speed and direction. Thezparameter determines the zoom level, whilexandycontrol the horizontal and vertical position of the image, respectively. Thedparameter sets the duration of each zoom phase, andsspecifies the maximum scale factor. Therparameter defines the number of rotation points per second, andzminandzmaxlimit the minimum and maximum zoom levels. -
We've added the
c:v libx264and-crf 23 -preset slowoptions to improve the video quality and reduce the jerkiness during playback. -
The
-movflags +faststartoption helps the video stream quickly when loading in media players.
Code Explanation
The provided code uses FFmpeg to process an input video (input.mp4) and an overlay image (logo.png). The filter_complex option is used to combine multiple filters to achieve the desired effect.
ffmpeg -i input.mp4 -i logo.png -filter_complex "[1]scale=w=min(ih,iw):h=min(ih,iw),zoompan=z='if(gte(a,10),10,a+0.005)':x='if(gte(a,10),10,a+0.01)':y='if(gte(a,10),10,a+0.01)':d=10:s=1000:r=60:zmin=1:zmax=1000[logo];[0][logo]overlay" -c:v libx264 -crf 23 -preset slow -movflags +faststart output.mp4
Summary and References
In this article, we've discussed how to rotate and bounce an overlay image across a video using FFmpeg, addressing the issue of jerky motion by using the zoompan filter.
- FFmpeg Documentation: Filtering
- FFmpeg Documentation: Zoompan Filter
- FFmpeg Documentation: Scale Filter
- FFmpeg Documentation: Overlay Filter
Generated by Article Generator