Mind Dump, Tech And Life Blog
written by Ivan Alenko
published under license CC4-BY
posted in category Systems Software / ffmpeg
posted at 11. Aug '23

Howto Compile ffmpeg 6 With av1_nvenc And Use It In Debian 13 (trixie/testing)

To encode AV1 video on a regular CPU is extremely time consuming, 30s takes like 2 hours, maybe more on Ryzen 5900X. Therefore I want to use AV1 encoder in my nVidia RTX 4800. That was one of reason why I paid eye watering amount (and to try AI images and play games in 4k).

I did a bad job to document everything, sorry if there are mistakes, but the plan is - to compile https://www.ffmpeg.org/ with av1_nvenc support. This requires:

Install Cuda

Cuda is needed to compile AV1 support.

Their instructions:

wget https://developer.download.nvidia.com/compute/cuda/12.2.1/local_installers/cuda-repo-debian11-12-2-local_12.2.1-535.86.10-1_amd64.deb
sudo dpkg -i cuda-repo-debian11-12-2-local_12.2.1-535.86.10-1_amd64.deb
sudo cp /var/cuda-repo-debian11-12-2-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo add-apt-repository contrib
sudo apt-get update
sudo apt-get -y install cuda

Run nvidia-smi to find out CUDA version detected by nVidia driver (also useful to see the power consumption):

nvidia-smi
Fri Aug 11 00:26:21 2023       
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.54.03              Driver Version: 535.54.03    CUDA Version: 12.2     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  NVIDIA GeForce RTX 4080        On  | 00000000:09:00.0  On |                  N/A |
|  0%   52C    P0              16W / 320W |   1185MiB / 16376MiB |     25%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
                                                                                         
+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|    0   N/A  N/A     26487      G   /usr/lib/xorg/Xorg                          546MiB |
|    0   N/A  N/A     26919      G   /usr/bin/kwalletd5                            4MiB |
|    0   N/A  N/A     27155      G   /usr/bin/ksmserver                            4MiB |
|    0   N/A  N/A     27157      G   /usr/bin/kded5                                4MiB |
|    0   N/A  N/A     27160      G   /usr/bin/kwin_x11                           119MiB |
|    0   N/A  N/A     27190      G   /usr/bin/plasmashell                         26MiB |
|    0   N/A  N/A     27222      G   ...c/polkit-kde-authentication-agent-1        4MiB |
|    0   N/A  N/A     27437      G   /usr/bin/nextcloud                           21MiB |
|    0   N/A  N/A     27446      G   ...86_64-linux-gnu/libexec/kdeconnectd        4MiB |
|    0   N/A  N/A     27466      G   /usr/bin/kaccess                              4MiB |
|    0   N/A  N/A     27471      G   ...-linux-gnu/libexec/DiscoverNotifier        4MiB |
|    0   N/A  N/A     27583      G   /usr/bin/konsole                              4MiB |
|    0   N/A  N/A     27600      G   /usr/bin/dolphin                              4MiB |
|    0   N/A  N/A     27757      G   /home/aaa/Stiahnuté/firefox/firefox        316MiB |
|    0   N/A  N/A     27831      G   ...-gnu/libexec/xdg-desktop-portal-kde        4MiB |
|    0   N/A  N/A     28287      G   /usr/bin/krunner                             15MiB |
|    0   N/A  N/A     28354      G   ...86_64-linux-gnu/libexec/baloorunner        4MiB |
|    0   N/A  N/A     31514      G   /usr/bin/ghostwriter                         19MiB |
+---------------------------------------------------------------------------------------+

OpenCL (?)

If I remember correctly also OpenCL should be installed, at least headers:

apt-get install nvidia-opencl-dev nvidia-opencl-icd nvidia-opencl-common

Compile ffmpeg

Clone the source code repository, switch to stable version 6, configure and compile. Not going to install it. You should also have development files for stuff like opus.

git clone https://git.ffmpeg.org/ffmpeg.git
cd ffmpeg
git co release/6.0

./configure --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
make -j 22

You can see an error with message Undefined subroutine &Texinfo::Config::set_from_init_file called at, but it is enough to run make again. It will create ffmpeg binary anyways.

Check if nvenc support has been compiled properly:

./ffmpeg -encoders | grep av1
ffmpeg version n6.0-31-g1ebb0e43f9 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 12 (Debian 12.3.0-5)
  configuration: --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
  libavutil      58.  2.100 / 58.  2.100
  libavcodec     60.  3.100 / 60.  3.100
  libavformat    60.  3.100 / 60.  3.100
  libavdevice    60.  1.100 / 60.  1.100
  libavfilter     9.  3.100 /  9.  3.100
  libswscale      7.  1.100 /  7.  1.100
  libswresample   4. 10.100 /  4. 10.100
 V....D av1_nvenc            NVIDIA NVENC av1 encoder (codec av1)
 A....D wmav1                Windows Media Audio 1

The system ffmpeg use libaom-av1 to encode AV1:

ffmpeg -encoders | grep av1
ffmpeg version 6.0-5 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 13 (Debian 13.2.0-1)
  configuration: --prefix=/usr --extra-version=5 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libglslang --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librist --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --disable-sndio --enable-libjxl --enable-pocketsphinx --enable-librsvg --enable-libvpl --disable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-libplacebo --enable-librav1e --enable-shared
  libavutil      58.  2.100 / 58.  2.100
  libavcodec     60.  3.100 / 60.  3.100
  libavformat    60.  3.100 / 60.  3.100
  libavdevice    60.  1.100 / 60.  1.100
  libavfilter     9.  3.100 /  9.  3.100
  libswscale      7.  1.100 /  7.  1.100
  libswresample   4. 10.100 /  4. 10.100
  libpostproc    57.  1.100 / 57.  1.100
 V....D libaom-av1           libaom AV1 (codec av1)
 V....D librav1e             librav1e AV1 (codec av1)
 V..... libsvtav1            SVT-AV1(Scalable Video Technology for AV1) encoder (codec av1)
 V....D av1_nvenc            NVIDIA NVENC av1 encoder (codec av1)
 V..... av1_qsv              AV1 (Intel Quick Sync Video acceleration) (codec av1)
 A....D wmav1                Windows Media Audio 1

Encode

The options for nvenc AV1 are a bit different and I haven’t time to read through all properly. The classic constant factor from x264, x265 and libaom AV1 -crf xx is not there.

This will encode the file to AV1 with nvidia encoder and use Opus audio codec and Matroska container, -ss is starting position in video file, -t ending position (to cut), -vf to downscale to fullHD from 4k using graphics card. The order of arguments are important.

./ffmpeg -hwaccel cuda -hwaccel_output_format cuda -ss 00:00:06.00 -accurate_seek -t 01:30:50.00 -i '/home/damon/video.mp4' -c:v av1_nvenc -vf scale_npp=1920:1080,fps=30 -c:a opus -b:a 128k -compression_level 10 -channels 2 -f matroska -strict -2 videoav1.mkv

And that’s it. Maybe one day I will write an article about options, but be sure to read this - https://docs.nvidia.com/video-technologies/video-codec-sdk/12.1/ffmpeg-with-nvidia-gpu/index.html. The documentation is written very well. I am really impressed.

Add Comment