FFmpeg Overlay Watermark at Specific Pixel Offset
This article explains how to precisely position an image watermark on
a video using the FFmpeg overlay filter. By utilizing
built-in mathematical variables for the main video and overlay image
dimensions, you can dynamically calculate and apply specific pixel
offsets from any corner of the video.
Understanding the Overlay Variables
FFmpeg’s overlay filter uses a coordinate system where
x represents the horizontal position (from the left edge)
and y represents the vertical position (from the top edge).
To calculate custom offsets, FFmpeg provides several built-in
variables:
main_worW: The width of the main input video.main_horH: The height of the main input video.overlay_worw: The width of the overlay (watermark) image.overlay_horh: The height of the overlay (watermark) image.
Positioning Formulas with Pixel Offsets
By combining these variables with basic arithmetic, you can place your watermark at an exact pixel offset from any corner of the video frame.
1. Bottom-Right Corner Offset
To place a watermark in the bottom-right corner with a 20-pixel
padding from both the right and bottom edges, use the following
formulas: * x = main_w - overlay_w - 20 *
y = main_h - overlay_h - 20
2. Bottom-Left Corner Offset
To position the watermark in the bottom-left corner with a 15-pixel
margin from the left and bottom edges: * x = 15 *
y = main_h - overlay_h - 15
3. Top-Right Corner Offset
To place the watermark in the top-right corner with a 10-pixel
padding from the top and right edges: *
x = main_w - overlay_w - 10 * y = 10
4. Top-Left Corner Offset
To place the watermark in the top-left corner with a simple 25-pixel
margin: * x = 25 * y = 25
Example FFmpeg Command
Below is a complete command demonstrating how to apply a bottom-right watermark offset of 30 pixels horizontally and 20 pixels vertically:
ffmpeg -i input.mp4 -i watermark.png -filter_complex "[0:v][1:v]overlay=x=main_w-overlay_w-30:y=main_h-overlay_h-20[out]" -map "[out]" -map 0:a? -c:v libx264 -c:a copy output.mp4Command Breakdown:
-i input.mp4: Specifies the main background video.-i watermark.png: Specifies the watermark image.-filter_complex: Invokes the filtergraph.[0:v][1:v]overlay=...: Feeds the video stream[0:v]and the image stream[1:v]into the overlay filter.x=main_w-overlay_w-30: Sets the horizontal offset to 30 pixels from the right edge.y=main_h-overlay_h-20: Sets the vertical offset to 20 pixels from the bottom edge.-map "[out]": Maps the processed video output to the final file.-map 0:a?: Copies the audio stream from the input video if it exists.-c:v libx264 -c:a copy: Encodes the output video using H.264 while copying the audio stream without re-encoding.