FFmpeg WebP Compression Quality and Loop Count

This guide explains how to control output file size and playback behavior when encoding WebP images and animations using FFmpeg. By adjusting specific encoder flags, you can toggle between lossy and lossless compression, fine-tune the quality level, and define whether an animated WebP loops infinitely, loops a set number of times, or plays only once.

Setting WebP Compression Quality

FFmpeg uses the libwebp encoder to generate WebP files. You can control quality using either lossy or lossless compression modes.

Lossy Compression

For standard lossy encoding, use the -q:v (or -quality) flag followed by a value from 0 to 100. Lower values yield smaller file sizes at the cost of visual fidelity, while higher values preserve more detail. The default value is typically 75.

ffmpeg -i input.mp4 -c:v libwebp -q:v 80 output.webp

You can also adjust the encoding effort using the -compression_level flag, which takes an integer from 0 (fastest encoding, larger file) to 6 (slowest encoding, best compression ratio). The default value is 4.

ffmpeg -i input.mp4 -c:v libwebp -q:v 80 -compression_level 6 output.webp

Lossless Compression

To enable true lossless compression, set the -lossless flag to 1. In lossless mode, visual quality is completely preserved, and the -q:v parameter dictates the amount of effort spent compressing the file rather than the visual quality.

ffmpeg -i input.mp4 -c:v libwebp -lossless 1 output.webp

Configuring Loop Count

The repetition behavior of animated WebP files is controlled using the -loop flag.

  • 0: Loops infinitely.
  • N (positive integer): Loops the animation N additional times after the initial playback.
  • -1: Plays once without looping.

To create an animated WebP that loops continuously:

ffmpeg -i input.mp4 -c:v libwebp -loop 0 output.webp

To create an animation that plays once and stops:

ffmpeg -i input.mp4 -c:v libwebp -loop -1 output.webp

Complete Command Example

To combine both quality tuning and loop control for an optimized, continuously looping animation at 15 frames per second:

ffmpeg -i input.mp4 -vf "fps=15,scale=640:-1:flags=lanczos" -c:v libwebp -q:v 75 -compression_level 5 -loop 0 output.webp