日本語

FFmpeg WebM (VP9) vs MP4 (H.264): File Size, Quality, and Encoding Time

​​FFmpeg​H.264,video,VP9,WebM

I converted the same video to WebM (VP9) and MP4 (H.264) with FFmpeg, and measured file size, quality, and the time each conversion took. The results can help you decide which format to export for a website and how much to compress.

To summarize, with no options set, VP9 files were 8 to 31% larger and had higher quality than H.264 files. VP9 encoding took 6 to 13 times as long as H.264. When I matched the quality of the two outputs, VP9 was 8 to 26% smaller for the simple-shape video, but there was almost no difference for the fine-pattern video.

Note

The test environment was a virtual machine running Ubuntu 24.04.5, FFmpeg 6.1.1 (the Ubuntu package), x264 core 164, and 4 CPU cores. The test videos were two clips generated with FFmpeg (10 seconds, 1280×720, 30 fps). I did not use any camera footage. There is no audio. For H.264, I used only the default medium preset. I did not test two-pass encoding, encoding with a specified bitrate, H.265 or AV1, evaluation with VMAF, or playback in browsers.

Test Videos and Conversion Commands

I made two source videos with FFmpeg filters and saved them in a lossless format (FFV1). I decoded the saved videos and compared them frame by frame with the MD5 of the videos generated by the filters. All 300 frames matched for both videos. One video is a moving test pattern of simple shapes (testsrc2). The other is a zooming Mandelbrot set (mandelbrot), where fine patterns move across the whole screen.

bash
ffmpeg -f lavfi -i "testsrc2=size=1280x720:rate=30,format=yuv420p" -t 10 -c:v ffv1 src-simple.mkv
ffmpeg -f lavfi -i "mandelbrot=size=1280x720:rate=30,format=yuv420p" -t 10 -c:v ffv1 src-detail.mkv

First, I converted the videos by specifying only the encoders. MP4 uses libx264 (H.264), and WebM uses libvpx-vp9 (VP9).

bash
ffmpeg -i src-detail.mkv -c:v libx264 -an out.mp4
ffmpeg -i src-detail.mkv -c:v libvpx-vp9 -an out.webm

I measured quality by comparing each frame of the output with the original video, using FFmpeg's psnr and ssim filters. A higher PSNR and an SSIM closer to 1 mean the output is closer to the original. Before comparing, I reset the timestamps of both videos from their frame numbers with setpts=N/(30*TB).

Without this reset, only the MP4 result was measured incorrectly. For the simple-shape video converted with no options, the WebM PSNR was 46.55 dB, the same as with the reset. The MP4 PSNR was 28.31 dB, much lower than the 44.54 dB I got after the reset.

The cause is a difference in time base (time_base). The original MKV and WebM use 1/1000 second, and MP4 uses 1/15360 second. Frame timestamps in the MKV were rounded, such as 0.033 s and 0.067 s. When I re-encoded the MP4 with the time base set to 1/1000 second using -video_track_timescale 1000, the PSNR was 44.54 dB even without the reset. When comparing WebM and MP4, you need to be careful that this difference does not make only the MP4 look worse.

bash
ffmpeg -i out.webm -i src-detail.mkv \
  -lavfi "[0:v]setpts=N/(30*TB),split[a1][a2];[1:v]setpts=N/(30*TB),split[b1][b2];[a1][b1]psnr;[a2][b2]ssim" \
  -f null -

Comparison with Default Settings

File sizes use 1 MB = 1,000,000 bytes. PSNR is the average of Y, U, and V combined. SSIM is the overall (All) value. Encoding times are the minimum and maximum of three runs of the same conversion.

Video Format Size PSNR SSIM Encoding time
Simple shapes MP4 (H.264) 3.78MB 44.54dB 0.9956 2.3–2.5 s
Simple shapes WebM (VP9) 4.08MB 46.55dB 0.9973 29.9–31.3 s
Fine pattern MP4 (H.264) 7.08MB 39.87dB 0.9893 5.7–5.9 s
Fine pattern WebM (VP9) 9.31MB 41.94dB 0.9916 34.2–34.8 s

