43
votes

I want to use ffmpeg to accelerate video encode and decode with an NVIDIA GPU.

From NVIDIA's website:

NVIDIA GPUs contain one or more hardware-based decoder and encoder(s) (separate from the CUDA cores) which provides fully-accelerated hardware-based video decoding and encoding for several popular codecs. With decoding/encoding offloaded, the graphics engine and the CPU are free for other operations.

My question is: can I use CUDA cores to encode and decode video, maybe faster?

3
Yes, you can use cuda cores to encode and decode video, just like you could with just about any programmable processor. Were you planning to write that software yourself?Robert Crovella
Thanks. I want to trancode many videos at the same time, it's too difficult to write encode/decode myself. The CUDA Video Decoder API seems help, am I right?Wang Hai
Current NVIDIA encode/decode support is via NVENC and NVDEC only, which are HW subsystems not directly related to CUDA and separate from CUDA cores. NVIDIA doesn't provide any supported libraries to accelerate video encode/decode using CUDA any more. So you would need to write the CUDA code yourself, or find 3rd party libraries that do it. If you are asking for links for 3rd party libraries, that question is off-topic for SO. Unless you actually want to do the programming work yourself, this question is off-topic for SO.Robert Crovella
@llogan why are you marking old questions as duplicates to more recent questions??Mike Versteeg
@llogan you're punishing this poster by publicly stating "This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.". That statement is not true but it does create the idea OP was lazy and did not do proper research. So I think it does matter. If this mark is merely intended as a link to a better answer, then it has very poor phrasing.Mike Versteeg

3 Answers

31
votes

FFmpeg provides a subsystem for hardware acceleration, which includes NVIDIA: https://trac.ffmpeg.org/wiki/HWAccelIntro

In order to enable support for GPU-assisted encoding with an NVIDIA GPU, you need:

  • A ​supported GPU
  • Supported drivers for your operating system
  • The NVIDIA Codec SDK
  • ffmpeg configured with --enable-nvenc (default if the drivers are detected while configuring)
14
votes

As Mike mentioned, ffmpeg wraps some of these HW-accelerations. You should use it instead of going for more low-level approaches (official NVIDIA libs) first!

The table shows, that NVENC is probably your candidate.

But: Be careful and do some benchmarking. While GPU-encoders should be very fast, they are also worse than CPU ones in comparison to visual quality.

The thing to check here is: Does a GPU-encoder compete with a CPU-encoder when some quality at some given bitrate is targeted? I would say no no no (except for very high bitrates or very bad quality), but that's something which depends on your use-case. GPU-encoding is not a silver-bullet providing only advantages.

11
votes

Quick use on ​supported GPU:

CUDA

ffmpeg -hwaccel cuda -i input output

CUVID

ffmpeg -c:v h264_cuvid -i input output

Full hardware transcode with NVDEC and NVENC:

ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input -c:v h264_nvenc -preset slow output

If ffmpeg was compiled with support for libnpp, it can be used to insert a GPU based scaler into the chain:

ffmpeg -hwaccel_device 0 -hwaccel cuda -i input -vf scale_npp=-1:720 -c:v h264_nvenc -preset slow output.mkv

Source: https://trac.ffmpeg.org/wiki/HWAccelIntro