FFmpeg Scale Video and Apply Custom Color Matrix

To scale a video and apply a custom color matrix in a single step using FFmpeg, you can combine the scale filter and color manipulation filters into a single video filterchain using the -vf flag. This approach processes both operations in a single pipeline, which avoids multiple decoding and encoding passes, saves disk space, and significantly reduces rendering time.

The Single-Step Command

To chain filters in FFmpeg, separate them with a comma inside the -vf argument. The output of the first filter (scaling) is automatically passed as the input to the second filter (color matrix).

Here is the basic command template:

ffmpeg -i input.mp4 -vf "scale=1920:1080,colormatrix=bt601:bt709" output.mp4

How the Filters Work Together

  1. scale=1920:1080: Rescales the input video to the specified width and height. You can replace 1920:1080 with your target resolution, or use scale=1280:-1 to scale to a width of 1280 pixels while keeping the original aspect ratio automatically.
  2. colormatrix=bt601:bt709: Converts the color space from one standard to another. In this example, it converts the video from BT.601 (standard definition color space) to BT.709 (high definition color space).

Supported color spaces for the colormatrix filter include: * bt709 * bt601 * fcc * smpte170m * smpte240m

Applying Fully Custom Color Matrices

If you need to apply a custom mathematical RGB color matrix (for color grading, tinting, or channel swapping) instead of a standard color space conversion, use the colorchannelmixer filter in your chain.

The command below scales the video and applies a custom matrix that slightly boosts the red channel while reducing green and blue:

ffmpeg -i input.mp4 -vf "scale=1920:1080,colorchannelmixer=rr=1.2:rg=0.0:rb=0.0:gr=0.0:gg=0.9:gb=0.0:br=0.0:bg=0.0:bb=0.9" output.mp4

In the colorchannelmixer filter, the parameters represent the contribution of the input channels (red, green, blue) to the output channels: * rr, rg, rb: Contribution of red, green, and blue input to the output red channel. * gr, gg, gb: Contribution of red, green, and blue input to the output green channel. * br, bg, bb: Contribution of red, green, and blue input to the output blue channel.