FFMPEG 使用笔记

记录 MacOS 下 ffmpeg 的使用。

M1 Mac 启用GPU加速

通过 ffmpeg -hwaccels 查看支持的硬件加速选项。

1
2
3
4
5
ffmpeg -hwaccels

# ......
# Hardware acceleration methods:
# videotoolbox

通过 ffmpeg -codecs | grep videotoolbox 查看所选的硬件加速支持的编解码器

1
ffmpeg -codecs | grep videotoolbox
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
ffmpeg version 5.1.2 Copyright (c) 2000-2022 the FFmpeg developers
  built with Apple clang version 14.0.0 (clang-1400.0.29.102)
  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/5.1.2 --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. 28.100 / 57. 28.100
  libavcodec     59. 37.100 / 59. 37.100
  libavformat    59. 27.100 / 59. 27.100
  libavdevice    59.  7.100 / 59.  7.100
  libavfilter     8. 44.100 /  8. 44.100
  libswscale      6.  7.100 /  6.  7.100
  libswresample   4.  7.100 /  4.  7.100
  libpostproc    56.  6.100 / 56.  6.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 )

开启GPU加速,并指定视频编码器进行编码。要注意的是 VideoToolbox 不支持 CRF,因此必须通过 -b:v 指定码率。

1
2
3
4
5
ffmpeg -hwaccel videotoolbox \
 -i input.mp4 \
 -c:v hevc_videotoolbox -b:v 4M \
 -c:a copy \
 output.mp4

其他平台硬件加速可参考 wiki - HWAccelIntro

HDR 转 SDR

使用 zscale

1
2
3
4
5
6
7
ffmpeg -hwaccel videotoolbox \
-i input.mp4 \
-vf zscale=t=linear:npl=100,format=gbrpf32le,  zscale=p=bt709,tonemap=tonemap=hable:desat=0, zscale=t=bt709:m=bt709:r=tv,format=yuv420p \
-c:v hevc_videotoolbox \
-b:v 4M \
-c:a copy \
output.mp4
0%