76

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?

talonmies
  • 70,661
  • 34
  • 192
  • 269
Wang Hai
  • 791
  • 1
  • 6
  • 7
  • 7
    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 Jun 13 '17 at 03:53
  • 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](http://docs.nvidia.com/cuda/video-decoder/index.html) seems help, am I right? – Wang Hai Jun 13 '17 at 07:11
  • Well, the name says it's decoding only. So it may help only partially in your case. – sascha Jun 13 '17 at 10:25
  • 3
    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 Jun 13 '17 at 14:25
  • See [this](https://stackoverflow.com/a/55747785/4675388) answer on what to expect with hardware acceleration using NVIDIA GPUs in FFmpeg. – Dennis Mungai May 03 '19 at 14:11
  • 1
    @llogan why are you marking old questions as duplicates to more recent questions?? – Mike Versteeg May 06 '19 at 11:34
  • @MikeVersteeg Date of question doesn't matter. The two questions are close enough to be duplicates, and the answer by 林正浩 is best of them all. If you prefer I can undo it. – llogan May 06 '19 at 17:53
  • 3
    @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 May 07 '19 at 09:22
  • @MikeVersteeg I was prompted to close this question (and the other you questioned me about) because several users were attempting to post the same answers to each respective question pair. Regarding "This question has been asked before": that's the canned response that the web site puts in there when a question is closed as a duplicate. If you disagree with the wording you should bring it up on [meta]. Anyway, you seem to care about this a lot more than I do. It's obviously generating rep for you (thank slhck for elaborating your original, link-only answer), so I re-opened it. – llogan May 07 '19 at 17:52

4 Answers4

41

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)
slhck
  • 36,575
  • 28
  • 148
  • 201
Mike Versteeg
  • 1,615
  • 14
  • 28
39

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

GetoX
  • 4,225
  • 2
  • 33
  • 30
18

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.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
sascha
  • 32,238
  • 6
  • 68
  • 110
  • 1
    I have tried [ffmpeg with hw accelerate](https://developer.nvidia.com/ffmpeg), like the decode and transcode, it runs almost the same speed compared to soft decode on my laptop (i5-4200U cpu, 740M gpu), while with less cpu load. And from [video codec sdk](https://developer.nvidia.com/nvidia-video-codec-sdk), I doubt it may just use NVENC and NVDEC, not cuda cores. So I want make use of cuda cores. – Wang Hai Jun 13 '17 at 11:13
  • 2
    Why use Cuda cores if there is special hardware? Look... You don't give much info about your use-case and it seems you are missing some basics. Either invest more and be more precise (incl. analysis, what's ffmpeg saying) or make your life easy: use CPU and preset superfast or something similar for your codec (which we don't even know). – sascha Jun 13 '17 at 11:30
  • 1
    Hardware encoders typically generate output of significantly lower quality than good software encoders like x264, but are generally faster and do not use much CPU resource. (That is, they require a higher bitrate to make output with the same perceptual quality, or they make output with a lower perceptual quality at the same bitrate.) (From : https://trac.ffmpeg.org/wiki/HWAccelIntro) – mustafa candan Nov 05 '20 at 16:03
4

For AMD cards, use these -vcodec options:

Windows:
h264_amf
hevc_amf

Linux:
h264_vaapi
hevc_vaapi

ffmpeg -i input.mp4 -b:v 10400k -vcodec h264_amf -vf crop=1920:848:0:116 -c:a copy output.mp4

ffmpeg -i input.mp4 -b:v 10400k -vcodec hevc_amf -vf crop=1920:848:0:116 -c:a copy output.mp4

ffmpeg -i input.mp4 -b:v 10400k -vcodec h264_vaapi -vf crop=1920:848:0:116 -c:a copy output.mp4

ffmpeg -i input.mp4 -b:v 10400k -vcodec hevc_vaapi -vf crop=1920:848:0:116 -c:a copy output.mp4
hacknull
  • 317
  • 1
  • 5