日本語

How to Choose FFmpeg GIF Settings by Comparing File Size and Quality

​​FFmpeg​GIF,video

When converting a screen recording or other video to a GIF with FFmpeg, you can decide which settings keep the file small while preserving the look by making a GIF for each setting and measuring both the file size and how close it is to the original video using the same yardstick. This post includes the measurement script, the numbers from comparing 12 settings in my test environment, and enlarged images for side-by-side comparison. It is a procedure you can use when you want to make an operation demo GIF for a README or blog smaller.

Note

The test environment is Ubuntu 24.04 with FFmpeg 6.1.1 (the Ubuntu package). The source video is a 6-second video made only with FFmpeg (640×360, 30fps), in which test patterns containing color bands and a clock move horizontally over a static gradient background. I did not test real screen recordings, other FFmpeg versions, GIF conversion tools other than FFmpeg, tools that compress an existing GIF afterward, or differences in how browsers display the result. The numbers depend on the content of the video, so read this as a procedure for measuring your own video.

The difference between a conversion with no options and one that uses a palette

First, I converted with no options at all.

bash
ffmpeg -i input.mp4 output.gif

In this case, the output pixel format was bgr8. This is an 8-bit format that assigns 3, 3, and 2 bits to red, green, and blue, so it represents every video with the same fixed set of 256 colors. Even when only fps=10 and scale=480:-1 were added, the format stayed bgr8, and as shown later, a fine mesh pattern appeared across the whole screen.

text
Stream #0:0(und): Video: gif, bgr8(pc, gbr/unknown/unknown, progressive), 640x360 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 30 fps, 100 tbn (default)

To make a GIF look good, you create a color table (palette) chosen for the video with palettegen, then pass the video through paletteuse, which replaces colors using that table (both are explained in the official FFmpeg filters documentation). Written as a single command, it looks like this. fps=10 reduces the number of frames, scale=480:-1:flags=lanczos shrinks the width to 480px (using the lanczos method), and then the video is split in two: one branch builds the palette and the other has it applied.

bash
ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" output.gif

I also tried the two-step form from the official documentation, which writes the palette to palette.png and then uses it. The GIFs made both ways had the same md5 (bb0394999497ddbdf2e8a0f140370ff0).

bash
ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,palettegen" palette.png
ffmpeg -i input.mp4 -i palette.png -lavfi "fps=10,scale=480:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gif

A script that measures file size and quality on the same yardstick

File size can be compared by the size of the file. For quality, I read the GIF back in, compared it frame by frame against a lossless video at the same fps and the same width, and turned the result into numbers with FFmpeg's ssim and psnr filters. SSIM is closer to 1 and PSNR is larger when the video is closer to the original. When I compared two identical files, SSIM was 1.000000 and PSNR was inf (infinity).

The script measure.sh used in the test is shown below. You run it in the form ./measure.sh name fps width options.... It creates the GIF and then prints the frame count, file size, SSIM, and PSNR on one line.

bash
#!/bin/bash
# usage: ./measure.sh name fps width [ffmpeg options for making the GIF...]
name=$1; fps=$2; w=$3; shift 3
ffmpeg -hide_banner -loglevel error -y -i input.mp4 "$@" "$name.gif"
# Comparison target: a lossless video scaled to the same fps and the same width
ref="ref_${fps}_${w}.mkv"
[ -f "$ref" ] || ffmpeg -hide_banner -loglevel error -y -i input.mp4 \
  -vf "fps=$fps,scale=$w:-1:flags=lanczos,format=rgb24" -c:v ffv1 "$ref"
frames() { ffprobe -v error -count_frames -select_streams v -show_entries stream=nb_read_frames -of csv=p=0 "$1"; }
cmp="[0:v]format=gbrp,setpts=N/TB[a];[1:v]format=gbrp,setpts=N/TB[b];[a][b]"
ssim=$(ffmpeg -hide_banner -i "$name.gif" -i "$ref" -lavfi "${cmp}ssim" -f null - 2>&1 | grep -o 'All:[0-9.]*')
psnr=$(ffmpeg -hide_banner -i "$name.gif" -i "$ref" -lavfi "${cmp}psnr" -f null - 2>&1 | grep -o 'average:[0-9.]*')
printf "%-12s frames=%s/%s  size=%7d bytes  SSIM=%s  PSNR=%s dB\n" "$name" "$(frames "$name.gif")" "$(frames "$ref")" \
  "$(stat -c %s "$name.gif")" "${ssim#All:}" "${psnr#average:}"

The comparison video is kept at the same fps and width as the GIF. The ssim and psnr filters are documented to compare frame by frame, assuming both videos have the same resolution and pixel format and the same number of frames. So both are converted to gbrp (RGB), and setpts=N/TB replaces timestamps with frame numbers so that the first frames are compared with each other and the second frames with each other. The frames=60/60 in the output means "frames in the GIF / frames in the comparison video", which also confirms that the counts match.

For a setting that uses a palette, the output looks like this:

