GPU-accelerated video processing with ffmpeg
Asked Answered
U

5

90

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?

Uphroe answered 13/6, 2017 at 0:52 Comment(9)
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?Hendecasyllable
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?Uphroe
Well, the name says it's decoding only. So it may help only partially in your case.Julianejuliann
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.Hendecasyllable
See this answer on what to expect with hardware acceleration using NVIDIA GPUs in FFmpeg.Cryptogenic
@Welles why are you marking old questions as duplicates to more recent questions??Cassidy
@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.Welles
@Welles 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.Cassidy
@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 Stack Overflow. 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.Welles
D
52

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

Defoliant answered 25/8, 2020 at 18:50 Comment(2)
This answer was pretty much copy and pasted from the source.Lopez
As it should be.Upheld
C
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)
Cassidy answered 13/6, 2017 at 10:14 Comment(1)
default if the drivers are detected while configuring How do i trigger the "configuring" process? Does it happen every time i run ffmpeg?Upheld
J
19

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.

Julianejuliann answered 13/6, 2017 at 10:24 Comment(3)
I have tried ffmpeg with hw accelerate, 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, I doubt it may just use NVENC and NVDEC, not cuda cores. So I want make use of cuda cores.Uphroe
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).Julianejuliann
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 : trac.ffmpeg.org/wiki/HWAccelIntro)Bonnes
B
9

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
Borden answered 14/4, 2022 at 19:43 Comment(0)
S
0

If you use Kdenlive video editor, one of the best free video editor on Linux, it has already the exporting hardware accelerated option. I takes half of the time of only GPU exporting option. I suggest you NVENC H264 VBR option, it produces file with smaller size and very good quality.

Sunder answered 5/7 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.