Complex FFmpeg Filtergraph: Scale, Deinterlace, Watermark
Building a complex video processing pipeline in FFmpeg requires
combining multiple video operations into a single chain using the
-filter_complex flag. This article provides a clear,
step-by-step guide to constructing an FFmpeg command that deinterlaces
raw footage, rescales the resolution, overlays a watermark image, and
encodes the final output using high-quality video settings.
The Complete Command
To perform all these actions in a single pass without intermediate rendering, use the following FFmpeg command:
ffmpeg -i input.mp4 -i watermark.png -filter_complex \
"[0:v]yadif=0:-1:0[deinterlaced]; \
[deinterlaced]scale=1920:1080[scaled]; \
[scaled][1:v]overlay=W-w-20:H-h-20[outv]" \
-map "[outv]" -map 0:a -c:v libx264 -crf 21 -preset medium -c:a aac -b:a 128k output.mp4How the Filtergraph Works
The -filter_complex argument treats video processing as
a directed graph. Inputs are sent into filters, and the labeled outputs
of those filters are piped into subsequent operations.
- Deinterlacing (
yadif):[0:v]targets the video stream of the first input (input.mp4).yadif=0:-1:0applies the “Yet Another Deinterlacing Filter.” The parameters define the mode (0 outputs 1 frame per frame of input), parity (-1 auto-detects field dominance), and deinterlacing choice (0 deinterlaces all frames).[deinterlaced]labels the clean progressive output stream.
- Scaling (
scale):[deinterlaced]scale=1920:1080[scaled]takes the deinterlaced output and resizes it to Full HD (1920x1080 pixels). If you want to maintain the aspect ratio while scaling, usescale=1920:-1instead.[scaled]labels the resized output stream.
- Watermarking (
overlay):[scaled][1:v]takes the scaled video stream and pairs it with the video stream of the second input (watermark.png).overlay=W-w-20:H-h-20places the watermark in the bottom-right corner.WandHrepresent the width and height of the main video, whilewandhrepresent the width and height of the watermark. The-20adds a 20-pixel padding from the edges.[outv]labels the final combined video stream.
Mapping and Encoding the Output
Once the filtergraph processing is complete, you must map the final streams and configure your encoders:
-map "[outv]": Instructs FFmpeg to use the final processed video stream from your filtergraph in the output file.-map 0:a: Maps the original audio from the first input file directly to the output without altering it.-c:v libx264 -crf 21 -preset medium: Encodes the video to H.264. The Constant Rate Factor (-crf) controls visual quality (lower values mean higher quality; 18-23 is standard). The-presetconfigures encoding speed versus compression ratio.-c:a aac -b:a 128k: Encodes the audio to AAC format at a standard bitrate of 128 kbps.