FFmpeg NVENC CUDA Encoding Fails: "Failed to query nvenc max version"

I am developing a video processing application that uses FFmpeg + CUDA + NVENC for hardware-accelerated encoding.
However, when I try to encode a video using h264_nvenc, I encounter the following error:

Command:

ffmpeg -y -vsync 0 -hwaccel cuda -i input.mp4 -c:a copy -c:v h264_nvenc output.mp4

Error Message:

[h264 @ 0x55e18ff71000] decoder->cvdl->cuvidGetDecoderCaps(&caps) failed
[h264 @ 0x55e18ff71000] Failed setup for format cuda: hwaccel initialisation returned error.
[h264_nvenc @ 0x55e18f441300] Failed to query nvenc max version: unknown error (402681824): (no details)

System Information:

  • OS: Ubuntu 20.04
  • GPU: NVIDIA RTX 4090
  • CUDA Version: 12.2
  • NVIDIA Driver Version: 535.161.07
  • FFmpeg Version: N-118453-ga28dc06869 (custom-built)

FFmpeg Configuration:

I built FFmpeg with the following options:

./configure --enable-gpl --enable-libx264 --enable-libx265 --enable-nvenc --enable-cuda \
--enable-cuvid --enable-nonfree --enable-cuda-nvcc --enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
make -j$(nproc)
make install

Steps I Have Taken:

1. Updated nv-codec-headers to match my driver version
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
git checkout sdk/12.0
sudo make install
2. Recompiled FFmpeg using CUDA 12.2 and NVENC
./configure --enable-gpl --enable-libx264 --enable-libx265 --enable-nvenc --enable-cuda \
--enable-cuvid --enable-nonfree --enable-cuda-nvcc --enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
make -j$(nproc)
make install
3. Installed Video Codec SDK 12.0.16 and manually copied
cp Video_Codec_SDK_12.0.16/libnvcuvid.so /usr/lib/x86_64-linux-gnu/
ln -s /usr/lib/x86_64-linux-gnu/libnvcuvid.so /usr/lib/x86_64-linux-gnu/libnvcuvid.so.1
cp Video_Codec_SDK_12.0.16/libnvidia-encode.so /usr/lib/x86_64-linux-gnu/
ln -s /usr/lib/x86_64-linux-gnu/libnvidia-encode.so /usr/lib/x86_64-linux-gnu/libnvidia-encode.so.1
4. Verified CUDA is detected in FFmpeg
ffmpeg -hwaccels
Output:
Hardware acceleration methods:
cuda

But this error still occurs