Overlay Image into Video

Example overlay: -filter-complex
  ffmpeg -i /Users/nickolaykondratyev/Downloads/Restaurant_Song_2.0_.mp4 -i image.png -filter_complex "[0:v][1:v] overlay=(W-w)/2:(H-h)/2:enable='between(t,2,10)'" -codec:a copy output_video.mp4

Let's break down the command segment "[0:v][1:v] overlay=(W-w)/2:(H-h)/2:enable='between(t,2,10)'":

Components of the Command

  1. [0:v][1:v]:

    • These represent the video streams that are being processed.
    • [0:v] refers to the first video stream, which is the input video (input_video.mp4).
    • [1:v] refers to the second video stream, which is the image (image.png).
    • In FFmpeg, the index 0, 1, etc., indicate the input file index. The v stands for the video stream.
  2. overlay=(W-w)/2:(H-h)/2:

    • The overlay filter is used to place one video (or image) on top of another.
    • W and H are the width and height of the main video ([0:v]), respectively.
    • w and h are the width and height of the overlay image ([1:v]), respectively.
    • (W-w)/2: This calculates the horizontal position where the overlay should be placed, centering it horizontally on the video.
    • (H-h)/2: This calculates the vertical position where the overlay should be placed, centering it vertically on the video.
    • The result is that the image is centered on the video frame.
  3. enable='between(t,2,10)':

    • The enable option controls when the overlay filter should be active.
    • t represents the current timestamp in the video (in seconds).
    • between(t,2,10) is a condition that makes the overlay active only when the timestamp is between 2 seconds and 10 seconds.
    • So, the image will only appear from 2 seconds to 10 seconds in the video.

Putting It All Together

This command:

  • Takes the main video and an image.
  • Centers the image on top of the video.
  • Displays the image only between the 2nd and 10th seconds of the video.

The entire filter expression [0:v][1:v] overlay=(W-w)/2:(H-h)/2:enable='between(t,2,10)' instructs FFmpeg to take the first video input and overlay the second video input (the image), positioning it in the center of the video, and making it visible only between 2 and 10 seconds into the video.


Backlinks