console
$ ./measure.sh palette 10 480 -vf "fps=10,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse"
palette      frames=60/60  size= 680141 bytes  SSIM=0.960383  PSNR=35.423939 dB

Results for 12 settings

Using the default setting of fps=10, 480px width, and palettegen + paletteuse as the baseline, I made GIFs with changed settings and measured them. The change from the baseline is written in the left column of the table. File sizes in KB are calculated as 1KB = 1000 bytes.

Setting (change from baseline) Size SSIM PSNR
No options (30fps, width 640) 1,261KB 0.294 20.62dB
No palette (only fps=10, width 480) 482KB 0.298 20.58dB
Baseline (palettegen + paletteuse) 680KB 0.960 35.42dB
dither=bayer:bayer_scale=3 621KB 0.935 35.96dB
dither=none 554KB 0.966 36.52dB
max_colors=128 619KB 0.938 32.12dB
max_colors=64 528KB 0.912 29.60dB
stats_mode=diff 734KB 0.926 35.17dB
diff_mode=rectangle 689KB 0.960 35.42dB
stats_mode=diff + diff_mode=rectangle 779KB 0.927 35.20dB
fps=15 973KB 0.960 35.46dB
Width 320px 331KB 0.971 36.86dB
A horizontal bar chart of the GIF file size for each setting, with the SSIM for each shown alongside. Width 320 is the smallest at 331KB, and no options is the largest at 1261KB
File size (horizontal bars) and SSIM for each setting. Gray is a conversion without a palette

For this video, width and frame rate had the biggest effect on file size. Reducing the width from 480px to 320px took the file from 680KB to 331KB, and raising fps from 10 to 15 increased it to 973KB. SSIM barely changed in either case. The comparison video also uses the same width and fps, so this SSIM shows how close the result is to the original at the reduced size. Whether the part you want to show is readable at that size needs to be checked separately by eye.

The conversion without a palette stayed at a SSIM of 0.298 even with the same fps=10 and width 480. Its file was smaller than the baseline, but a mesh pattern appeared across the whole screen.

The stats_mode=diff option of palettegen counts colors only in the parts that changed from the previous frame. The documentation notes that it can help when a video has a static background and you want to weight the moving parts more heavily. In this video, where the background is static, the file grew to 734KB and SSIM dropped to 0.926. Adding only diff_mode=rectangle to paletteuse gave nearly the same result as the baseline.

Reducing the number of colors with max_colors lowered the file size to 619KB at 128 colors and 528KB at 64 colors, but SSIM and PSNR went down.

Don't decide by numbers alone; compare enlarged images

From the table alone, dither=none is smaller than the baseline and has a higher SSIM, so among the width-480 settings it looks like the best one. Dithering is a process that mixes dots of other colors at color boundaries, as in the enlarged image below, so that steps between colors are less noticeable. The documentation for paletteuse's bayer_scale also says that the more visible the pattern, the fewer color steps (banding) there are. The default for paletteuse is a method called sierra2_4a, and setting none turns this processing off.

Enlarging the same part of the frame at 3.0 seconds (the on-screen clock reads 03.033) three times and lining them up (stacked vertically) makes the difference visible.

Four GIFs cropped from the same frame and the same region, stacked vertically: no palette, palettegen + paletteuse, dither=none, and max_colors=64
The same part of one frame enlarged 3 times for comparison. From top: no palette, default palette, dither=none, max_colors=64

At the top, the no-palette version shows a mesh across the entire background. In the second image, the baseline setting, fine dots are mixed at the color boundaries of the rainbow band, which makes it look smooth. In the third, dither=none, the dots are gone, but the rainbow band has split into a few solid blocks of color. In the fourth, 64 colors, dots stand out in the dark square and the rainbow band.

In this test, the settings without dithering scored higher on SSIM, but they showed visible color steps. Because the numbers alone cannot settle the choice, I recommend deciding in the following order.

  1. Lower the width and fps as far as the content you want to show stays readable (in this test, they had the biggest effect on file size)
  2. Make the GIF with palettegen + paletteuse, and measure file size and SSIM with measure.sh
  3. If you want to reduce the size further, make and measure candidates with changed dither or max_colors
  4. For candidates with close SSIM values, compare the same frame enlarged and choose by how visible the color steps and dots are

For this video, the gradient background showed no steps, so if you don't mind the steps in the rainbow band, dither=none reduces the file to 554KB. If you want to avoid color steps, keeping the baseline setting and lowering the width reduces the size more.

Point

Quality numbers are calculated by comparing against a video at the same fps and width as the GIF. In addition, compare candidates with close SSIM values on enlarged frames. A setting without dithering can show a higher SSIM and still produce visible color steps.

The enlarged comparison image above was also made with FFmpeg alone. It selects frame 31 with select=eq(n\,30), crops part of it with crop=200:110:60:70, and triples it with scale=600:-1:flags=neighbor. The meanings of each option are summarized in the palettegen and paletteuse sections of the official FFmpeg filters documentation.

​Posted by 86