If you have a notebook that ships with a Nvidia GPU and you're recording screencasts or editing/encoding video, this post might be for you.

This blog post has an accompanying screencast:

I have a ThinkPad X1 Extreme Gen 2 notebook for work. The reason I bought this machine is that it has lots of power which I can make good use of when developing software. Being a higher end notebook, it did come with a Nvidia GeForce GTX 1650 GPU which is a pretty strong beast.

Having a strong GPU is not only helpful for playing games or crunching ML models, it can also be used instead of the CPU when recording a screencast or editing and encoding video. In my personal experience, my notebook likes to mimic the sound characteristics of a jet engine when doing any of these tasks. The reason is, of course, that modern CPUs have many cores, they get hot quickly, and the super slim body of higher end notebooks isn't that suitable for transporting hot air out. Using a GPU will help with that - and it will make encoding and transcoding tasks faster.

The relevant feature is called Nvidia NVENC. This feature performs video encoding, offloading this compute-intensive task from the CPU to the GPU. The encoder is supported in many streaming and recording programs, such as Open Broadcaster Software (OBS), Kdenlive and ffmpeg.

NVENC is a proprietary encoder, so it's not built-in to any of the base-installations in Debian. However, it's not that much work to get it going.

Install dependencies

This assumes that you already have the proprietary Nvidia graphics driver running for your graphical session (X11 or Wayland). If you don't have that, yet, there are many tutorials online.

sudo apt update
sudo apt install nvidia-cuda-toolkit libnvidia-encode1

Install nvidia codec APIs

cd ~/src
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
make
sudo make install

Install ffmpeg

Add a deb-src entry to /etc/apt/sources.lst for your primary packages.

Then install ffmpeg source dependencies:

sudo apt update
mkdir -p ~/src/ffmpeg
cd ~/src/ffmpeg
apt source ffmpeg
sudo apt build-dep ffmpeg

Edit debian/rules to include these CONFIG flags:

--enable-cuda-nvcc                       \
--enable-cuvid                           \
--enable-nvenc                           \
--enable-nonfree                         \
--enable-libnpp                          \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64

Build ffmpeg and dependencies .deb packages:

dpkg-buildpackage -rfakeroot -b -uc -us

Install ffmpeg and dependencies:

sudo dpkg -i ../*deb

Kdenlive settings (optimized for publishing to Youtube)

f=mp4 vcodec=nvenc_h264 acodec=aac ab=384k g=15 profile:v=high global_quality=21 -coder 1 vq=21 -r 29.97 preset=slow bf=2 movflags=faststart pix_fmt=yuv420p

Resources

Closing words

If you liked this post and want to say 'thanks', please head over to our free/libre and open source software page - and if you like one of them, give it a star on Github or Gitlab.