Ffmpeg Filters Explained
Ffmpeg is a powerful tool for working with multimedia files. I’ve put an introduction here. This article talks about how filters work in ffmpeg in more detail.
Ffmpeg ships with many audio and video filters.
Examples of FFmpeg filters
You can use the -vf
option to apply a filter to a video or the -af
option to apply a filter to an audio file.
For example you can use the following command to apply the boxblur filter to a video with radius 10.
Box blur filter
ffmpeg -i input.mp4 -vf "boxblur=10" output.mp4
High and low pass filters
The highpass
filter is used to remove low frequency components from the audio or video. However, it is more useful for audio. For example, you can use the following command to remove low frequency components below 100Hz:
ffmpeg -i input.mp4 -af "highpass=f=1000" output.mp4
Here f
is the cutoff frequency.
Using a similar command you can remove high frequencies from your audio. This is usually useful to remove noise that is above a certain frequency.
ffmpeg -i input.mp4 -af "lowpass=f=1000" output.mp4
Filter Graphs
If you want to combine multiple filters, you can use the -filter_complex
option. This is a more advanced option that is used to apply multiple filters to a video stream and connect them together in a complex filtergraph. With filter_complex, you can apply filters such as scaling, cropping, blending, and overlaying to a video stream, and connect them together in any way you like. For example, imagine you have the following video:
If you want to add a watermark to the video, you can use the overlay filter as follows:
ffmpeg -i scrat.mp4 -i logo.png -filter_complex "[0:v][1:v]overlay=10:10" scrat_watermark.mp4
This command the uses the filter_complex
option to apply the overlay
filter to the video stream of the input video file and the logo image. The [0:v]
and [1:v]
parts of the command refer to the video streams of the input video and the overlay image respectively. The overlay=10:10
part of the command specifies the position of the overlay image. The resulting video will look something similar to the following.
Anatomy of a filter graph
Filtergraph is a term used to describe a collection of filters connected together. The syntax goes as follows:
[inputs]filter=param1=value1:param2:value2[outputs];...
To explain filter graph syntax it’s best to illustrate them with some examples.
Consider the following command:
ffmpeg -i scrat.mp4 -i logo.png -filter_complex "[0]vflip[f];[0][f]vstack[vs];[vs]split[l][r];[l][r]hstack[h];[h][1]overlay=10:10" scrat_stacked.mp4
This command will result in a video similar to the following:
The following graph illustrates the filter graph used in the above command
Here’s what the command does:
- Pass the first input
[0]
,scrat.mp4
in this case as input to thevflip
filter and assign the output to the variable [f]. - Pass the [0] input and the [f] output from previous step as inputs to the
vstack
filter, which stacks the two inputs vertically and assign the output to [vs]. - split the output of the
vstack
filter into two streams and assign the left stream to [l] and the right stream to [r]. - Pass the [l] and [r] streams as inputs to the
hstack
filter, which stacks the two inputs horizontally and assign the output to [h]. - Pass the [h] output from the previous step and the
logo.png
input,[1]
as inputs to theoverlay
filter which overlays the logo on top of the output from previous step at position(10px, 10px)
. - The output from the
overlay
filter is saved to the output filescrat_stacked.mp4
.
Read more about vflip, vstack, split, hstack, and overlay filters.
If this example wasn’t super clear, consider the following one.
Enabling and Disabling filters
You can enable and disable filters by using the enable
parameter, for example:
ffmpeg -i testsrc.mp4 -vf "boxblur=enable='lt(t,4)'" output.mp4
ffmpeg -i testsrc.mp4 -vf "boxblur=enable='between(t,0,4)'" output.mp4
The two commands above, when applied to a video generated using the testsrc
input device, will enable the boxblur
filter for the first 4 seconds of the video, producing a video like the following one.
To read more about timeline editing, check the docs.