How to reencode H.264 videos to H.265
By Dmitry Kabanov
I had videos in H.264 codec that I wanted to reencode to H.265. The reason is that H.265 (a.k.a. High Efficiency Video Codec, HEVC) is a more modern and efficient format than H.264 (a.k.a. Advanced Video Codec, AVC), which allows to keep the same quality with about twice less storage.
As I make from time to time YouTube, Instagram, and TikTok videos, which I need to backup, it is always good to store your data in the most storage-efficient format.
To reencode the videos, I use ffmpeg. The base command to do this is
ffmpeg -i input.mp4 -vcodec libx265 -vtag hvc1 -acodec copy output.mp4
However, if you use M1 Macbooks, you can speed up the process by using hardware codec:
ffmpeg \
-i input.mp4 \
-hwaccel videotoolbox \
-vcodec hevc_videotoolbox \
-vtag hvc1 \
-acodec copy \
output.mp4
What I have noticed is that the output quality is very bad comparing
to the input quality.
The thing is that ffmpeg
chooses bandwidth rate by itself, and this rate
is relatively low.
The solution is to specify the required bandwidth rate manually. For my videos in this particular case, the original format is 1080x1920 (FullHD) and 60 Hz. For this resolution and frame rate, 16 megabit per second should be enough to preserve the quality.
With the specified variable bandwidth rate (-b:v
argument), the command
is the following:
ffmpeg \
-i input.mp4 \
-b:v 16000k \
-hwaccel videotoolbox \
-vcodec hevc_videotoolbox \
-vtag hvc1 \
-acodec copy \
output.mp4
This way, I compressed several videos from roughly 950 megabytes to 480 megabytes. This is extremely useful, when you need to upload backups to cloud services, especially with the upload speeds from my Internet provider :-)