(FFmpeg) How to chain multiple filters ?

As you use FFMPEG one command might get the job done but at times applying multiple filters to a video is needed. It is possible to apply one filter at a time constantly referencing the last output file but ideally, chains are used to efficiently apply multiple filters.

There are two types of filter chaining in FFMPEG; Linear chains and distinct linear chains. Linear chains are separated by commas while distinct linear chains are separated by semicolons. 

Linear chains are filtering done on the input stream or virtual temporary streams but do not require an additional temporary stream before creating the output file. Below is an example of a linear chain horizontally flipping a video and then inverting the colors:

  $ ffmpeg -i input.mp4 -vf "hflip,negate" output.mp4  

Distinct linear chains use secondary inputs or create temporary streams before creating the final output. Below is an example of a distinct linear chain. The input.mp3, [main], will split into a temporary stream [tmp]. The [tmp] audio is reversed creating [new] and finally the two streams ([main] + [new]) will merge and output as a new file, output.mp3:

$ ffmpeg -i input.mp3 -filter_complex "asplit [main][tmp]; [tmp] areverse [new]; [main][new]amix=inputs=2[out]" -map "[out]" output.mp3 

Don’t worry if the above code looks intimidating, distinct linear chains are easy once the more you use them and get used to the syntax.

In addition to chaining, some filters may take a list of parameters which might look like chaining in itself. To distinguish between chaining and a parameter list the filter starts with the name followed by an equal sign and value then each additional parameter separated by a colon. As seen in the example below, adding 2 seconds to the end of an audio file:

$ ffmpeg -i input.mp3 -af "apad=packet_size=4096:pad_dur=2" output.mp3

Where apad is a padding audio filter with a packet size of 4096 and a pad duration of 2 seconds.

Note: It is important to use -af or -filter_compex when applying an audio filter. Using -vf (video filter) would output a new file but not apply the apad filter.

Love FFmpeg? Grab a copy of FFmpeg For Beginners on Kindle or Paperback to learn over 120 ways to master FFmpeg!

buy now on amazon