FFmpeg filter_complex Complete Guide for Multiple Filters
FFmpeg's filter_complex
option is a powerful tool for handling complex audio and video filters. It allows you to combine multiple filters in a single command, enabling various audio and video processing effects from simple to complex. Whether you're adding watermarks, subtitles, adjusting colors, or compositing images, filter_complex
can handle it all.
Core Syntax and Concepts
Basic Syntax Structure
The basic format of a filter:
filtername=option1=value1:option2=value2:option3=value3...
Complete command format using filter_complex
:
ffmpeg -i input0 -i input1 -i inputN -filter_complex "multiple filter combination expression" output.mp4
Multiple Filter Combination Rules
The core idea of multiple filter combination expressions is:
[input]filter[output];[input]filter[output];
- Use
;
semicolons to separate different filter chains (pipelines) - Use
:
colons to separate multiple parameters of the same filter - Use
[]
square brackets to identify input sources and output variables
Input Source Identification
[0]
: First input file[0:v]
: Video track of the first input file[0:a]
: Audio track of the first input file[0:s]
: Subtitle track of the first input file[0:a:1]
: Second audio track of the first input file (index starts from 0)- Custom variables: Such as
[temp]
,[out]
, etc., used to pass intermediate results between filter chains
Practical Examples
Example 1: Add Watermark and Subtitles
Add image watermark and subtitles to a video simultaneously:
ffmpeg -y -i v.mp4 -i logo.png -filter_complex "[0][1]overlay=(W-w)/2:(H-h)/2[watermarked];[watermarked]subtitles=s.srt:force_style='FontName=PingFang SC,Fontsize=16'" output.mp4
Note: Subtitle files are specified directly in the
subtitles
filter and do not need to be input separately with the-i
parameter.
Example 2: Create Rotated Text Watermark
Create a PNG watermark image with rotated text and overlay it on a video:
- Generate test video:
ffmpeg -y -f lavfi -i testsrc=size=192x108:rate=10:duration=10 -vcodec libx264 -pix_fmt yuv420p -f h264 input.264
- Generate watermark image with rotated text:
ffmpeg -y -f h264 -i input.264 -filter_complex "color=black@0:100x100,format=rgba[c];[c]scale2ref[ct][mv];[ct]setsar=1,drawtext=text='WATERMARK':x=(W-tw)/2:y=(H-th)/2:fontfile=arial.ttf:fontsize=(w+h)/18:[email protected],split[txt][alpha],[txt][alpha]alphamerge,rotate=(-27*PI)/180:ow=iw:oh=ih:c=black@0,drawtext=text='Bottom Center':x=(W-tw)/2:y=H-th-3:fontfile=arial.ttf:fontsize=12:[email protected]:box=1:[email protected]:boxborderw=5,drawtext=text='Top Center':x=(W-tw)/2:y=3:fontfile=arial.ttf:fontsize=7:fontcolor=4F81BD@1:box=1:boxcolor=C0504D@1:boxborderw=5[fin];[mv]nullsink" -frames:v 1 -map "[fin]" rot_text.png
- Overlay watermark on video:
ffmpeg -y -r 25 -f h264 -i input.264 -i rot_text.png -filter_complex "[0][1]overlay=x=(W-w)/2:y=(H-h)/2" -c:v libx264 output.mp4
Example 3: Separate Audio and Video Processing
Scale the video while adjusting audio volume:
ffmpeg -i input.mp4 -filter_complex "[0:v]scale=1280:720[video_out];[0:a]volume=1.5[audio_out]" -map "[video_out]" -map "[audio_out]" output.mp4
Common Questions
1. Does the order of filter chains matter?
Yes, the order in which filters are applied directly affects the final result. For example, scaling before adding a watermark produces different results than adding a watermark before scaling.
2. How to handle multiple input sources?
You can use multiple -i
parameters to specify multiple input sources, then reference them in the filter expression using [0]
, [1]
, etc.
3. How to optimize performance for complex filters?
- Minimize unnecessary filter operations
- Use hardware-accelerated encoding/decoding (such as
-hwaccel nvdec
and-c:v h264_nvenc
in examples) - Set intermediate variables appropriately to avoid redundant calculations
Reference Links
- FFmpeg Official Documentation
- Stack Overflow: ffmpeg multiple commands using filter_complex
- Stack Overflow: ffmpeg filter_complex with speedup and subtitles and scaling