Optimally using hevc_videotoolbox and ffmpeg on OSX
Asked Answered
P

3

22

I'm using ffmpeg 4.3.1 to convert videos from h264 to h265 and initially I was excited to discover that I can use my Mac's GPU to speed up the conversion with the flag hevc_videotoolbox.

My Mac hardware is the 10th generation Intel i5 with AMD Radeon Pro 5300

I'm using this command:

ffmpeg -i input_h264.mp4 -c:v hevc_videotoolbox -b:v 6000K -c:a copy -crf 19 -preset veryslow output_h265.mp4

The conversion speeds increased from 0.75x to 4x, almost a 500% improvement !

But then I noticed large filesizes and slightly fuzzy results. Then I noticed that changing the crf or the preset makes no difference, ffmpeg seems to ignore those settings. The only setting that seems to work is the video bit rate (-b:v).

So I started to google around to see how I could get better results.

But except for a few posts here and there, I'm mostly coming up blank.

Where can I get documentation on how to get better results using hevc_videotoolbox? How can I find out what settings work and which ones are ignored?

Prehistory answered 20/11, 2020 at 6:20 Comment(1)
as the ffmpeg tag states: Only questions about programmatic use of the FFmpeg libraries, API, or tools are on topic. Questions about interactive use of the command line tool should be asked on superuser.com or video.stackexchange.com. Please delete this.Revocable
H
31

Use the constant quality mode of videotoolbox on Apple Silicon to achieve high speed, high quality and small size. This works from FFmpeg 4.4 and higher — it's based on this commit.

Note that this does not work with Rosetta 2.

  1. Compile ffmpeg for macOS or use ffmpeg from Homebrew (brew install ffmpeg)

  2. Run with -q:v 65. The value should be 1-100, the higher the number, the better the quality. 65 seems to be acceptable.

For example:

ffmpeg -i in.avi -c:v hevc_videotoolbox -q:v 65 -tag:v hvc1 out.mp4
Hug answered 21/10, 2021 at 20:7 Comment(4)
This is super helpful! Do you know if it's still necessary to use a custom build or is it sufficient to use the ARM64 binary installed by homebrew?Cuspidation
Homebrew build is sufficient but not portable. Because it is dynamic linked.Hug
Awesome, this seems to work for me! It's worth noting that the higher the number the better the quality (it's opposite from the CRF value that x264 uses). 100 is max quality and 1 is bad quality.Cuspidation
For libx264, it's known that -crf 17 or -crf 18 can produce visually lossless output. May I ask if there is a value of -q for hevc_videotoolbox, which works similarly to the above mention, to give a visually lossless output?Plume
V
17

Listing options

Run ffmpeg -h encoder=hevc_videotoolbox to list options specific to hevc_videotoolbox.

Use -b:v to control quality. -crf is only for libx264, libx265, libvpx, and libvpx-vp9. It will be ignored by other encoders. It will also ignore -preset.

hevc_videotoolbox isn't as good as libx265, but it is fast

Like most hardware accelerated encoders, hevc_videotoolbox is not as efficient as libx265. So you may have to give it a significantly higher bitrate to match an equivalent quality compared to libx265. This may defeat the purpose of re-encoding from H.264 to HEVC/H.265.

Avoid re-encoding if you can

Personally, I would avoid re-encoding to prevent generation loss unless the originals were encoded very inefficiently and drive space was more important.

Venetic answered 22/11, 2020 at 2:34 Comment(2)
This answer is outdated. From ffmpeg 4.4 onwards there is a CQ mode in hevc_videotoolbox, see the answer by Philip Mok.Urias
Which answer? - he says to use -q:v and that crf is not possible, so what is outdated? - Or am I missing something?Jello
R
5

VideoToolBox can only use the -b:v setting. The crf is ignored. You can run a few test encodes and get an idea what video bitrate is "equivalent" to the CF you desire, then use that bit rate.

Rebirth answered 21/11, 2020 at 23:5 Comment(1)
Answer is outdated, see the one from Philip Mok. There is a CQ mode now.Urias

© 2022 - 2024 — McMap. All rights reserved.