You need to install ffmpeg with hardware acceleration. You can either build it yourself or use prebuilt deb packages from here: Hardware decoding in mpv player - #6 by Lissanro (scroll down until you see “ffmpeg with nvmpi” section then copy and paste into a terminal a block of commands to download and install ffmpeg with nvmpi).
For achieving the best performance, use nvmpi not only for encoding but for decoding too (nvmpi supports HW decoding of
mpeg4), for example:
ffmpeg -c:v hevc_nvmpi -i input.mp4 -c:v h264_nvmpi output.mp4 # Decode input.mp4 with HEVC and encode it to H264
You can use jtop to verify that you are using NVDEC and NVENC while decoding/encoding with ffmpeg.