For both videos, the WebM file was larger and had a higher PSNR. The size difference was about 8% for the simple shapes and about 31% for the fine pattern. VP9 took about 13 times as long for the simple shapes and about 6 times as long for the fine pattern.

This is because the two encoders aim for different quality levels by default. The x264 settings written into the H.264 file show crf=23.0. For VP9, FFmpeg printed the following message during conversion (it appears when -v verbose is set).

text
Neither bitrate nor constrained quality specified, using default CRF of 32

The libvpx encoder source code in FFmpeg 6.1.1 shows that when neither a bitrate nor a CRF is specified for VP9, it uses CRF 32 in constant-quality mode. In ffmpeg -h, the valid CRF range also differs by encoder: libvpx-vp9 accepts integers from -1 to 63, and libx264 accepts decimal values. In practice, H.264 at CRF 23 and VP9 at CRF 32 differed in PSNR by about 2 dB. Since the two defaults do not aim for the same quality, comparing them at default settings cannot tell us which one is smaller.

When Quality Is Matched, Whether VP9 Is Smaller Depends on the Video

Next, I converted the videos at several CRF values and graphed file size against PSNR. For H.264, I used CRF 16 to 34 in steps of 1. For VP9, I used CRF 24, 32, 40, and 48. For VP9, I added -b:v 0 along with -crf.

bash
ffmpeg -i src-detail.mkv -c:v libx264 -crf 21 -an out.mp4
ffmpeg -i src-detail.mkv -c:v libvpx-vp9 -crf 32 -b:v 0 -an out.webm
Two line graphs stacked vertically, with file size (MB) on the horizontal axis and PSNR (dB) on the vertical axis. In the upper graph for simple shapes, the VP9 line (light blue) is above and to the left of the H.264 line (gray), so at the same quality VP9 is smaller. In the lower graph for the fine pattern, the two lines almost overlap. Diamond markers show the default results: H.264 at 3.78MB and 44.54dB and at 7.08MB and 39.87dB, and VP9 at 4.08MB and 46.55dB and at 9.31MB and 41.94dB.
Relationship between file size and quality (PSNR). A line further toward the upper left means higher quality at a smaller file size.

On this graph, a line closer to the upper left means the output reaches a closer match to the original at a smaller file size. For the simple shapes, the VP9 line is above and to the left of the H.264 line. For the fine pattern, the two lines nearly overlap.

For each of the four VP9 results, the table below pairs it with the H.264 result that has the closest PSNR.

Video WebM (VP9) MP4 (H.264) VP9 size (ratio to H.264)
Simple shapes CRF 24: 5.81MB, 50.84dB CRF 17: 6.29MB, 50.77dB 0.92x
Simple shapes CRF 32: 4.08MB, 46.55dB CRF 21: 4.57MB, 46.59dB 0.89x
Simple shapes CRF 40: 2.47MB, 42.57dB CRF 25: 2.95MB, 42.40dB 0.84x
Simple shapes CRF 48: 1.15MB, 39.37dB CRF 29: 1.56MB, 39.11dB 0.74x
Fine pattern CRF 24: 13.65MB, 45.56dB CRF 16: 14.16MB, 45.85dB 0.96x
Fine pattern CRF 32: 9.31MB, 41.94dB CRF 21: 8.81MB, 41.56dB 1.06x
Fine pattern CRF 40: 5.69MB, 38.29dB CRF 25: 5.43MB, 38.07dB 1.05x
Fine pattern CRF 48: 2.75MB, 34.76dB CRF 29: 2.83MB, 34.81dB 0.97x

For the simple shapes, VP9 was 8 to 26% smaller, and the gap grew as the compression became stronger. For the fine pattern, some pairs favored VP9 and others favored H.264, and the difference was about ±6%. I converted H.264 in steps of 1 CRF, so the paired PSNR values do not match exactly. The largest gap was 0.37 dB (VP9 CRF 32 and H.264 CRF 21 for the fine pattern). With these two test videos, WebM was not always smaller at the same quality.

