Decode on the GPU with -hwaccel cuda
Decode H.264 on an NVIDIA GPU with ffmpeg -hwaccel cuda, which uses NVDEC but copies frames back to system memory unless you also set an output format.
Adding -hwaccel cuda before the input tells FFmpeg to decode with the NVDEC engine on an NVIDIA GPU instead of the CPU. The rest of the pipeline is unchanged, so this is the smallest edit that moves decoding off the processor.
ffmpeg -hwaccel cuda -i in.mp4 -c:v libx264 -crf 23 -c:a copy out.mp4Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for helpThe stream mapping still says h264 (native), because the decoder is still FFmpeg's h264 decoder, now with the NVDEC hardware accelerator attached to it. That is how to tell the difference from a dedicated decoder such as h264_cuvid. By default each decoded frame is then copied from video memory back to system memory, so the CPU encoder and any software filters see normal frames.
That copy is why this alone helps little with a software encoder, and why the next step is usually to keep frames on the GPU. Which codecs and profiles NVDEC can handle depends on the GPU generation, and a stream it cannot decode falls back to software with a warning. Pick another card with -hwaccel_device 1. The options are in the hardware acceleration introduction on the FFmpeg wiki.