Skip to content

How to Enable FFmpeg GPU Acceleration

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about How to Enable FFmpeg GPU Acceleration. We cover key concepts, practical examples, and best practices.

The Problem

Your FFmpeg command runs without GPU acceleration, using the CPU instead. The process is slow and top shows 100% CPU usage. Hardware acceleration errors like Unknown decoder 'h264_cuvid' or Device creation failed prevent GPU encoding.

Quick Fix

Fix 1: Check GPU Encoder Availability

WRONG — assuming NVENC is installed:

ffmpeg -encoders | grep nvenc
# (empty — NVENC is not available in this FFmpeg build)

RIGHT — check available GPU encoders:

# NVIDIA NVENC:
ffmpeg -encoders | grep nvenc
# V..... h264_nvenc           NVIDIA NVENC H.264 encoder (codec h264)
# V..... hevc_nvenc           NVIDIA NVENC hevc encoder (codec hevc)

# AMD AMF:
ffmpeg -encoders | grep amf
# V..... h264_amf             AMD AMF H.264 Encoder (codec h264)

# Intel QSV:
ffmpeg -encoders | grep qsv
# V..... h264_qsv             H.264 video encoder (codec h264)

If none are available, install a GPU-enabled FFmpeg build.

Fix 2: NVIDIA NVENC Encoding

WRONG — using NVENC without specifying the encoder:

ffmpeg -i input.mp4 -c:v libx264 output.mp4
# (uses CPU — slow)

RIGHT — use h264_nvenc:

ffmpeg -i input.mp4 -c:v h264_nvenc -preset p4 -b:v 5M -c:a copy output.mp4
# (uses GPU — fast)

Fix 3: Full Hardware Transcoding (CUDA + NVENC)

ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 \
    -c:v h264_nvenc -preset p4 -b:v 5M -c:a copy output.mp4

WRONGhwaccel cuda without hwaccel_output_format cuda:

# (decoding happens on GPU but the decoded frames are transferred to CPU memory)

Fix 4: VAAPI (Linux/Intel/AMD)

ffmpeg -vaapi_device /dev/dri/renderD128 -i input.mp4 \
    -vf 'format=nv12,hwupload' -c:v h264_vaapi -b:v 5M output.mp4

Check device availability:

ls /dev/dri/render*
# /dev/dri/renderD128

Fix 5: AMD AMF (Windows/Linux)

ffmpeg -i input.mp4 -c:v h264_amf -usage transcoding -quality quality -b:v 5M output.mp4

Fix 6: GPU-Accelerated Filtering

# Scale on GPU instead of CPU:
ffmpeg -hwaccel cuda -i input.mp4 \
    -vf "scale_cuda=1280:720" -c:v h264_nvenc output.mp4

Fix 7: Multiple GPU Selection

# List NVIDIA GPUs:
nvidia-smi
# GPU 0: RTX 4090
# GPU 1: RTX 3060

# Select GPU:
ffmpeg -hwaccel cuda -hwaccel_device 0 -i input.mp4 -c:v h264_nvenc output.mp4

Use DodaTech's Hardware Acceleration Checker to test GPU encoding, benchmark performance, and recommend optimal settings for your hardware.

Prevention

  • Install FFmpeg with GPU support from your package manager or static builds.
  • Verify encoders with ffmpeg -encoders | grep nvenc (or amf/qsv).
  • Check GPU driver version: nvidia-smi (NVIDIA), vainfo (Intel/AMD).
  • Use -hwaccel cuda for NVIDIA, -vaapi_device for Intel.
  • Monitor GPU usage with nvidia-smi pmon during encoding.

Common Mistakes with gpu acceleration

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world FFMPEG code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does GPU encoding produce larger files than CPU encoding?

GPU encoders prioritize speed over compression efficiency. NVENC typically produces files 10-30% larger than libx264 at the same quality setting. Increase bitrate or use -preset p7 (slowest NVENC) for better compression.

Can I use GPU acceleration for decoding only?

Yes, use -hwaccel cuda for decoding and -c:v libx264 for CPU encoding. This offloads decoding to the GPU while keeping CPU encoding quality: ffmpeg -hwaccel cuda -i input.mp4 -c:v libx264 output.mp4.

What is the difference between CUDA and NVENC?

CUDA is the general-purpose GPU compute API used for decoding (h264_cuvid). NVENC is NVIDIA's dedicated hardware encoder (h264_nvenc). Both use GPU hardware but NVENC is more efficient for encoding. CUDA decoding + NVENC encoding is the fastest pipeline.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro