Hardware-Accelerated Desktop Capture with FFmpeg gdigrab
This article explains how to capture your Windows desktop using
FFmpeg's built-in gdigrab input device combined with GPU
hardware acceleration. While the gdigrab module uses the
Windows Graphics Device Interface to capture raw screen pixels, you can
achieve low CPU usage and high frame rates by offloading the video
encoding to dedicated hardware encoders such as NVIDIA NVENC, Intel
Quick Sync (QSV), or AMD AMF.
How Hardware Acceleration Works with gdigrab
The gdigrab device captures the Windows desktop at the
software/GDI layer. Because GDI captures raw frames into system memory
(RAM), true end-to-end GPU capture is not native to
gdigrab. However, the most resource-intensive phase of
screen recording is video encoding. By piping frames from
gdigrab directly into your GPU’s hardware encoder, you
eliminate nearly all CPU encoding strain, allowing smooth 60 FPS
recording.
NVIDIA GPUs (NVENC)
To capture the desktop using an NVIDIA graphics card, pair
gdigrab with the h264_nvenc encoder:
ffmpeg -f gdigrab -framerate 60 -i desktop -c:v h264_nvenc -preset p4 -tune ll -b:v 6M -pix_fmt yuv420p output_nvenc.mp4-preset p4: Balanced performance and quality preset for modern NVENC.-tune ll: Low-latency tuning to prevent frame buffer build-up.-b:v 6M: Sets target bitrate to 6 Mbps (adjust based on resolution).-pix_fmt yuv420p: Converts GDI's RGB output to standard YUV 4:2:0 for player compatibility and hardware encoder acceptance.
Intel GPUs (Quick Sync Video / QSV)
For systems with integrated or discrete Intel graphics:
ffmpeg -f gdigrab -framerate 60 -i desktop -c:v h264_qsv -b:v 6M -pix_fmt nv12 output_qsv.mp4-c:v h264_qsv: Invokes the Intel Quick Sync H.264 encoder.-pix_fmt nv12: Intel hardware encoders operate natively with the NV12 pixel format.
AMD GPUs (AMF)
For systems powered by AMD Radeon graphics:
ffmpeg -f gdigrab -framerate 60 -i desktop -c:v h264_amf -quality speed -b:v 6M -pix_fmt yuv420p output_amf.mp4-c:v h264_amf: Invokes the AMD Advanced Media Framework encoder.-quality speed: Prioritizes throughput to prevent dropped frames during active screen capture.
Capturing a Specific Window or Region
You can apply hardware-accelerated encoding to specific regions or individual applications instead of the entire desktop:
Region Capture
ffmpeg -f gdigrab -framerate 60 -offset_x 100 -offset_y 100 -video_size 1280x720 -i desktop -c:v h264_nvenc -b:v 4M -pix_fmt yuv420p region.mp4Specific Window Capture
ffmpeg -f gdigrab -framerate 60 -i title="Calculator" -c:v h264_nvenc -b:v 4M -pix_fmt yuv420p window.mp4Optimizing Performance and Avoiding Stutter
- Match Refresh Rates: Ensure the
-frameratevalue matches your monitor's display frequency (or an even fraction of it, such as 60 on a 120Hz display) to avoid stuttering. - Buffer Size: If you encounter frame drops, append
-probesize 32M -analyzeduration 0before the-iflag to reduce initial pipeline latency. - True Hardware Capture Alternative: If
gdigrabintroduces high CPU overhead on 4K or high-refresh-rate setups, consider using FFmpeg'sddagrab(Desktop Duplication API) filter instead. Unlikegdigrab,ddagrabcaptures directly in GPU VRAM and pipes directly into hardware encoders without traversing system RAM.