Apply Multiple Filters to One Video Using FFmpeg Split

This guide explains how to use the FFmpeg split filter to duplicate a single video stream, apply different filters to each stream, and save them as separate output files in a single command execution. This method is highly efficient because FFmpeg only decodes the source video once, saving significant processing time and CPU resources.

The FFmpeg Command Syntax

To process a video stream with multiple filters and output different files, you must use FFmpeg's -filter_complex option.

Here is the template command to achieve this:

ffmpeg -i input.mp4 -filter_complex "[0:v]split=2[temp1][temp2]; [temp1]filter_one[out1]; [temp2]filter_two[out2]" -map "[out1]" output1.mp4 -map "[out2]" output2.mp4

Practical Example

In this practical example, we will take an input video (input.mp4), split it, apply a grayscale filter to the first stream, scale the second stream to 720p, and output them as two distinct files while retaining the original audio.

Run the following command:

ffmpeg -i input.mp4 -filter_complex \
"[0:v]split=2[stream1][stream2]; \
 [stream1]hue=s=0[gray]; \
 [stream2]scale=1280:720[scaled]" \
-map "[gray]" -map 0:a? black_and_white.mp4 \
-map "[scaled]" -map 0:a? scaled_720p.mp4

Command Breakdown

  • -i input.mp4: Specifies the source video file.
  • -filter_complex: Initiates the complex filtergraph, which is required when dealing with multiple inputs or outputs.
  • [0:v]split=2[stream1][stream2]:
    • [0:v] selects the video stream of the first input file.
    • split=2 duplicates this video stream into two identical copies.
    • [stream1][stream2] assigns custom label names to these newly created streams.
  • [stream1]hue=s=0[gray]: Takes the first split stream, applies the hue=s=0 filter (which desaturates the color to make it black and white), and labels the output stream as [gray].
  • [stream2]scale=1280:720[scaled]: Takes the second split stream, applies the scale filter to resize it to 1280x720 pixels, and labels the output stream as [scaled].
  • -map "[gray]" -map 0:a? black_and_white.mp4: Maps the processed grayscale video stream and the original audio stream (if present) to the first output file. The ? in 0:a? ensures the command does not fail if the input video has no audio.
  • -map "[scaled]" -map 0:a? scaled_720p.mp4: Maps the scaled video stream and the original audio stream to the second output file.