How do I use FFmpeg and rav1e to create high quality AV1 files?

It looks like this goal can be accomplished in 4 relatively easy steps, thus allowing Ubuntu Focal Fossa 20.04 LTS to get to grips with AV1 encoding with FFmpeg and rav1e!

1. Get a recent copy of rust...

Focal Fossa and rav1e benefit from a newer version of rust so grab a newer copy as follows:

sudo apt-get install curl
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Follow the defaults (just press 'enter') and then run the following to configure your current shell:

source $HOME/.cargo/env

to allow instant access to the newest rust and friends.

2. Install rav1e

Best to install both the executable (the command rav1e --fullhelp will then show options that can be used from within FFmpeg with the -rav1e-params option) as well as the libraries that FFmpeg will use to successfully compile and produce librav1e.

First the executable, using a simple copy and paste code block:

sudo apt-get install nasm build-essential libssl-dev && 
mkdir -pv $HOME/{bin,ffmpeg_sources,ffmpeg_build} && 
cd $HOME/ffmpeg_sources && 
wget https://github.com/xiph/rav1e/archive/refs/tags/v0.5.1.tar.gz && 
tar xvf v0.5.1.tar.gz && cd rav1e-0.5.1 && 
cargo build --release && 
find target -name rav1e -exec install -m 755 {} $HOME/bin \; && 
strip ~/bin/rav1e && \
cd $HOME/ffmpeg_sources && rm -rfv rav1e-0.5.1

Next install the cargo-c applet as well as the libraries and pkg-config file needed by FFmpeg. First run a one-off command to install cargo-c:

cargo install cargo-c

and then compile rav1e again using the installed cargo-c:

cd $HOME/ffmpeg_sources && tar xvf v0.5.1.tar.gz && \
cd rav1e-0.5.1 && \
cargo cinstall --release \
     --prefix=$HOME/ffmpeg_build \
     --libdir=$HOME/ffmpeg_build/lib \
     --includedir=$HOME/ffmpeg_build/include

Best to then remove the rav1e shared libraries, leaving the static libraries for FFmpeg to pick up (FFmpeg has a problem with the shared rav1e libraries in this location on my system):

rm -v $HOME/ffmpeg_build/lib/librav1e.so*

3. Install FFmpeg

Now go to the FFmpeg trac site and install FFmpeg as instructed, remembering to add:

--enable-librav1e

to the ./configure string. Then the hard work is done :)

4. Run the encoder

A very reasonable encode can then be run on the test files mentioned in the original question:

ffmpeg -i sintel_trailer_2k_720p24.y4m -i sintel_trailer-audio.flac \
       -c:v librav1e -qp 80 -speed 4 \
       -tile-columns 2 -tile-rows 2 \
       -c:a libfdk_aac -b:a 128k \
       sintel_trailer_av1.mp4

Things to manipulate here would be:

  • -qp 80: Uses 'quantizer' mode to encode with a range of 0-255. Smaller values are a higher quality, the default is 100. 80 seems to be the sweet spot for this media clip.
  • -speed 4: Selects the speed preset (0-10) to encode with, 0 is best quality while 10 is the fastest. This particular setting is a nice compromise between speed and quality on my system (2nd generation Threadripper).
  • -tile-columns 2 -tile-rows 2 Quite decent speed enhancements can be made by manipulating rav1e's tile based encoding. The example given here splits the encoding and decoding into 4 segments and seems a decent choice for the suggested input video, feel free to experiment though..

This encodes at many times the speed that is seen with FFmpeg and libaom-av1 and arguably produces far better quality. Doubtless this encoding string can be further refined and I will do this as rav1e, FFmpeg and AV1 encoding mature...

5. Gratuitous screenshot

OK so there is an optional 5th step; taking a screenshot of the completed output file running :)

enter image description here

Additional Notes...

  • FFmpeg Documentation: The docs on the FFmpeg implementation of encoding with rav1e, this can also be seen by searching the FFmpeg man pages, either man ffmpeg-all or man ffmpeg-codecs. Remember that native rav1e options can be seen with rav1e --help and added into any FFmpeg command line by using the -rav1e-params option...

  • Updating rust: If you are incrementally updating your copy of rav1e you will often have to update your rust installation as well. The command rustup update will accomplish this.