Overlay a Logo onto a Video with FFmpeg
This article provides a quick overview and step-by-step guide on how to overlay an image logo onto a video file using FFmpeg on Linux. You will learn the core command structure, how to position the logo in different corners of the video using predefined variables, and how to adjust the logo’s size. By the end of this guide, you will be able to apply a professional watermark to your videos directly from the command line.
The Basic Command Structure
To overlay an image onto a video, FFmpeg uses the
overlay video filter. This filter takes two inputs: the
main video (the background) and the image logo (the foreground).
Here is the foundational command to place a logo in the top-left corner:
ffmpeg -i input_video.mp4 -i logo.png -filter_complex "overlay=0:0" output_video.mp4-i input_video.mp4: Specifies the main video file.-i logo.png: Specifies the image file you want to use as a logo.-filter_complex "overlay=0:0": Invokes the complex filtergraph. The0:0coordinates tell FFmpeg to place the top-left corner of the logo at the absolute top-left corner (X=0, Y=0) of the video.output_video.mp4: The final exported file.
Advanced Positioning Using Variables
Hardcoding pixel values can be inefficient if you want to place the logo in other corners. Fortunately, FFmpeg provides built-in variables to calculate positions dynamically based on the dimensions of the video and the logo:
main_w(orW): Width of the main input video.main_h(orH): Height of the main input video.overlay_w(orw): Width of the overlay logo.overlay_h(orh): Height of the overlay logo.
Using these variables, you can position your logo anywhere while adding a 10-pixel padding from the edges:
- Top-Right Corner:
ffmpeg -i input_video.mp4 -i logo.png -filter_complex "overlay=main_w-overlay_w-10:10" output_video.mp4- Bottom-Right Corner (Most Common):
ffmpeg -i input_video.mp4 -i logo.png -filter_complex "overlay=main_w-overlay_w-10:main_h-overlay_h-10" output_video.mp4- Bottom-Left Corner:
ffmpeg -i input_video.mp4 -i logo.png -filter_complex "overlay=10:main_h-overlay_h-10" output_video.mp4- Center of the Video:
ffmpeg -i input_video.mp4 -i logo.png -filter_complex "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" output_video.mp4Resizing the Logo on the Fly
If your logo image is too large for the video resolution, you can
scale it down within the same command before overlaying it. This
requires chaining the scale filter and the
overlay filter together:
ffmpeg -i input_video.mp4 -i logo.png -filter_complex "[1:v]scale=200:-1[logo];[0:v][logo]overlay=main_w-overlay_w-10:main_h-overlay_h-10" output_video.mp4In this command, [1:v]scale=200:-1[logo] takes the
second input (the logo), resizes its width to 200 pixels, and
automatically calculates the height (-1) to maintain the
original aspect ratio. The resulting scaled image is saved into a
temporary label named [logo], which is then passed into the
overlay filter against the main video [0:v].