Hardware Decoding vs Hardware Encoding in FFmpeg Explained
Learn the difference between FFmpeg hardware decoding with -hwaccel and hardware encoding with a codec such as h264_nvenc, and why mixing them costs copies.

People say "turn on the GPU in FFmpeg" as if it were one switch. It is two, and they are separate. One switch makes the GPU read compressed video and produce pictures. The other makes the GPU take pictures and produce compressed video. Some commands use the first, some use the second, and many use both. Knowing which is which explains why a command that looks accelerated runs no faster than before, and why the same command that works on one machine fails on another. This post defines both terms from scratch, shows the FFmpeg 7.1 options that control each, and explains the cost that appears when you use one without the other. The full tour of encoders and their settings is in the cornerstone guide for this cluster.
Decoding and encoding in plain terms
A video file is a compressed stream. To show it, a decoder turns the compressed data into frames, which are rectangular pictures of pixels. To make a file, an encoder does the opposite: it takes frames and compresses them into a stream. Between the two, filters can change the frames, for example by scaling them.
Every step is arithmetic on a lot of data. A single 1080p frame in the common YUV 4:2:0 layout holds about three million bytes, and a 30 frames per second video needs ninety million bytes of frames every second. Doing that on the CPU is possible but uses cores that you may want for something else. Most GPUs, and the integrated graphics in many CPUs, therefore include fixed-function blocks built to do exactly this work for specific codecs. These blocks decode or encode a fixed list of codecs quickly and with low power use, and they are separate from the GPU's general compute cores.
The two directions use separate blocks, and they differ in difficulty. Decoding follows rules that the codec's specification fixes completely, so every decoder produces the same pictures from the same stream. Encoding is a search: the encoder chooses how to split the picture, which motion vectors to use and how finely to quantise, and a better search gives a smaller file at the same quality. A software encoder such as x264 can spend a lot of time on that search. A fixed-function encoder has a fixed budget, so it is fast, but it may need more bitrate to match the quality of a slow software encode.
The two switches in FFmpeg
In FFmpeg the two directions are controlled in different places on the command line.
Hardware decoding is an input option. -hwaccel is written before -i and applies to that input:
ffmpeg -hwaccel cuda -i input.mp4 -c:v libx264 -crf 20 out.mp4Here the GPU decodes input.mp4, and the CPU encodes with x264. The decoder is still the codec's normal decoder, for example h264. The -hwaccel option asks it to hand the work to the hardware when it can, and to fall back to software when it cannot.
Hardware encoding is a codec choice. You pick a different encoder by name with -c:v, written after the input:
ffmpeg -i input.mp4 -c:v h264_nvenc -b:v 6M out.mp4Here the CPU decodes and the GPU's NVENC block encodes. The encoder h264_nvenc is a separate codec implementation in FFmpeg, with its own options. There is no -hwaccel for encoding, because an encoder is not an accelerated version of another encoder: it is a different one.
You can combine them:
ffmpeg -hwaccel cuda -i input.mp4 -c:v h264_nvenc -b:v 6M out.mp4Both blocks work, and the CPU only moves data. That sentence hides a cost, described below.
What your build supports
FFmpeg lists the methods and encoders compiled into your binary. Check both before writing a command:
ffmpeg -hide_banner -hwaccelsHardware acceleration methods:
cuda
vaapi
qsv
drm
opencl
vulkanffmpeg -hide_banner -encoders | grep -E "nvenc|vaapi|qsv|videotoolbox" V....D h264_nvenc NVIDIA NVENC H.264 encoder (codec h264)
V....D hevc_nvenc NVIDIA NVENC hevc encoder (codec hevc)
V....D h264_vaapi H.264/AVC (VAAPI) (codec h264)
V....D hevc_vaapi H.264/HEVC (VAAPI) (codec hevc)The exact lines depend on the platform and on how FFmpeg was built, and the wording of descriptions differs between versions. A name that is not in the list cannot be used on that binary, whatever your hardware can do. Methods in the first list also need the right driver at run time, so being listed is necessary but does not prove the hardware works.
Where the cost hides: system memory and video memory
A decoded frame lives in memory. With a software decoder it is in system memory, the RAM that the CPU uses. A hardware decoder writes frames into video memory, the GPU's own memory. If the next stage runs on the CPU, FFmpeg has to copy each frame from video memory to system memory. The reverse copy happens when a software-decoded frame goes to a hardware encoder.
For the command with -hwaccel cuda and libx264, each frame decoded on the GPU is copied back to RAM for the CPU encoder. At 1080p that is about three million bytes per frame, which is cheap, but at 4K it is roughly twelve million bytes per frame, and the copies compete with the encoder for memory bandwidth. This is one reason decode-only acceleration often brings a smaller win than expected: the decode is rarely the slow part, and the copy takes some of the gain back.
The following table lists the four combinations.
| Decode | Encode | CPU work | Copies |
|---|---|---|---|
| Software | Software | Everything | None |
| Hardware | Software | Encode, filters | GPU to RAM |
| Software | Hardware | Decode, filters | RAM to GPU |
| Hardware | Hardware | Little | None, if frames stay on the GPU |
The last row only avoids copies when the frames stay in video memory from the decoder to the encoder. By default FFmpeg copies them back to system memory after a hardware decode so that ordinary filters can work on them. To keep them on the GPU, you add -hwaccel_output_format and use GPU filters, which a later post in this cluster covers.
Which switch helps your job
Match the switch to the slow part of the job.
Encoding a long video to a smaller file for archive. Use a software encoder. Quality per bit matters more than speed, and a slow x264 or x265 encode beats a hardware encode at the same bitrate.
Making many streams or a quick preview. Use a hardware encoder. Speed and low CPU use matter most.
Playing or analysing a high-resolution source. Hardware decoding helps, because decoding 4K HEVC in software is heavy.
Scaling or filtering in the middle. Check whether the filter has a GPU version, or the copies will undo the gain.
When a hardware option is wrong for the machine, FFmpeg fails with a clear message. A typical one for a missing NVIDIA driver:
[h264_nvenc @ 0x5581a1c0b800] Cannot load libcuda.so.1
[h264_nvenc @ 0x5581a1c0b800] Cannot load libnvidia-encode.so.1
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate control, width, height or pixel formatThat message means the encoder is compiled in, but the driver libraries are not there. The fix is the driver, not the command.
Limits to know about
Hardware decoders support specific codecs, profiles and bit depths. A GPU that decodes H.264 8-bit may not decode 10-bit H.264, and a GPU that decodes HEVC 10-bit may not decode HEVC 4:2:2. When the hardware cannot handle a stream, -hwaccel falls back to software decoding with a warning, and the command still succeeds, only without the speed-up you expected. Read the log to see which path ran. Hardware encoders also have limits on resolution, bit depth and the number of streams they can handle at once.


