日本語

How to Change Video Color Tone with FFmpeg curves

​​FFmpeg​command line,video

The FFmpeg curves filter lets you change the brightness and color of a video in one command, using the same idea as the tone curve in image editing software. You can move the red-only or blue-only curves, so it is useful for making a video warmer or cooler, or for giving it a movie-like or film-like look (color grading).

In my test environment, I applied curves to a video of a grayscale gradient from black (0) to white (255) and to a color chart. I measured what the input values became and how the average R, G, and B of the video changed. The numbers in this article are the results as measured.

Note

The test environment was Ubuntu 24.04.5 with FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5). The video was one frame of a color chart made with FFmpeg's colorchart, shown for 6 seconds (640×360, 30fps, H.264, yuv420p), with a 440Hz sine wave audio (AAC) added. To find the curve shapes, I applied curves to a 256-pixel-wide grayscale gradient (0 at the left edge, 255 at the right edge) and read the filter output before encoding. I did not test camera footage, 10-bit or HDR video, or the psfile option that reads Photoshop curve files.

Writing points as input/output pairs

curves takes the points the curve passes through, written as x/y pairs separated by spaces. x is the input value and y is the output value, both written from 0 to 1. For 8-bit video, 0 corresponds to 0 and 1 corresponds to 255.

To brighten the midtones of the whole image, write the points under master (short form m). The following command brightens pixels whose input is the middle value (0.5) to 0.6, and leaves black (0) and white (1) unchanged.

bash
ffmpeg -i in.mp4 -vf "curves=m='0/0 0.5/0.6 1/1'" -c:v libx264 -crf 18 -pix_fmt yuv420p -c:a copy out.mp4

The point list contains spaces, so wrap it in '…'. The curves documentation defines the point list format as x0/y0 x1/y1 x2/y2 ....

When I applied this setting to the grayscale gradient, the input values changed as follows.

Input 0 32 64 96 128 160 192 224 255
0/0 0.5/0.6 1/1 0 41 81 119 153 183 209 233 255
0/0 0.25/0.2 0.75/0.8 1/1 0 23 52 88 129 169 204 232 255

Black and white stay the same, and the values in between rise along a smooth curve that passes through the points. The second row is an S-shaped curve that lowers the dark side a little and raises the bright side a little. The midtone (128) barely changes, while dark areas get darker and bright areas get brighter.

Graph of curves points and the resulting curve. The curve that brightens the midtones passes through the point (0.5, 0.6) and bulges upward overall. The S-shaped curve passes below the line on the dark side and above it on the bright side
Input and output values when curves is applied to a grayscale gradient

Warm and cool color tones with red and blue curves

To change the color tone, write a curve for each color: red (r), green (g), and blue (b). The following command raises the midtone of red and lowers the midtone of blue, producing a warm color tone.

bash
ffmpeg -i in.mp4 -vf "curves=r='0/0 0.5/0.56 1/1':b='0/0 0.5/0.44 1/1'" -c:v libx264 -crf 18 -pix_fmt yuv420p -c:a copy out.mp4

Swapping red and blue produces a cool color tone. I extracted the first frame of each output video as RGB and measured the average of R, G, and B.

Setting R G B
Original video 134.6 125.8 116.8
Warm tone (r='0/0 0.5/0.56 1/1':b='0/0 0.5/0.44 1/1') 144.7 124.5 103.1
Cool tone (r='0/0 0.5/0.44 1/1':b='0/0 0.5/0.56 1/1') 121.9 124.4 127.8
preset=vintage 148.8 120.2 109.4

With the warm tone, R went up and B went down, and the reverse happened with the cool tone. G, whose curve I did not change, was in the 124 range in both cases. On the grayscale gradient with the warm-tone setting, the input 128 gray became 143,128,113 (R,G,B), so it turned into a reddish gray.

Four frames from the first frame of the color chart video side by side. The warm tone makes the whole image reddish, the cool tone makes it bluish, and preset=vintage makes black purplish and white yellowish
The first frame of the original video and the videos exported with curves applied. Each is labeled above

Presets are fixed combinations of points

The available preset values include vintage, cross_process, negative (inverts brightness and color), lighter, darker, and increase_contrast. Running ffmpeg -h filter=curves listed 11 options, including none.

Each preset is a fixed combination of points. The documentation example states that curves=preset=vintage is the same as the following setting.

bash
curves=r='0/0.11 .42/.51 1/0.95':g='0/0 0.50/0.48 1/1':b='0/0.22 .49/.44 1/0.8'

I applied both to the grayscale gradient, and all 9 measured values matched. With vintage, black at input 0 becomes 28,0,56, and white at input 255 becomes 242,255,204. Black turns purplish and white turns yellowish because the ends of the red and blue curves are moved away from 0 and 1.

The points for the other presets can be found in the FFmpeg 6.1.1 curves source. For example, lighter is m='0/0 0.4/0.5 1/1' and darker is m='0/0 0.5/0.4 1/1', both of which are master curves.

If you add r, g, or b to a preset, the explicit setting takes priority. With curves=preset=vintage:g='0/0 1/1', only green stayed as it was (input 128 green became 128), while red and blue kept the vintage values.

