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:

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.mp4

Command Breakdown: