The function of the filter is mainly to process the original audio and video data to achieve various effects. Such as overlaying watermarks, flipping and zooming videos, etc.
The following figure shows the normal transcoding process, the filter is in the middle of decoding and encoding, and the dotted line indicates that it is optional.
Use the command to view the filters supported by ffmpeg
ffmpeg -filters
View detailed parameters of a filter
ffmpeg -h filter=pad
The above figure shows the function and required parameters of the pad filter.
Filters in ffmpeg can be divided into:
- source filter only output
- audio filter audio filter
- video filter video filter
- multimedia filter complex filter
- sink filter only input
Two special filters need to be mentioned here: buffer and buffersink .
(1) Buffer: The filter buffer represents the source in the filter graph, and the original data is input to this filter node. By calling the function provided by the filter (such as av_buffersrc_add_frame), the frame to be filtered can be transmitted into the filtering process. When creating the filter instance, you need to provide some necessary parameters about the format of the input frame (such as: time_base, image width and height, image pixel format, etc.).
(2) buffersink: a special filter, the filter buffersink represents the output node in the filter graph, and the processed data is output from this filter node. By calling the function provided by the filter (such as av_buffersink_get_frame), the frame after being filtered by the filtering process can be provided.
Except for source and sink filters, all other filters have at least one input and at least one output.
It can also be simply divided into simple filters and complex filters.
Simple filter: only one input and output, use -vf (video filter), -af (audio filter) on the command line
Complex filter: with multiple inputs and outputs, use -lavfi or -filter_complex on the command line
In the figure below, an official filtergrpah example is given:
In this filter graph, we can see that using 4 filters
1. Use the split filter to copy the input stream into two stream outputs, one called main and one tmp
2. Use the crop filter to crop the tmp stream
3. Use vflip to vertically flip the tmp stream
4. Use overlay to superimpose the tmp stream on the main stream
It can be expressed using the command line as follows
ffmpeg -i INPUT -vf 'split [main][tmp]; [tmp] crop=iw:ih/2:0:0,vflip [flip]; [main][flip] overlay=0:H/2' OUTPUT
Filters of the same path are separated by commas (','), and filters of different paths are separated by semicolons (';').
Syntax of Filter
Syntax of Filter
filter_name=param_name1=param_value1:param_name2=param_value2
filter_name: is the name of the filter, must have; its parameters are optional, separated by ":" or "+", can have parameter names or not;
For example:
ffmpeg -i video.avi -filter_complex 'extractplanes=y+u+v[y][u][v]' -map '[y]' y.avi -map '[u]' u.avi -map '[v]' v.avi
This filter that extracts the Y, U, and V components of the video has three outputs, which are [y][u][v]. After extraction, save different outputs to different files
A filter without audio and video input is called a source filter
A filter without audio and video output is called a sink filter
Syntax of filterchain
filter1,filter2,....
It is a combination of multiple filters, separated by commas; and each filter is the input and output of the previous filter;
ffmpeg -i audio.aac -filter_complex "aresample=async=16000,adelay=316397,volume=1.0" -acodec libfdk_aac -y output.mp4
Three filters are used here, respectively aresample, adelay, volume, to form a filterchain;
Grammar of filtergraph
filterchain1;filterchain2;...
It is a combination of multiple filterchains, separated by a semicolon ";",
ffmpeg -i INPUT -filter_complex "split [main][tmp]; [tmp] crop=iw:ih/2:0:0, vflip [flip]; [main][flip] overlay=0:H/2" OUTPUT
Three filterchains are used here, namely:
split [main][tmp]; // it has only one filter, that is split; it has a default input, that is, the decoded frame of INPUT;
There are two outputs, identified by [main], [tmp];
[tmp] crop=iw:ih/2:0:0, vflip [flip] // It consists of two filters, namely crop and vflip; one input [tmp], one output [flip];
[main][flip] overlay=0:H/2 // It consists of a filter, namely overlay, with two inputs, namely [main][flip], a default output;
common filters
- scale: scaling of the video/image
- overlay: overlay of video/picture
- crop: video/image cropping
- trim: intercept video clips
- rotate: rotate the video at any angle
- movie: load third-party videos and pictures
- yadif: deinterlacing
- pad: padding video
- drawtext: add text