FFmpeg Complex Filtergraph: Scale, Crop, and Watermark
Processing video files often requires multiple adjustments like resizing, cropping, and adding branding elements. Performing these operations sequentially wastes time and degrades video quality through multiple re-encodings. This guide demonstrates how to combine scaling, cropping, and watermarking into a single, highly efficient FFmpeg complex filtergraph command, saving processing power and maintaining optimal video quality.
To perform these actions simultaneously, FFmpeg uses the
-filter_complex flag. This allows you to chain multiple
filters together in a single pass.
The Unified FFmpeg Command
Below is the complete command to crop a video, scale it to 720p (1280x720), and overlay a watermark logo in the top-right corner.
ffmpeg -i input.mp4 -i watermark.png -filter_complex \
"[0:v]crop=in_w-200:in_h-200:100:100,scale=1280:720[processed_bg]; \
[processed_bg][1:v]overlay=main_w-overlay_w-20:20[outv]" \
-map "[outv]" -map 0:a? -c:v libx264 -crf 23 -c:a copy output.mp4How the Filtergraph Works
The core of this command is the -filter_complex string,
which is broken down into distinct steps separated by commas (for
continuous streaming) and semicolons (for defining new paths).
- Selecting the Input Video
[0:v]: This identifies the video stream of the first input (input.mp4). - Cropping
crop=in_w-200:in_h-200:100:100: This cuts 100 pixels off the top, bottom, left, and right of the video.in_w-200andin_h-200define the width and height of the output crop area.100:100defines the X and Y coordinates where the crop begins.
- Scaling
scale=1280:720: Chained directly after the crop with a comma, this resizes the newly cropped video to 1280x720 pixels. - Naming the Temporary Stream
[processed_bg]: The output of the crop and scale steps is saved to a temporary label called[processed_bg]. - Overlaying the Watermark
[processed_bg][1:v]overlay=...: This takes the processed video and overlays the second input ([1:v], which iswatermark.png).main_w-overlay_w-20: Positions the watermark horizontally. It subtracts the watermark width (overlay_w) and a 20-pixel margin from the video width (main_w), pushing it to the right edge.20: Positions the watermark vertically, placing it 20 pixels down from the top edge.
- Output Label
[outv]: The final combined video stream is labeled[outv].
Mapping and Encoding Settings
-map "[outv]": Tells FFmpeg to use the final processed filtergraph stream for the video output.-map 0:a?: Copies the audio from the original video file (the?ensures the command does not fail if the input video has no audio).-c:v libx264 -crf 23: Encodes the output video using the H.264 codec at a visually lossless quality level.-c:a copy: Copies the audio stream directly without re-encoding to save processing time.