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 videotoolboxTo check for the hardware encoder:
ffmpeg -encoders | grep videotoolboxYou 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-hwaccel videotoolbox: Tells FFmpeg to use macOS hardware to decode the incominginput.mp4video stream.-c:v libx264: Encodes the output using the CPU-based H.264 software encoder.
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-hwaccel videotoolbox: Decodes the input H.264 video using hardware.-c:v h264_videotoolbox: Encodes the output H.264 video using hardware.-b:v 5M: Sets the target video bitrate to 5 Megabits per second. Note: Unlike software encoders that use CRF (Constant Rate Factor), the VideoToolbox encoder relies on bitrate flags (-b:v) for quality control.
Adjusting Quality Settings
Because h264_videotoolbox does not support standard
-crf flags, quality is managed using the following
parameters:
Bitrate control: Define a target bitrate using
-b:v(e.g.,-b:v 8Mfor 8 Mbps).Quality-based variable bitrate (VBR): You can enable quality mode using
-q:v. The scale typically runs from 1 to 100, where higher is better:ffmpeg -hwaccel videotoolbox -i input.mp4 -c:v h264_videotoolbox -q:v 65 output.mp4