How to Enable FFmpeg GPU Acceleration
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
WRONG — hwaccel 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 cudafor NVIDIA,-vaapi_devicefor Intel. - Monitor GPU usage with
nvidia-smi pmonduring encoding.
Common Mistakes with gpu acceleration
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro