August 18, 2019 · software macos

Transcode HEVC/H.265 for iOS and macOS

hevc2
Since 2017, all Apple devices that have been released support hardware accelerated HEVC/H.265 playback. As we transition to HEVC/H.265 as an industry standard for video files, we should strive to ensure that they can be played back with hardware acceleration because: a) it preserves battery life, b) it doesn't require installing more apps or codecs, and c) it allows for smoother playback and scrubbing. Currently, many video sharing networks have converged on HEVC/H.265 as a standard as well, although with a caveat: the container (i.e. the file extension) for the video files are usually not supported by either the macOS or iOS native video player. Thankfully, we can use ffmpeg, the de facto multimedia encoder/decider, to swap the container without modifying the video data inside! However, we sometimes have to undergo a lossy conversion for the audio due to restrictions on the audio codec for the mp4 container.

Transcode on macOS

First, install homebrew. Then, compile ffmpeg using the following commands:

brew tap varenc/ffmpeg
brew install varenc/ffmpeg/ffmpeg --with-fdk-aac

Now, simply run the command on any HEVC/H.265 file you wish:

ffmpeg -i [h265_file.mkv] -vcodec copy -c:a libfdk_aac -vbr 5 -pix_fmt yuv420p -tag:v hvc1 -movflags faststart [output_file].mp4

However, the command above reprocesses the audio, even it was originally encoded in the AAC format. What you should first try to do, is run the following command to check if the original video file has audio in the AAC format:

ffmpeg -i [h265_file.mkv] -vcodec copy -acodec copy -pix_fmt yuv420p -tag:v hvc1 -movflags faststart [output_file].mp4

And in case you aren't sure, you can always run the command above, press Ctrl + C after a minute, and try playing the resulting video file using Quicktime. If it plays properly (without showing an error or a "Converting" message), then the full transcode will be successful.

Transcode on Windows

Windows is a bit trickier because we don't have an easy way to compile ffmpeg. Instead, we can download a precompiled version of ffmpeg from the mplayer SourceForge site. Make sure to download and extract the compiled archive and not the source code.

Next, we must download and extract the AAC library into the same directory as the ffmpeg.exe binary. This secondary step is due to licensing issues.

Finally, open up a PowerShell window and run the following command:

.\ffmpeg.exe -i [h265_file.mkv] -vcodec copy -c:a libfdk_aac -vbr 5 -pix_fmt yuv420p -tag:v hvc1 -movflags faststart [output_file].mp4

References