Pass Linux DRM Devices to FFmpeg for Transcoding

Leveraging Direct Rendering Manager (DRM) nodes allows FFmpeg to bypass overhead and interact directly with Linux graphics hardware. This guide demonstrates how to identify specific hardware devices under /dev/dri/ and explicitly pass them to FFmpeg to execute hardware-accelerated workflows like VAAPI or Intel Quick Sync Video (QSV).

Step 1: Identify Your Linux DRM Devices

Linux maps graphics devices and hardware accelerators to the /dev/dri/ directory. To find the exact file name assigned to your intended transcoding device, list the contents of that folder:

ls -l /dev/dri/

The output generally displays two types of character devices:

[!TIP] For hardware transcoding workflows, always prefer the render node (e.g., /dev/dri/renderD128) over the card node. Render nodes do not require master privileges, making them safer, more efficient, and easier to use inside isolated container environments or multi-user setups.

Step 2: Use the Device Flag in FFmpeg

Depending on the underlying hardware architecture and hardware acceleration framework you are targeting, the syntax for passing the DRM device to FFmpeg varies slightly.

Using VAAPI (Video Acceleration API)

VAAPI is the standard cross-vendor framework for Intel and AMD hardware on Linux. Use the -vaapi_device global option to bind a specific DRM render node:

ffmpeg -vaapi_device /dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -i input.mp4 -c:v h264_vaapi output.mp4

Using Intel QSV (Quick Sync Video)

If you are utilizing Intel’s dedicated QSV runtime library wrappers, map the hardware device context explicitly using the -qsv_device parameter:

ffmpeg -qsv_device /dev/dri/renderD128 -hwaccel qsv -c:v h264_qsv -i input.mp4 -c:v h264_qsv output.mp4

Using Kernel Mode Setting Grabbing (kmsgrab)

When performing real-time screen captures directly from a DRM plane alongside hardware transcoding, specify the device using the -device input modifier flag paired with -f kmsgrab:

ffmpeg -device /dev/dri/card0 -f kmsgrab -image_format bgr0 -i - -vf "hwmap=derive_device=vaapi,scale_vaapi=w=1920:h=1080:format=nv12" -c:v h264_vaapi output.mp4

Step 3: Handle Permission and Access Rights

If FFmpeg returns an error indicating that it cannot open or initialization failed for the DRM device, check user access rights. By default, access to these device nodes is restricted to system administrators or specific groups.

Verify your user groups by typing groups. If your user account is missing access, append it to the video or render groups:

sudo usermod -aG video $USER
sudo usermod -aG render $USER

Log out and back into your session for the system group modifications to take effect.