16

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?

Brajesh
  • 271
  • 1
  • 3
  • 11

3 Answers3

24

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
slhck
  • 36,575
  • 28
  • 148
  • 201
Philip Mok
  • 269
  • 2
  • 8
  • 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? – sstur Nov 14 '21 at 17:45
  • 2
    Homebrew build is sufficient but not portable. Because it is dynamic linked. – Philip Mok Nov 16 '21 at 00:22
  • 3
    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. – sstur Nov 17 '21 at 01:21
  • 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? – Hui Gordon Jun 07 '23 at 15:16
16

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.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • This answer is outdated. From ffmpeg 4.4 onwards there is a CQ mode in `hevc_videotoolbox`, see the answer by Philip Mok. – slhck Dec 22 '22 at 09:40
4

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.

Emma
  • 27,428
  • 11
  • 44
  • 69