Ch. 20 · Optimizing Web Images and Animations

한국어日本語
8 min

Conversion Commands: cwebp, avifenc, cjxl, ffmpeg

cwebp is the standard tool for WebP, avifenc for AVIF, and cjxl for JPEG XL. ffmpeg can convert too, but whether the encoder is included depends on the build, so check first. Quality numbers mean different things in each format and have to be compared directly.

Dedicated per-format programs are the baseline for conversion, and ffmpeg is a handy alternative when it's already installed. The options below were picked only from each tool's official documentation, but options and defaults can change between versions, so check with tool-name -h before running. In particular, even the same -q 80 is not the same quality when the format differs.

FormatMain toolQuality optionSpeed/effort option
WebPcwebp (libwebp)-q 0~100 (lossy), -lossless (lossless)-m 0~6 (higher is slower and usually smaller)
AVIFavifenc (libavif)-q 0~100 (100 = lossless). Set alpha quality separately with --qalpha-s 0~10 (0 is slowest, default 6)
JPEG XLcjxl (libjxl)-d distance (0 = lossless family, larger means more loss) or -q-e number (higher is slower)
JPEGcjpeg / mozjpeg, etc.-quality 0~100Varies by tool

The quality scale differs by format and tool. Don't compare using the same number. Option ranges are based on each tool's help and manual.

WebP: cwebp

Lossy / lossless / a logo with alpha
# Photo: lossy WebP. Quality 80, compression effort 6
cwebp -q 80 -m 6 photo.jpg -o photo.webp

# Logo or screenshot: lossless WebP
cwebp -lossless logo.png -o logo.webp

AVIF: avifenc

Quality 60–80 is a commonly used starting point (the general range in the help). Verify with your own photos
# Quality 70, speed 6 (default)
avifenc -q 70 -s 6 photo.jpg photo.avif

# Transparent PNG: color and alpha quality set separately
avifenc -q 70 --qalpha 90 sticker.png sticker.avif

JPEG XL: cjxl

The simplest form for converting an existing JPEG to JPEG XL
# An existing JPEG is normally handled as lossless recompression (the original JPEG can be restored)
cjxl photo.jpg photo.jxl

# A regular image, lossy: distance 1 is described as visually close to lossless
cjxl photo.png photo.jxl -d 1

Making them with ffmpeg

To make images with ffmpeg, encoders such as libwebp, libaom-av1, and libsvtav1 have to be included in the build. Check first with ffmpeg -encoders. A big plus is that you can use video filters on images as is. Resizing is the same as the scale filter.

ffmpeg image conversion examples: first check that the encoder is in your build
ffmpeg -encoders
# Check that libwebp shows up in the list

# Shrink to a width of 800 and output WebP (height keeps the aspect ratio)
ffmpeg -i photo.jpg -vf scale=800:-2 -c:v libwebp -quality 80 photo-800.webp

# Make three sizes, widths 480, 800, and 1600, in one go (Linux and Mac)
for w in 480 800 1600; do
  ffmpeg -i photo.jpg -vf scale=$w:-2 -c:v libwebp -quality 80 photo-$w.webp
done
Recompressing repeatedly keeps eroding quality

Converting a lossy image (JPEG) into another lossy format causes generation loss. Whenever possible, make each format and size directly from the original (camera file, PNG, lossless master). If all you have is a JPEG, keep in mind that it has already been through loss once, and start with the quality set slightly higher.

Metadata and color

Location and shooting information the camera embedded (EXIF) may be kept or dropped during conversion, depending on the tool. On a public site, it's a good habit to check which metadata remains in the output file so you don't accidentally expose location data. If the color profile is lost, colors can look different, so open one image in several browsers and compare (color-related symptoms).

cwebpcwebp

The command-line WebP encoder included in libwebp.

avifencavifenc

The command-line AVIF encoder from libavif. -q sets quality and -s sets speed.

cjxlcjxl

The command-line JPEG XL encoder from libjxl.

Check yourself

Can you consider the results of cwebp -q 80 and avifenc -q 80 to be the same quality?

Check yourself

What should you check first before making a WebP with ffmpeg?