How to Use H.264 Hardware Acceleration in FFmpeg on macOS

This article provides a straightforward guide on how to leverage hardware-accelerated H.264 decoding and encoding on macOS using FFmpeg. By utilizing Apple’s VideoToolbox framework, you can significantly speed up video transcoding tasks while reducing CPU usage on both Intel and Apple Silicon (M1/M2/M3) Macs.

Understanding VideoToolbox in FFmpeg

On macOS, hardware acceleration for video decoding and encoding is facilitated through Apple’s VideoToolbox framework. FFmpeg supports this framework natively.

To utilize hardware acceleration for H.264, you will use: * -hwaccel videotoolbox: The input option that enables hardware-accelerated decoding. * -c:v h264_videotoolbox: The output option that enables hardware-accelerated encoding.


Step-by-Step Transcoding Commands

1. Verification of Support

Before starting, verify that your FFmpeg installation supports VideoToolbox. Run the following commands in your Terminal:

To check for the hardware decoder:

ffmpeg -decoders | grep videotoolbox

To check for the hardware encoder:

ffmpeg -encoders | grep videotoolbox

You should see h264_videotoolbox listed in the output for both.


2. Transcoding with Hardware Decoding and Software Encoding

If you want to use the hardware decoder to ingest the video quickly but prefer a software encoder (like libx264) for maximum compatibility or compression efficiency, use this command:

ffmpeg -hwaccel videotoolbox -i input.mp4 -c:v libx264 -crf 23 output.mp4

3. Fully Hardware-Accelerated Transcoding (Decode & Encode)

For the fastest possible transcoding speeds, you can use hardware acceleration for both decoding and encoding. This offloads almost the entire process from the CPU to the Mac’s GPU/Media Engine.

ffmpeg -hwaccel videotoolbox -i input.mp4 -c:v h264_videotoolbox -b:v 5M output.mp4

Adjusting Quality Settings

Because h264_videotoolbox does not support standard -crf flags, quality is managed using the following parameters: