How to Overlay an Image on a Video Using FFmpeg
Using FFmpeg to place an image over a video is a powerful way to add
watermarks, logos, or static graphics to your multimedia projects. This
article provides a straightforward, step-by-step guide on how to use the
overlay filter in FFmpeg, covering basic command syntax,
common positioning coordinates, and advanced placement techniques.
The Basic Overlay Command
To overlay an image on top of a video, you need to provide both files
as inputs and use the -filter_complex flag with the
overlay filter. The basic syntax is:
ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=10:10" output.mp4Here is what each part of the command does: *
-i input.mp4: The main input video (input 0). *
-i logo.png: The image you want to overlay (input 1). *
-filter_complex "overlay=10:10": The filter that places the
image at the coordinates X=10 and Y=10 (10
pixels from the top-left corner). * output.mp4: The final
exported video file.
Position Variables
FFmpeg allows you to use internal variables to position your image
dynamically, regardless of the video resolution. The most useful
variables are: * main_w (or W): The width of
the main video. * main_h (or H): The height of
the main video. * overlay_w (or w): The width
of the overlay image. * overlay_h (or h): The
height of the overlay image.
Common Positioning Examples
Using the variables above, you can precisely position your image in various locations on the video screen:
Top-Right Corner
To place the image in the top-right corner with a 10-pixel margin:
ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=main_w-overlay_w-10:10" output.mp4Bottom-Right Corner (Standard Watermark)
To place the image in the bottom-right corner with a 10-pixel margin:
ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=main_w-overlay_w-10:main_h-overlay_h-10" output.mp4Center of the Video
To center the image perfectly on the screen:
ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" output.mp4Overlaying Transparent PNGs
FFmpeg automatically respects alpha channels (transparency) in PNG images. If your image has a transparent background, it will render correctly over the video without any additional settings. Ensure your output format supports video codecs (like H.264) that handle standard color mixing.