H.265 hardware encoding of 4K video on MacBook Pro equipped with M1 Pro chip
Introduction
I installed ffmpeg on my Mac, so I decided to test out the capabilities of my Mac.
Exam environment
We tested a 16-inch Macbook Pro equipped with M1 Pro.
Mac hardware encoder is videotoolbox
The installed ffmpeg properly supported the hardware encoder. You can use the hardware encoder if –enable-videotoolbox is specified in the ffmpeg option.
tomokatsu@snow-under-MacBook-Pro ~ % ffmpeg -codecs | grep tool
ffmpeg version 5.0.1 Copyright (c) 2000-2022 the FFmpeg developers
built with Apple clang version 13.1.6 (clang-1316.0.21.2.5)
configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/5.0.1_1 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-neon
libavutil 57. 17.100 / 57. 17.100
libavcodec 59. 18.100 / 59. 18.100
libavformat 59. 16.100 / 59. 16.100
libavdevice 59. 4.100 / 59. 4.100
libavfilter 8. 24.100 / 8. 24.100
libswscale 6. 4.100 / 6. 4.100
libswresample 4. 3.100 / 4. 3.100
libpostproc 56. 3.100 / 56. 3.100
DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (encoders: libx264 libx264rgb h264_videotoolbox )
DEV.L. hevc H.265 / HEVC (High Efficiency Video Coding) (encoders: libx265 hevc_videotoolbox )
DEVIL. prores Apple ProRes (iCodec Pro) (encoders: prores prores_aw prores_ks prores_videotoolbox )
Try hardware encoding 4K video with H.265 right away
The video was taken at Kohoku Interchange at night.
I tried encoding with the following command.
ffmpeg -i 第三京浜\ 港北インターチェンジ-最大4K.mov -c:v hevc_videotoolbox hevc_videotoolbox_default.mp4
Stream #0:0(und): Video: hevc (hev1 / 0x31766568), yuv420p(tv, bt709, progressive), 3840x2160 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 29.97 fps, 30k tbn (default)
Metadata:
creation_time : 2022-06-04T07:16:16.000000Z
handler_name : Core Media Video
vendor_id : [0][0][0][0]
timecode : 00:00:00;00
encoder : Lavc59.18.100 hevc_videotoolbox
Stream #0:1(jpn): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
creation_time : 2022-06-04T07:16:16.000000Z
handler_name : Core Media Audio
vendor_id : [0][0][0][0]
encoder : Lavc59.18.100 aac
frame= 1548 fps= 52 q=-0.0 Lsize= 22975kB time=00:00:51.66 bitrate=3642.7kbits/s speed=1.72x
Speed=1.72x, which is really fast.
File size has been dramatically reduced.
What is the image quality?
Hmm…it’s a blurry video that is proportional to the file size. I get the impression that it might be difficult for fast-moving images.
hevc_videotoolbox seems to be able to fix the quality
It seems that you can encode with fixed quality by specifying -q:v nn as an option for hevc_videotoolbox.
git.ffmpeg.org Git - ffmpeg.git/commit
Optimally using hevc_videotoolbox and ffmpeg on OSX
I tried encoding it below.
ffmpeg -i 第三京浜\ 港北インターチェンジ-最大4K.mov -c:v hevc_videotoolbox -q:v 50 hevc_videotoolbox_q50.mp4
The file size has become quite large.
Fixed quality (q=50) image quality
The image does not seem to be corrupted, probably because the image does not change the entire screen. It feels pretty good.
frame= 1548 fps= 52 q=-0.0 Lsize= 174735kB time=00:00:51.66 bitrate=27703.6kbits/s speed=1.73x
video:173879kB audio:810kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.026356%
After all, the encoding speed is fast.
Bonus: Encoding speed with libx265
I tried encoding it below.
ffmpeg -i ./第三京浜\ 港北インターチェンジ-最大4K.mov -c:v libx265 -crf 28 -preset fast libx265.mp4
The results are below.
frame= 1548 fps=3.5 q=32.9 Lsize= 24886kB time=00:00:51.66 bitrate=3945.5kbits/s speed=0.116x
Oh… it’s late. It seems difficult to soft encode 4K video.
Encoding result with libx265
An incredible compression ratio.
And the image quality is also fine.
Summary: Mac hardware encoding was 15 times faster than soft encoding
There was a trade-off between file size and image quality, but the speed was fast. Real-time encoding is also possible. By the way, the CPU usage rate during hardware encoding was around 30-50%.
Yes, if you are looking for real-time performance and processing time.
It’s fast anyway, so it seems suitable for applications such as distribution. If you are looking for high image quality, soft encoding seemed better.