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

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

  1. Deinterlacing (yadif):
    • [0:v] targets the video stream of the first input (input.mp4).
    • yadif=0:-1:0 applies 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.
  2. 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, use scale=1920:-1 instead.
    • [scaled] labels the resized output stream.
  3. 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-20 places the watermark in the bottom-right corner. W and H represent the width and height of the main video, while w and h represent the width and height of the watermark. The -20 adds 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: