|Version 2 (modified by slhck, 3 months ago) (diff)|
libvpx is the VP8 video encoder for WebM, an open, royalty-free media file format. This guide is an attempt to summarize the most important options for creating video with libvpx.
To install FFmpeg with support for libvpx, look at the Compilation Guides and compile FFmpeg with the --enable-libvpx option.
Note that in the below examples, the libvorbis audio encoder is used. Make sure your FFmpeg version also includes this (check with ffmpeg -codecs), as the native Vorbis encoder from FFmpeg itself should not be used.
Like most other encoders, libvpx offers a constant bitrate encoding mode, which tries to encode the video in such a way that an average bitrate is reached. This doesn't mean that every frame is encoded with the same amount of bits (since it would harm quality), but the bitrate will be very constrained. You should use constant bitrate encoding if you need your video files to have a certain size, or if you're streaming the videos over a channel that only allows a certain bit rate.
The syntax for setting a constant bit rate is:
ffmpeg -i input.mp4 -c:v libvpx -minrate 1M -maxrate 1M -b:v 1M -c:a libvorbis output.webm
Here, you can choose different values for the bitrate other than 1M, e.g. 500K, but you must set all options (i.e., minrate,maxrate and b:v) to the same value.
libvpx offers two different variable bitrate modes, which are very similar. Like in the x264 encoder (for H.264 video), a constant quality mode will ensure that every frame gets the number of bits it deserves to achieve a certain quality level, rather than forcing the stream to have an average bit rate. This results in overall better quality and should be the default method of encoding video with libvpx.
There are four important parameters for encoding VBR with libvpx, all of which are optional, but there are some caveats:
- -qmin – the minimum quantizer (default 4)
- -qmax – the maximum quantizer (default 63)
- -b:v – the target bit rate setting. If not set, the encoder will choose ~1000 kBit/s as a default, but only when the -crf option is used.
- -crf – the overall quality setting. If not set, the encoder will do "normal" VBR, trying to reach the target bitrate in within the qmin/qmax bounds. If set, the encoder will use CQ mode, and the target bitrate will become the maximum allowed bitrate. The CRF value is 10 by default.
Important: If neither -b:v nor -crf are set, the encoder will use a default bitrate of 256 kBit/s and your result will probably look very bad.
So, how do you set the quality?
- -qmin should be set from 0–4, and -qmax from 50–63. These Q values are quantization parameters, and lower generally means "better quality". If you set the bounds from 0 to 63, this means the encoder has free choice of how to assign the quality. You can try to set -qmax to 50 to get an overall better quality.
- -crf defaults to 10 and seems like a good choice. Lower values mean better quality, and the worst quality is achieved with -crf 63.
- -b:v should be set to your target bitrate if you don't use -crf, or your maximum allowed bit rate if you use -crf. In the latter case, libvpx will try not to overshoot it.
To summarize this, in one example with constant quality and an allowed maximum bit rate of 1 MBit/s:
ffmpeg -i input.mp4 -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis output.webm
Or, if you want to have libvpx just target 2 MBit/s:
ffmpeg -i input.mp4 -c:v libvpx -b:v 2M -c:a libvorbis output.webm
You can also artificially force libvpx to use a certain quality if you specify -qmin and -qmax without any bit rate or CRF value, but this is not an ideal solution.