Escape Special Characters in FFmpeg Filter Complex
Handling special characters in FFmpeg’s -filter_complex
can be highly confusing due to multiple layers of parsing. This article
provides a clear, step-by-step guide to the syntax rules required to
escape characters like commas, colons, semicolons, and quotes, ensuring
your complex video and audio filtering commands run without syntax
errors.
The Three Levels of Escaping
To successfully write an FFmpeg command, you must understand that escaping happens at three different layers:
- The Shell Level: Your terminal (Bash, PowerShell, or Command Prompt) processes the command first.
- The Filtergraph Level: FFmpeg parses the
-filter_complexstring to identify individual filters, inputs, and outputs (separated by commas and semicolons). - The Filter/Value Level: FFmpeg parses the specific arguments inside a single filter (separated by colons).
Rule 1: Escape Special Characters inside Filter Values
In FFmpeg, certain characters have special structural meanings: *
Comma (,) separates different filters in a
chain. * Semicolon (;) separates different
filter chains. * Colon (:) separates
arguments within a filter. * Equals sign
(=) assigns a value to an argument. *
Square brackets ([ and ])
define input and output link labels.
If you need to use any of these characters as literal text inside a
filter (for example, in the drawtext filter), you must
escape them.
Method A: Backslash Escaping
Precede the special character with a backslash (\).
drawtext=text='Hello\: World'(Here, the colon is escaped so FFmpeg does not treat “World” as a new argument key).
Method B: Single Quote Wrapping
Wrap the entire value in single quotes.
drawtext=text='Hello: World, how are you?'When using single quotes, you only need to escape literal single
quotes (') and backslashes (\) using a
backslash.
Rule 2: Account for Shell Parsing (Double Escaping)
Because the terminal shell processes your command before passing it
to FFmpeg, the shell may strip away your backslashes. To prevent this,
you must wrap your entire -filter_complex argument in
double quotes (for Bash/Zsh) or use double-escaping.
Using Double Quotes for the Shell
Wrapping the filter string in double quotes protects the internal single quotes and backslashes from being consumed by the shell.
ffmpeg -i input.mp4 -filter_complex "drawtext=text='Time\: 10\:00'" output.mp4The Double-Escaping Rule
(\\)
If you are not wrapping the command in shell quotes, or if you are
using nested strings, you must use a double backslash (\\).
The shell consumes the first backslash, and FFmpeg receives the second
one.
For example, to specify a Windows file path with a drive letter (which contains a colon) inside a filter:
# Correct escaping for a path inside a filter
fontfile='C\:\\Windows\\Fonts\\arial.ttf'Quick Reference: Common Escaping Scenarios
1. Escaping a Comma inside Text
If you want to draw the text “Go, Team!” on a video, the comma must be escaped so FFmpeg doesn’t think you are starting a new filter.
-filter_complex "drawtext=text='Go\, Team!'"2. Escaping Colons in Windows Paths
Colons are argument separators. When using a Windows absolute path for a font file or image overlay, escape the colon and the backslashes.
-filter_complex "drawtext=fontfile='C\:\\Windows\\Fonts\\Arial.ttf':text='Hello'"3. Escaping Single Quotes inside Single Quotes
If your text contains an apostrophe/single quote, escape it with a backslash inside the single-quoted string.
-filter_complex "drawtext=text='It\'s a beautiful day'"