How to Configure WebP Compression and Loop in FFmpeg
This article provides a quick guide on how to configure the compression quality and loop count when encoding WebP images and animations using FFmpeg. You will learn the exact command-line parameters needed to control image fidelity, enable lossless compression, and define how many times an animated WebP should loop.
Configuring Compression Quality
To encode videos or image sequences to WebP, you must use the FFmpeg
WebP encoder codec, specified as -c:v libwebp. The
compression quality is controlled primarily by the -q:v (or
-qscale) option and the -lossless flag.
Lossy Compression
For standard lossy compression, use a quality scale from 0 to 100, where 0 is the lowest quality (highest compression) and 100 is the highest quality (lowest compression). The default value is 75.
ffmpeg -i input.mp4 -c:v libwebp -q:v 80 output.webpLossless Compression
To enable lossless compression, add the -lossless 1
flag. In lossless mode, the -q:v option behaves
differently: it defines the amount of CPU effort spent on compressing
the file size (0 to 100). A higher value results in a smaller file size
but takes longer to encode.
ffmpeg -i input.mp4 -c:v libwebp -lossless 1 -q:v 90 output.webpConfiguring Loop Count
For animated WebPs, you can control how many times the animation
plays using the -loop output option.
-loop 0: Infinite looping (default behavior for animations).-loop 1: Plays the animation exactly once and stops on the last frame.-loop N: Loops the animation \(N\) times (where \(N\) is a positive integer).
To create an animated WebP with a quality of 80 that loops infinitely, use the following command:
ffmpeg -i input.mp4 -c:v libwebp -q:v 80 -loop 0 output.webpTo create an animated WebP that plays only once:
ffmpeg -i input.mp4 -c:v libwebp -q:v 80 -loop 1 output.webp