Ch. 17 · FFmpeg Filters in Practice

한국어日本語
7 min

Filtergraph Syntax: -vf and -filter_complex

FFmpeg filters come in two styles: a chain joined with commas, and a graph joined with labels. This lesson covers what the four symbols (, ; : [ ]) do, and when -vf is enough versus when you need -filter_complex.

In the Chapter 10 hands-on lesson (Encoding practice with FFmpeg) you only touched encoder options. In real work, though, commands get long mostly because of filters that process the video before encoding. Shrinking, cropping, deinterlacing, and adding subtitles are all filters. Before learning the filters one by one, this chapter starts with the syntax for chaining them together.

A filter is a stage between decoding and encoding

FFmpeg decodes the file into uncompressed frames, passes those frames through filters, and hands them to the encoder. A filter changes the decoded pixels, so the moment you apply one, that stream has to be re-encoded. That is why -c:v copy and -vf can't be used together (FFmpeg reports an error). The same goes for audio: -af can't be combined with -c:a copy.

Input file Decoder Filter (-vf / -af) Encoder Output Decoded frames → process → compress again
Filters only act on decoded frames, so using a filter means re-encoding

-vf: a chain with one input and one output

If the job is to process one input video into one output, -vf (video) and -af (audio) are enough. Joining filters with a comma (,) feeds the output of the previous filter into the next one. Options within a filter are separated with a colon (:).

One chain: crop, then shrink, then match the pixel format
ffmpeg -i input.mp4 \
  -vf "crop=1920:800:0:140,scale=1280:-2,format=yuv420p" \
  -c:v libx264 -crf 20 -c:a copy output.mp4
Order changes the result

A chain runs left to right. If crop comes before scale, you crop in the original resolution; if you reverse them, you have to crop using coordinates from the already-shrunk frame. In general, the order "crop → scale → pad → subtitles → pixel format" makes your intent easiest to read. Subtitle placement comes up again in Lesson 6.

-filter_complex: when there is more than one input or output

Placing two videos side by side, overlaying a logo on a video, or making two outputs of different resolutions from one input all involve a flow that splits and merges. For these you use `-filter_complex` and attach bracketed labels at the connection points. Multiple filter chains (groups joined with commas) are separated with a semicolon (;).

SymbolRoleExample
,Joins filters in order inside one chaincrop=...,scale=...
;Separates different chains (in -filter_complex)[0:v]scale=...[a];[a][1:v]overlay[v]
:Separates the options of one filterscale=1280:720:flags=lanczos
[ ]Input/output labels. [0:v] is the video of input 0; [v] is a name you assign[0:v][1:v]hstack=inputs=2[v]

The syntax is documented under "Filtergraph description" in the official FFmpeg documentation.

Stack the original and the encoded result side by side to compare by eye
ffmpeg -i original.mp4 -i encoded.mp4 \
  -filter_complex "[0:v][1:v]hstack=inputs=2[v]" \
  -map "[v]" -c:v libx264 -crf 18 compare.mp4
Two outputs at different resolutions from one input (split divides the flow in two)
ffmpeg -i input.mp4 \
  -filter_complex "[0:v]split=2[a][b];[b]scale=-2:720[small]" \
  -map "[a]" -c:v libx264 -crf 20 full.mp4 \
  -map "[small]" -c:v libx264 -crf 22 small.mp4

The result labels of -filter_complex ([v], [small]) only end up in the output if you pull them out with `-map`. If you create a label and forget -map, FFmpeg falls back to its default stream selection and may use a stream that never went through the filter. Note that an output without a label is not automatically connected to the next stage.

Quotes and commas: the most common syntax error

  • Wrapping the whole filter string in double quotes is the safest choice. Shells on Linux and macOS also accept single quotes, but Windows cmd does not treat single quotes as quotes.
  • When an option value contains a comma (an expression like min(1280,iw)), the comma is read as a chain separator. Either wrap the expression in single quotes (scale='min(1280,iw)':-2) or put a backslash before the comma (\,). This is where shell quoting and FFmpeg quoting overlap, so if it doesn't work, check the [escaping section of the FFmpeg documentation](https://ffmpeg.org/ffmpeg-filters.html#Notes-on-filtergraph-escaping).
  • If you're unsure about a filter name or option, run ffmpeg -filters to see the filters in your build, and ffmpeg -h filter=scale (for example) to check options and defaults. Some filters are missing depending on the build (zscale, subtitles, and so on).
In the field: when filters get long

When the filter string gets long, shell quoting turns into a nightmare. In that case you can write the filtergraph in a text file and read it with -filter_complex_script graph.txt (an option listed in the FFmpeg documentation). Line breaks and comments make it easier to read, and you avoid shell-escaping problems. For repeat jobs, you can also start by building the base command with the FFmpeg Command Builder and then adding filters to it.

FiltergraphFiltergraph

The whole structure of filters connected to one another. It includes both comma-joined chains and label-joined branches and merges.

-vf / -afSimple filter options

Options that apply a filter chain to a single video/audio stream. Used when there is one input and one output.

-filter_complexComplex filtergraph

The option for filtergraphs with more than one input or output. Bracketed labels and -map go together.

Link labelLink label

A connection-point marker written as [name] inside a filtergraph. [0:v] is the video stream of input 0.

Check yourself

What is the fundamental reason ffmpeg -i in.mp4 -vf scale=1280:-2 -c:v copy out.mp4 fails?

Check yourself

You used -filter_complex and created a [v] label. How do you get this result into the output file?