Highlight Video Differences with FFmpeg lut2

This article explains how to use the FFmpeg lut2 (Look-Up Table 2D) filter to visually compare and highlight the differences between two similar video files. By evaluating the pixel values of two video inputs simultaneously, you can easily identify compression loss, editing variations, or rendering discrepancies. You will learn the exact FFmpeg commands required to generate a difference map and how to amplify subtle changes so they are clearly visible.

Understanding the lut2 Filter

The lut2 filter in FFmpeg allows you to merge two video streams by applying a mathematical expression to every corresponding pixel.

In the expression: * val(0) represents the pixel value from the first input video. * val(1) represents the pixel value from the second input video. * c0, c1, and c2 represent the color channels (Y, U, and V for YUV videos, or R, G, and B for RGB videos).

By subtracting the pixel values of the second video from the first, you can isolate the differences.

Method 1: Absolute Difference (Black Background)

To highlight differences as bright pixels on a black background, use the absolute difference formula. Because subtle compression differences might be too dark to see with the naked eye, you can multiply the result to amplify the differences.

Run the following command in your terminal:

ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]lut2=c0='abs(val(0)-val(1))*8':c1='abs(val(0)-val(1))*8':c2='abs(val(0)-val(1))*8'[diff]" -map "[diff]" difference_output.mp4

How it works: * abs(val(0)-val(1)) calculates the absolute difference between the two frames, ensuring negative values become positive. * *8 is the multiplier. This amplifies the differences by a factor of eight. If the output is still too dark, you can increase this multiplier (e.g., *16 or *32). * Identical pixels will result in 0 (pure black), while any differences will appear as colored or grey pixels.

Method 2: Offset Difference (Grey Background)

If you want to see which video is brighter or darker at a specific pixel, you can use an offset comparison. This places identical pixels on a neutral grey background, with positive differences showing as lighter and negative differences showing as darker.

Run this command:

ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]lut2=c0='(val(0)-val(1))*4+128':c1='(val(0)-val(1))*4+128':c2='(val(0)-val(1))*4+128'[diff]" -map "[diff]" offset_output.mp4

How it works: * (val(0)-val(1)) subtracts the second video’s pixel value from the first. * *4 amplifies the difference. * +128 offsets the baseline to mid-grey. If there is no difference between the pixels, the output pixel will be exactly grey (128).

Prerequisites for Best Results

For the lut2 filter to work correctly, both input videos must have: 1. The exact same resolution: If they differ, resize one input first using the scale filter. 2. The exact same framerate: Ensure both videos are synchronized frame-for-frame to prevent false positives caused by timing mismatches.