FFmpeg NVENC: GPU Transcoding Examples for NVIDIA Cards
How to use your NVIDIA GPU in FFmpeg: check NVENC support, full GPU transcoding with -hwaccel cuda, scaling with scale_cuda, picking a GPU, session limits and monitoring, plus the errors you'll hit along the way.
Originally published in April 2019. Reviewed and updated in December 2026 with the current FFmpeg syntax, and merged with my older guides on compiling FFmpeg with NVENC, choosing a GPU, the NVENC session limit and GPU monitoring.
An NVIDIA card has two separate video blocks: NVENC for encoding and NVDEC for decoding. FFmpeg can use both, and when the whole pipeline stays on the GPU, a transcode that keeps a CPU at 100% barely touches it.
A lot of FFmpeg NVENC examples online (including the first version of this page) still use 2019 syntax: -hwaccel cuvid, the nvenc encoder name, presets like slow and llhq. Some of that now prints warnings, and some of it fails outright. Everything below uses the current syntax.
Step 1: Check that your FFmpeg has NVENC
You probably don't need to compile anything anymore. Ubuntu's own ffmpeg package is built with NVIDIA support. On Ubuntu 24.04 (FFmpeg 6.1.1) I checked:
sudo apt install ffmpeg
ffmpeg -hide_banner -encoders | grep nvenc
V....D av1_nvenc NVIDIA NVENC av1 encoder (codec av1)
V....D h264_nvenc NVIDIA NVENC H.264 encoder (codec h264)
V....D hevc_nvenc NVIDIA NVENC hevc encoder (codec hevc)
The same build includes the h264_cuvid / hevc_cuvid / av1_cuvid decoders and CUDA filters: scale_cuda, yadif_cuda, bwdif_cuda, overlay_cuda. What it doesn't include is scale_npp, which needs a non-free build (see the compile section at the end).
The FFmpeg binary loads the NVIDIA libraries at runtime, so the only other thing you need is the proprietary NVIDIA driver. Check it with:
nvidia-smi
If that shows your card, FFmpeg can use it. Which codecs your card can encode depends on its generation: AV1 encoding needs an RTX 40-series (Ada) or newer, for example. NVIDIA's encode/decode support matrix lists every model.
Step 2: Basic NVENC encoding
The simplest form: the CPU decodes, the GPU encodes.
ffmpeg -i input.mp4 -c:v h264_nvenc -b:v 5M -c:a copy output.mp4
Swap h264_nvenc for hevc_nvenc (H.265) or av1_nvenc (AV1) if your card supports them.
This already takes the heaviest job off the CPU, but decoded frames still travel from system RAM to the GPU for every frame. For full speed, keep everything on the GPU.
Step 3: Full GPU transcoding (decode + encode)
This is the command most people are actually searching for:
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 \
-c:v h264_nvenc -preset p5 -b:v 5M -c:a copy output.mp4
-hwaccel cudadecodes on the GPU (NVDEC). FFmpeg picks the right decoder for the input codec automatically.-hwaccel_output_format cudakeeps decoded frames in GPU memory, so they go straight to the encoder.
NVIDIA's own FFmpeg guide uses this pattern. The older -hwaccel cuvid -c:v h264_cuvid form still runs on FFmpeg 6.1, but it prints a deprecation warning telling you to set -hwaccel_output_format cuda explicitly. Use the new form.
One catch: with frames in GPU memory, normal CPU filters (-vf scale=..., yadif, drawtext) won't work on them. Either use CUDA filters, shown below, or drop -hwaccel_output_format cuda and accept the extra copy.
Presets and quality settings
NVENC uses presets p1 to p7: p1 is fastest, p7 is best quality, p4 is the default. The old names (slow, medium, hq, llhq, hp) still appear in FFmpeg 6.1's help, but FFmpeg 9.0 removed a batch of deprecated NVENC options, so new scripts should stick to p1–p7.
Separately, -tune sets the use case: hq (default), ll (low latency), ull (ultra low latency) or lossless.
Some combinations I'd start from:
# Good quality file encode, constant quality (lower -cq = better quality, bigger file)
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 \
-c:v hevc_nvenc -preset p6 -tune hq -rc vbr -cq 24 -b:v 0 \
-spatial-aq 1 -rc-lookahead 32 -c:a copy output.mkv
# Live streaming, constant bitrate, low latency
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.ts \
-c:v h264_nvenc -preset p4 -tune ll -rc cbr -b:v 6M -maxrate 6M -bufsize 12M \
-g 60 -c:a aac -b:a 128k -f flv rtmp://server/live/key
To see every option your build supports:
ffmpeg -h encoder=h264_nvenc
ffmpeg -h decoder=h264_cuvid
Resize on the GPU with scale_cuda
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 \
-vf scale_cuda=-2:720 -c:v h264_nvenc -preset p5 -b:v 3M -c:a copy output_720p.mp4
-2:720 means "720 pixels high, width calculated from the aspect ratio and rounded to an even number". Encoders want even dimensions, so -2 is safer than the -1 you'll see in older examples.
scale_cuda also accepts interp_algo=lanczos for sharper downscaling, and format= to change the pixel format on the GPU.
You can also resize inside the decoder with -resize, which is a cuvid decoder option, so it needs the decoder named explicitly:
ffmpeg -c:v h264_cuvid -resize 1280x720 -i input.mp4 -c:v h264_nvenc -b:v 3M -c:a copy output.mp4
One input, several outputs
A classic ABR ladder in one pass, decoding once:
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 \
-filter_complex "[0:v]split=2[a][b];[a]scale_cuda=-2:1080[v1080];[b]scale_cuda=-2:720[v720]" \
-map "[v1080]" -map 0:a -c:v h264_nvenc -b:v 6M -c:a copy out_1080.mp4 \
-map "[v720]" -map 0:a -c:v h264_nvenc -b:v 3M -c:a copy out_720.mp4
Deinterlace on the GPU
For interlaced TV or camcorder sources, use yadif_cuda (or bwdif_cuda) instead of the old hwdownload,yadif,hwupload_cuda round trip:
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.ts \
-vf yadif_cuda=mode=send_frame -c:v h264_nvenc -preset p5 -b:v 4M -c:a aac -b:a 128k output.mp4
If you need a CPU-only filter in the middle, download the frames, filter, and upload again:
-vf "hwdownload,format=nv12,drawtext=text='Test':fontsize=48,hwupload_cuda"
If your files come from an AVCHD camera, see also deinterlacing MTS video with FFmpeg.
Choose which GPU FFmpeg uses
With more than one NVIDIA card, list them first:
nvidia-smi -L
GPU 0: Quadro P4000 (UUID: GPU-...)
GPU 1: NVIDIA GeForce GTX 1070 (UUID: GPU-...)
That's the pair I ran this on when I first wrote the GPU selection guide. The encoder has its own -gpu option:
ffmpeg -i input.mp4 -c:v h264_nvenc -gpu 1 -b:v 5M -c:a copy output.mp4
For a full GPU pipeline, the decoder must use the same card, so select the device with -hwaccel_device:
ffmpeg -hwaccel cuda -hwaccel_device 1 -hwaccel_output_format cuda -i input.mp4 \
-c:v h264_nvenc -preset p5 -b:v 5M -c:a copy output.mp4
The simplest way, which also catches any other CUDA code in the process, is to hide the other cards with an environment variable:
CUDA_VISIBLE_DEVICES=1 ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:v h264_nvenc output.mp4
Inside that process, GPU 1 is now seen as device 0. One gotcha: CUDA numbers cards by speed by default, which doesn't always match nvidia-smi's PCI order. Set CUDA_DEVICE_ORDER=PCI_BUS_ID if the wrong card lights up.
NVENC session limit on GeForce cards
GeForce cards limit how many NVENC encodes can run at the same time. When I wrote the original patching guide in 2017, Kepler and Maxwell GeForce cards allowed only two sessions, and a third FFmpeg process failed with an "out of memory" error even though memory was fine.
That limit has been raised several times. NVIDIA's NVENC application note now puts it at 8 concurrent encode sessions per system for non-qualified cards, which means GeForce. Quadro/RTX professional and data center cards are limited only by hardware.
Two things to note:
- It's per system, not per card. Two GeForce cards together still give you 8 sessions.
- Decoding (NVDEC) isn't limited the same way.
If you really need more than 8 encodes on GeForce, the community keylase/nvidia-patch still exists and patches the driver library. It's unofficial, it has to be re-applied after every driver update, and it may break with new driver versions, so for a business I'd buy a card without the limit instead.
Monitor GPU and encoder usage
nvidia-smi shows overall GPU usage, but the encoder and decoder have their own counters. For a live view:
nvidia-smi dmon -i 0 -s u
The enc and dec columns are the NVENC and NVDEC load in percent. sm is the CUDA cores, which stays low in a pure transcode. I've watched this on a GTX 1080 Ti while transcoding: if enc is near 100%, you're limited by NVENC, not by the CPU or by FFmpeg.
To see running encode sessions with resolution and fps:
nvidia-smi encodersessions
For a one-line snapshot you can log from a script:
nvidia-smi --query-gpu=index,name,utilization.gpu,utilization.encoder,utilization.decoder,memory.used --format=csv
Common errors
| Error | Cause and fix |
|---|---|
Cannot load libcuda.so.1 |
No NVIDIA driver, or FFmpeg can't see it (common in Docker). Install the driver; in containers use the NVIDIA Container Toolkit with --gpus all. |
Hardware device setup failed for decoder |
Same root cause on the decode side: no usable CUDA device. |
Unknown encoder 'nvenc' |
Old name. Use h264_nvenc or hevc_nvenc. |
No capable devices found / OpenEncodeSessionEx failed: out of memory |
Codec not supported by your GPU, or the session limit is reached. Check the support matrix and nvidia-smi encodersessions. |
Impossible to convert between the formats supported by the filter |
A CPU filter is getting GPU frames. Use a _cuda filter or add hwdownload,format=nv12. |
Driver does not support the required nvenc API version |
Your FFmpeg is newer than your driver. Update the NVIDIA driver. |
I reproduced the first three on a machine without a GPU. They're exactly what you get when the driver isn't there, so nvidia-smi is always the first thing to check.
Compiling FFmpeg with NVENC (only if you need it)
You only need your own build for things the distro package leaves out, like scale_npp, a newer FFmpeg than your distro ships (the current release is FFmpeg 9.1), or non-free codecs. The modern process is much simpler than the 2019 one: no Video Codec SDK zip and no CUDA path hacks for NVENC itself.
sudo apt install build-essential clang git pkg-config nasm yasm libx264-dev libx265-dev libnuma-dev
git clone https://github.com/FFmpeg/nv-codec-headers.git
cd nv-codec-headers && sudo make install && cd ..
git clone https://git.ffmpeg.org/ffmpeg.git
cd ffmpeg
./configure --enable-gpl --enable-libx264 --enable-libx265 \
--enable-cuda-llvm --enable-ffnvcodec --enable-nvenc --enable-nvdec --enable-cuvid
make -j"$(nproc)"
sudo make install
nv-codec-headers must match your driver. If the driver is older than the headers, FFmpeg will fail with the "required nvenc API version" error above. In that case check out an older tag of nv-codec-headers that matches your driver.
For scale_npp you also need the CUDA Toolkit and these extra flags, which make the binary non-redistributable:
--enable-nonfree --enable-cuda-nvcc --enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
On a desktop, the open-source nouveau driver must be off before you install the NVIDIA driver. Ubuntu's ubuntu-drivers install handles that for you.
FAQ
Is NVENC quality as good as x264/x265? At the same bitrate, a slow x264/x265 preset still wins on quality per bit. Modern NVENC (Turing and newer) with p6/p7 and lookahead is close enough for streaming, archives and media servers, and many times faster.
Why is my GPU encode not faster than the CPU? Usually the decode or a CPU filter is the bottleneck. Use -hwaccel cuda -hwaccel_output_format cuda and CUDA filters, then check nvidia-smi dmon to see which block is busy.
Can I use NVENC in Docker? Yes, with the NVIDIA Container Toolkit. Pass --gpus all and make sure the video capability is enabled (NVIDIA_DRIVER_CAPABILITIES=compute,video,utility), otherwise you'll get the libcuda error.
How do I keep a long FFmpeg job running? For streams that must come back after a crash, see automatically restarting FFmpeg.