日本語

Creating an Animated WebP from a Video with FFmpeg

​​FFmpeg​GIF,video,WebP

With FFmpeg, you can make an animated WebP from a video just by setting the output file extension to .webp. By default, however, the file plays once and stops, so add -loop 0 to make it repeat. This is a procedure for making a lighter animated image than GIF, for example to show an operation in a README or blog post. I also include measured file sizes and image quality.

Note

The test environment was Ubuntu 24.04, FFmpeg 6.1.1 (Ubuntu package), and libwebp tools 1.3.2 (Ubuntu webp package). The source video was a 6-second video made only with FFmpeg (640×360, 30 fps). A still gradient background is shown, and a test pattern with a time display moves horizontally across it. For playback checks I used Chromium 157 bundled with Playwright. I did not test real screen recordings, videos with transparency, other FFmpeg versions, or browsers other than Chromium.

The basic command and why to add -loop 0

The following command makes an animated WebP at 10 fps and 480 px wide. fps=10 reduces the number of frames, and scale=480:-1:flags=lanczos shrinks the width (the height is determined by the aspect ratio). The final -loop 0 makes the file repeat.

bash
ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos" -loop 0 output_loop.webp

To check why -loop 0 is needed, I first made a file without -loop. Even without specifying an encoder, libwebp_anim was selected. Part of the output was:

console
$ ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos" output.webp
  Stream #0:0 -> #0:0 (h264 (native) -> webp (libwebp_anim))
Output #0, webp, to 'output.webp':
  Stream #0:0(und): Video: webp, yuv420p(tv, progressive), 480x270 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 10 fps, 1k tbn (default)

When I opened the resulting file with webpinfo, a tool from libwebp, Loop count was 1. The WebP file format specification defines this value as the number of times the animation plays. 1 means it plays once, and 0 means it loops forever.

console
$ webpinfo output.webp | grep -E "Duration|Loop count" | sort | uniq -c
     60   Duration: 100
      1   Loop count      : 1

Duration: 100 is the time in milliseconds until the next frame is shown. Because I set fps=10, all 60 frames have a duration of 100 ms.

-loop is an option for the muxer, the part that writes the WebP file. The help output shows that its default value is 1.

console
$ ffmpeg -hide_banner -h muxer=webp
Muxer webp [WebP]:
    Common extensions: webp.
    Default video codec: webp.
WebP muxer AVOptions:
  -loop              <int>        E.......... Number of times to loop the output: 0 - infinite loop (from 0 to 65535) (default 1)

In output_loop.webp, which I made with -loop 0, Loop count was 0. Both files were 185,188 bytes.

I opened both files in Chromium and took a screenshot 8 seconds after loading.

Two screenshots stacked vertically, taken 8 seconds after opening the WebP files in Chromium: the WebP made without -loop (top) and the one made with -loop 0 (bottom). The top still shows the time display at 05.933, while the bottom shows 02.033
Screenshot 8 seconds after loading. The top stays on the last frame, and the bottom is playing its second loop

The file made without -loop played the 6-second video once and then stopped on the last frame (time display 05.933). The screenshots at 7 and 8 seconds were identical. The file made with -loop 0 showed 02.033, which is in the second loop, at 8 seconds.

FFmpeg 6.1 cannot read the WebP that was made

When I loaded the made WebP into FFmpeg 6.1.1, it returned errors. Some lines of the output and the @ 0x… addresses are omitted.

console
$ ffmpeg -i output_loop.webp -f null -
[webp] skipping unsupported chunk: ANIM
[webp] skipping unsupported chunk: ANMF
…
[webp] image data not found
…
Conversion failed!

ffprobe also reports the width and height as 0. To inspect the contents and measure quality, I used the libwebp tools, which are in the webp package on Ubuntu. webpinfo shows the number of frames, the display times, and the loop count. anim_dump writes each frame out as a PNG.

bash
sudo apt-get install webp
webpinfo output_loop.webp
mkdir frames
anim_dump -folder frames -prefix f_ output_loop.webp   # writes frames/f_0000.png and onward

The FFmpeg Changelog lists "Animated WebP decoder" and "Animated WebP demuxer" under version 9.0.

Controlling size with quality and compression_level

I measured the file size and quality for each setting in the same way. For quality, I used anim_dump to extract each frame from the file, then compared each frame one by one with a lossless video scaled to the same fps and width, using FFmpeg's ssim filter. SSIM is closer to 1 the more similar the result is to the original, and it is exactly 1.000 when every frame matches (the lossless section below shows an example that reached 1.000).

The reference was created by converting to bgra (RGB) in FFmpeg. In my earlier blog post comparing the size and quality of GIFs made with FFmpeg, the reference was converted to rgb24. As a result, the SSIM for the same GIF was 0.960 in that post and 0.963 here, so the numbers from the two posts cannot be compared directly.

The script measure.sh I used is shown below. Run it as ./measure.sh name option….

