HEVC Hardware Acceleration in FFmpeg on macOS
This article provides a quick and direct guide on how to utilize macOS’s native VideoToolbox framework to perform hardware-accelerated HEVC (H.265) decoding and encoding using FFmpeg. By leveraging your Mac’s dedicated graphics hardware (Apple Silicon or Intel Quick Sync), you can significantly reduce CPU load and drastically speed up video transcoding times.
Prerequisites
To use hardware acceleration on macOS, you must have FFmpeg installed with VideoToolbox support. You can easily install it using Homebrew:
brew install ffmpegTo verify that your FFmpeg installation supports VideoToolbox, run the following commands in your terminal:
# Check for hardware-accelerated decoders
ffmpeg -decoders | grep videotoolbox
# Check for hardware-accelerated encoders
ffmpeg -encoders | grep videotoolboxYou should see hevc_videotoolbox listed in both
outputs.
The Transcoding Command
To transcode a video using both hardware-accelerated decoding and encoding, use the following FFmpeg command structure:
ffmpeg -hwaccel videotoolbox -i input.mp4 -c:v hevc_videotoolbox -c:a copy output.mp4Command Breakdown:
-hwaccel videotoolbox: Tells FFmpeg to use Apple’s VideoToolbox framework to decode the input video file using hardware acceleration.-i input.mp4: Specifies the path to your source video.-c:v hevc_videotoolbox: Sets the video encoder to the hardware-accelerated HEVC encoder.-c:a copy: Copies the audio stream from the source directly to the destination without re-encoding, saving time and maintaining original audio quality.output.mp4: The name of your output file.
Controlling Output Quality and Bitrate
Hardware encoders handle rate control differently than software
encoders like x265. To control the quality and size of your
output file, you can specify a target bitrate or a quality profile.
Method 1: Target Bitrate (Recommended)
Specify a target bitrate using the -b:v flag. For
example, to set the video bitrate to 5 Megabits per second (5000k):
ffmpeg -hwaccel videotoolbox -i input.mp4 -c:v hevc_videotoolbox -b:v 5000k -c:a aac output.mp4Method 2: Constant Quality
You can use the -q:v flag to set a quality factor. For
VideoToolbox, this is a scale from 1 to 100, where higher numbers
represent better quality:
ffmpeg -hwaccel videotoolbox -i input.mp4 -c:v hevc_videotoolbox -q:v 65 -c:a aac output.mp4