Comparing Appearance at Similar File Sizes

For the simple-shape video, I picked a pair with nearly the same file size (H.264 CRF 32 at 1.09MB and VP9 CRF 48 at 1.15MB). I extracted the frame at the 5-second mark and compared them. I enlarged the checkerboard area of the original video by 2 times.

A checkerboard area enlarged 2 times, shown from top to bottom as the original video, MP4 (H.264) at CRF 32 and 1.09MB, and WebM (VP9) at CRF 48 and 1.15MB. The original shows fine grain inside the squares. In the H.264 version, the grain in the pink and light blue squares is blurred into smudged gray squares. In the VP9 version, more of the grain inside the squares remains than in H.264.
The checkerboard area enlarged 2 times. From top to bottom: the original video, H.264 at CRF 32, and VP9 at CRF 48.

With H.264, the grain in the pink and light blue squares was smeared, and the squares became blurry gray. With VP9, more of the grain inside the squares remained than with H.264. The PSNR was 37.42 dB for H.264 at CRF 32 and 39.37 dB for VP9 at CRF 48, so VP9 was closer to the original.

Adding or Omitting -b:v 0 for VP9 Made No Difference

In the comparisons above, I added -b:v 0 with -crf for VP9. To check whether this option changes the result, I tested with FFmpeg 6.1.1 in the same environment. I compared three cases:

  • No options specified
  • Only -crf 32
  • -crf 32 -b:v 0

The MD5 of the whole file differed among the three, but all three files were 4,075,651 bytes. When I computed the MD5 of only the video data, excluding the container, using -c copy -f md5, all three had the same value.

text
simple-vp9-default.webm MD5=62e62a24bda7a97ff15f35ced14ce806
simple-vp9-crf32.webm MD5=62e62a24bda7a97ff15f35ced14ce806
simple-vp9-crf32-nob.webm MD5=62e62a24bda7a97ff15f35ced14ce806

In the FFmpeg 6.1.1 source code, the default bitrate for the libvpx encoder is 0. When VP9 is given a CRF and the bitrate is 0, it uses constant-quality mode. So in this version, omitting -b:v 0 did not change the result.

Trying Settings to Speed Up VP9 Encoding

To shorten VP9 encoding time, I changed -cpu-used (balance between speed and quality; higher is faster), -row-mt (row-based multithreading), and -deadline in the FFmpeg encoder documentation, and converted at CRF 32. ffmpeg -h encoder=libvpx-vp9 shows the defaults as -deadline good and -cpu-used 1.

bash
ffmpeg -i src-detail.mkv -c:v libvpx-vp9 -crf 32 -b:v 0 -cpu-used 4 -row-mt 1 -an out.webm
Video VP9 settings Size PSNR Encoding time
Simple shapes Default (CRF 32) 4.08MB 46.55dB 30.6 s
Simple shapes -cpu-used 4 -row-mt 1 4.28MB 45.93dB 7.4 s
Simple shapes -deadline realtime 4.12MB 46.09dB 22.0 s
Fine pattern Default (CRF 32) 9.31MB 41.94dB 34.4 s
Fine pattern -cpu-used 4 -row-mt 1 10.00MB 41.39dB 13.5 s
Fine pattern -deadline realtime 9.97MB 41.58dB 44.2 s

With -cpu-used 4 -row-mt 1, encoding time dropped to about a quarter for the simple shapes and about 2.5 times faster for the fine pattern. In exchange, the file size grew by 5 to 7%, and the PSNR fell by 0.5 to 0.6 dB. Even so, it still took longer than the H.264 defaults (2.3–2.5 s and 5.7–5.9 s). With -deadline realtime, the fine pattern took longer than with the default settings. The encoding times are from single runs.

​Posted by 86