bash
#!/bin/bash
# usage: ./measure.sh name [ffmpeg options for making the WebP...]
# Makes a WebP from the video at 10fps and width 480, and prints size, time, frame count, and SSIM in one line
name=$1; shift
start=$(date +%s.%N)
ffmpeg -hide_banner -loglevel error -y -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos" "$@" "$name.webp"
end=$(date +%s.%N)
# Reference: a lossless video scaled to the same fps and width
[ -f ref.mkv ] || ffmpeg -hide_banner -loglevel error -y -i input.mp4 \
  -vf "fps=10,scale=480:-1:flags=lanczos,format=bgra" -c:v ffv1 ref.mkv
# FFmpeg 6.1 cannot read animated WebP, so use anim_dump to convert each frame to PNG
rm -rf "dump_$name"; mkdir "dump_$name"
anim_dump -folder "dump_$name" -prefix f_ "$name.webp" >/dev/null
n=$(ls "dump_$name" | wc -l)
cmp="[0:v]format=gbrp,setpts=N/TB[a];[1:v]format=gbrp,setpts=N/TB[b];[a][b]"
ssim=$(ffmpeg -hide_banner -framerate 10 -i "dump_$name/f_%04d.png" -i ref.mkv -lavfi "${cmp}ssim" -f null - 2>&1 | grep -o 'All:[0-9.]*')
printf "%-10s frames=%s  size=%7d bytes  SSIM=%s  time=%.2fs\n" "$name" "$n" "$(stat -c %s "$name.webp")" "${ssim#All:}" "$(echo "$end - $start" | bc)"

For example, running it with -quality 90 gives the following output. time is how long it took to make the WebP, and it varies a little each run.

console
$ ./measure.sh q90 -quality 90 -loop 0
q90        frames=60  size= 281584 bytes  SSIM=0.935734  time=0.41s

All files were made at fps=10, 480 px wide, with -loop 0, and each had 60 frames. For comparison, I also measured a GIF made with a palette (palettegen and paletteuse, same fps and width) against the same reference. Sizes in KB use 1 KB = 1000 bytes.

Setting Size SSIM Encoding time
-quality 50 153KB 0.914 0.34 s
Default (-quality 75) 185KB 0.931 0.38 s
-quality 90 282KB 0.936 0.41 s
-quality 100 476KB 0.942 0.52 s
-compression_level 0 244KB 0.929 0.25 s
-compression_level 6 180KB 0.931 3.07 s
-lossless 1 1,170KB 0.972 3.96 s
-lossless 1 -pix_fmt bgra 1,132KB 1.000 4.25 s
GIF (with palette) 680KB 0.963 -
Horizontal bar chart of the animated WebP file size for each setting, with the SSIM shown beside each bar. The default quality 75 is 185KB, and the GIF is 680KB
File size (horizontal bars) and SSIM for each setting. Pink is the default setting, and gray is the GIF reference

For lossy compression (the default, without -lossless), quality is set with -quality. According to the official FFmpeg encoder documentation, the value ranges from 0 to 100, and the default is 75. Raising it from 50 to 100 increased the size from 153KB to 476KB, about three times larger, but SSIM only went up from 0.914 to 0.942.

-compression_level ranges from 0 to 6, and the default is 4. The documentation says that for lossy encoding, a higher value takes longer to encode but gives better quality at the same file size. In this test, 6 made the file 5KB smaller than the default with the same SSIM, but encoding took about eight times as long. Setting it to 0 made encoding faster, but the file grew to 244KB.

Compared with GIF, the default WebP is about a quarter of the size (185KB vs. 680KB), but the GIF had a higher SSIM. Even with -quality 100, the SSIM did not reach the GIF's 0.963.

For lossless output, also add -pix_fmt bgra

-lossless 1 makes lossless compression (no quality loss). However, with -lossless 1 alone, the SSIM was 0.972, which did not match the reference. Adding -pix_fmt bgra as well brought the SSIM to 1.000, and PSNR was inf (infinity), meaning every frame matched the reference.

bash
ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos" -lossless 1 -pix_fmt bgra -loop 0 output_lossless.webp

After running measure.sh with the name lossless_bgra, I compared the extracted frames with the reference using PSNR. The result is below (only the latter part of the PSNR line is shown).

console
$ ffmpeg -framerate 10 -i dump_lossless_bgra/f_%04d.png -i ref.mkv -lavfi "[0:v]format=gbrp,setpts=N/TB[a];[1:v]format=gbrp,setpts=N/TB[b];[a][b]psnr" -f null -
PSNR r:inf g:inf b:inf average:inf min:inf max:inf

Without -pix_fmt, the encoder receives yuv420p (the basic command output above also shows yuv420p). According to the documentation, lossy WebP supports only YUV420, and lossless WebP supports only RGB. When YUV is passed to lossless encoding, libwebp converts it. When I converted the reference to yuv420p and back, then compared it with the original reference, the SSIM was 0.971, nearly the same as the 0.972 from -lossless 1 alone. With -pix_fmt bgra, the RGB data that FFmpeg converts is stored in the WebP as is. In both cases the file was over 1 MB, larger than the GIF.

Point

Make animated WebP files with -loop 0. For this video, the default -quality 75 gave a file about a quarter the size of the GIF. To improve quality, raise -quality and check the size and appearance. For lossless output, use -lossless 1 -pix_fmt bgra.

​Posted by 86