Important
This feature is experimental. The API is subject to change, and everything may break.
The HWDeviceContext API provides functionality to create and manage hardware device contexts for hardware-accelerated encoding and decoding.
const hwDevice = new ffmpeg.HWDeviceContext(type[, device])type(number): The hardware device type fromffmpeg.constants.hwDeviceTypes(e.g.,VIDEOTOOLBOX,VAAPI)device(string, optional): Device identifier. If not specified, uses the default device for the type.
Returns: A new HWDeviceContext instance
// Create VideoToolbox hardware device context (macOS/iOS)
const hwDevice = new ffmpeg.HWDeviceContext(ffmpeg.constants.hwDeviceTypes.VIDEOTOOLBOX)
// Create VAAPI hardware device context with specific device (Linux)
const hwDevice = new ffmpeg.HWDeviceContext(
ffmpeg.constants.hwDeviceTypes.VAAPI,
'/dev/dri/renderD128'
)Creates a HWDeviceContext instance from an existing native handle. Used internally when retrieving hardware device contexts from other objects.
Parameters:
handle(ArrayBuffer): Native handle to wrap
Returns: HWDeviceContext instance or null if handle is null
Destroys the HWDeviceContext and frees all associated resources. Automatically called when the object is managed by a using declaration.
Returns: void
Hardware device contexts are typically used with CodecContext to enable hardware acceleration:
const hwDevice = new ffmpeg.HWDeviceContext(ffmpeg.constants.hwDeviceTypes.VIDEOTOOLBOX)
const decoder = new ffmpeg.CodecContext(ffmpeg.Codec.AV1.decoder)
decoder.hwDeviceCtx = hwDevice
// Configure getFormat callback to select hardware pixel format
decoder.getFormat = (ctx, formats) => {
const hwFormat = formats.find((f) => f === ffmpeg.constants.pixelFormats.VIDEOTOOLBOX)
return hwFormat ?? formats[0]
}
decoder.open()
// Decoding now uses hardware accelerationThe available hardware device types depend on your FFmpeg build and platform:
- macOS/iOS:
VIDEOTOOLBOX - Linux:
VAAPI,VDPAU,DRM - Windows:
D3D11VA,DXVA2 - Cross-platform:
CUDA,OPENCL
Check ffmpeg.constants.hwDeviceTypes for the full list of available types.