To check the curve shape as a graph, the plot option writes a gnuplot script. Running curves=preset=vintage:plot=vintage.plt created a 19,022-byte file that includes the curve coordinates.

master is applied after r, g, and b

If you write both master and r, the r curve is applied first, and then the master curve is applied to that result. The documentation also describes master as acting as a second conversion (a post-processing LUT).

When I applied curves=r='0/0 0.5/0.6 1/1':m='0/0 0.5/0.6 1/1' to the grayscale gradient, the R value at input 128 became 177. The value that r alone would give, 153, was raised further by master to 177. G and B had only master applied and were 153.

To apply the same curve to all three colors, use all. When all is combined with b, r and g use the all curve and b uses its own curve. With curves=all='0/0 0.5/0.6 1/1':b='0/0 1/1', input 128 became 153,153,128, so only blue stayed unchanged.

Omitting the end points (0 and 1) makes the outside flat

When I wrote points only between 0.25 and 0.75, there was no error or warning (run with -loglevel warning, nothing was displayed and the exit code was 0). Values to the left of the first point were fixed at the first point's value, and values to the right of the last point were fixed at the last point's value.

Input 0 32 64 128 192 224 255
m='0.25/0.25 0.75/0.75' 63 63 64 128 191 191 191

0.25/0.25 0.75/0.75 looks like a setting that changes nothing, since both points have the same input and output. In practice, black at 0 was raised to 63, and white at 255 was lowered to 191. Black became gray and white became dull.

Warning

Always include the end points 0/… and 1/… in curves points. If you leave them out, black and white shift toward gray with no visible message.

With a single point, m='0.5/0.8', the warning "Only one point (at (0.500000;0.800000)) is defined, this is unlikely to behave as you expect." was displayed, and every pixel became 204 (the value corresponding to 0.8).

When points are steep, natural dips in the middle

The interp option selects how the points between are connected. The default is natural (natural cubic spline). According to the documentation, choosing the other option, pchip, guarantees that the values change monotonically between points.

I compared them with m='0/0 0.1/0.5 0.3/0.55 1/1', which lifts the dark areas significantly.

Input 16 32 48 64 96 128 192
interp=natural 88 146 161 152 126 123 171
interp=pchip 100 130 134 137 147 162 201

With natural, the output rose to 161 at input 45, then fell to 121 at input 115. The gray at input 96 became darker than the gray at input 48, so the brightness order was reversed. With pchip, there was no point where the output decreased.

Graph of the curves connecting the same points with natural and pchip. The natural curve forms a peak near input 45 and then drops into a valley. The pchip curve keeps rising without any peak or valley
Input and output values when m=’0/0 0.1/0.5 0.3/0.55 1/1′ is applied with different interp settings

For the two gently curved examples in the first section (0/0 0.5/0.6 1/1 and the S-shape), the difference in output between natural and pchip was 3 or less for every input from 0 to 255. If placing a sharply bending point reverses the brightness order, adding interp=pchip removes the dips.

Errors from incorrect point syntax

When a point value was outside 0 to 1, the following error stopped processing (I omitted the address shown at the start of the line).

text
Invalid key point coordinates (0.500000;1.200000), x and y must be in the [0;1] range.

When the x values were not in increasing order, or when two points' x values were too close together, the following error appeared. Two points such as 0.5/0.5 0.501/0.6, whose integer parts after multiplying by 255 are the same (both 127), are considered too close.

text
Key point coordinates (0.500000;0.500000) and (0.501000;0.600000) are too close from each other or not strictly increasing on the x-axis

In both cases, ffmpeg ended with exit code 234, and no output was written.

Add -pix_fmt yuv420p when writing output

The reason I add -pix_fmt yuv420p to the commands above is that without it, the format of the output video changes. curves processes data in RGB format. In -loglevel verbose output, I could see that a filter converting yuv420p video to rgb24 was inserted automatically.

When I wrote output with libx264 without -pix_fmt, the results were as follows.

Output pix_fmt H.264 profile
Original video yuv420p High
No -pix_fmt yuv444p High 4:4:4 Predictive
With -pix_fmt yuv420p yuv420p High

If you want to write output with the same yuv420p and High profile as the original video, add -pix_fmt yuv420p.

Changing color only in part of the video

ffmpeg -h filter=curves shows "This filter has support for timeline through the 'enable' option.", so you can use enable to specify when the filter applies. The following command applies vintage only from 2 to 4 seconds.

bash
ffmpeg -i in.mp4 -vf "curves=preset=vintage:enable='between(t,2,4)'" -c:v libx264 -crf 18 -pix_fmt yuv420p -c:a copy out.mp4

When I measured every 0.5 seconds from 0 to 5 seconds, the average R, G, and B from 2 to 4 seconds was R=148.8 G=120.2 B=109.4, the same as when preset=vintage was applied to the whole video (the values in the table above). At other times it was R=133.8 G=124.9 B=115.8, which did not have the vintage tone (about 1 different from the original video's R=134.6 G=125.8 B=116.8). The syntax for enable expressions is described in the Timeline editing documentation.

You can copy the audio unchanged with -c:a copy. The MD5 of the audio in the original video and the video exported with curves applied both matched 1d82ff928d14ff51532eb5a9d4fc5aca.

​Posted by 86