19

While trying to optimize the size of my home videos I converted some of them to WebM as a test, using Pitivi on Xubuntu. The size difference is significant. The resulting webm-file is only around 10% of the original file, while the picture quality looks pretty much the same to me.

I also tried to do the conversion using avconv so I could script it, but the resulting file looks really crappy. After looking at the rendering settings in Pitivi I tried some parameters: avconv -i $1 -q 5 -qmin 0 -qmax 63 ${1%.*}.webm, but the quality still looks like crap, so it seems I'm missing/using some wrong paramters. Does somebody know which are the correct parameters to use to convert a h264 movie to a webm and get the same quality as from Pitivi?

These are the settings in Pitivi: creating the Pitivi Project Rendering to webm (html5 video) codec configuration

1 Answer 1

55

Note: This information is based on the FFmpeg Wiki on VP9 encoding. Please refer to that article for more information – it will be continuously maintained and extended. For the best results you should use the a recent version of ffmpeg by downloading it from their website (a static build will suffice; it contains the libvps-vp9 encoder).

Variable Bit Rate

VBR encoding gives you the optimal overall quality, since the encoder can freely choose how many bits to assign to a frame. Choose this mode unless you are preparing videos for constant-bitrate streaming.

Option 1: Constant quality encoding

Typically, if you do not want to target a specific file size, you should let the bitrate vary freely, as that will lead to the highest quality. You can do this by setting the bitrate to 0 and the constant rate factor (CRF) to the target quality level:

ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 output.webm

CRF value can be from 0–63. Lower values mean better quality. Recommended values range from 15–35, with 31 being recommended for 1080p HD video. Google has a guide with more info on that.

Option 2: Two-pass encoding with a target bitrate

If you want your file to have a specific target bitrate or file size, you need to specify the rate and use two-pass encoding (which will ensure an optimal quality distribution). Here we're choosing 5 MBit/s, which should be enough for 1080p content.

ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 5M -pass 1 -f webm /dev/null && \
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 5M -pass 2 output.webm

Windows users need to use NUL instead of /dev/null, and a ^ instead of \.

Constant Bit Rate

First of all, libvpx offers constant bitrate and variable bitrate encoding modes. Constant bitrate should be avoided whenever possible (unless you target a specific file size or streaming scenario), since the average quality per file size will be worse. Still, you could try setting a constant bitrate if nothing else works for you, e.g. with 1 MBit/s:

ffmpeg -i input.mp4 -c:v libvpx-vp9 -minrate 5M -maxrate 5M -b:v 5M output.webm

Look at the output and increase or decrease the bit rate to your liking (or file size constraints). For example, you can use 500K or 5M, et cetera.

You have to specify -minrate, -maxrate and the bitrate -b:v in order for the encoder to use CBR. They all have to have the same value—otherwise it'll choose a different target bitrate instead and do VBR encoding, but with bad quality.

Audio

The current audio codec of choice for VP9 encoding is Opus. FFmpeg will choose the necessary encoder and its options by default. If you want to explicitly set -c:a libopus, you can do that as well. Refer to the libopus documentation for more options.

5
  • 4
    This is, currently, the single best guide to encoding VP8 on the whole internet. Well done, I wish I could upvote more that once!
    – evilsoup
    Feb 24, 2013 at 19:00
  • @evilsoup Thank you. I'm still tweaking it and will add a new page on the FFmpeg wiki later.
    – slhck
    Feb 24, 2013 at 19:07
  • This is truly a wonderful answer! I now understand why my video was always in the same low quality, because I was actually always using the default of 256 kBit/s. Thank you very much!
    – jeroen
    Feb 24, 2013 at 21:27
  • What were the size differentials between the original mp4 and the converted webm?
    – switz
    Dec 11, 2014 at 8:54
  • 1
    @Dan I just updated the guide – noticed that it was a little outdated. Glad you found it useful.
    – slhck
    Dec 8, 2017 at 11:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .