Sunday, May 29, 2022

Apple M1 hardware acceleration of H.264 and H.265 encoding



I have a video file that I want to share with some friends. The file is 413,5 MB and contains 29 minutes of video and audio in 1920x1080 resolution in H.264 video codec. The audio is mono, AAC at 32K Hz sample rate.




Like I said, this file is now 413,5 MB, and it could be quite a lot smaller, if only I compressed it a bit, maybe halved the resolution as well 🤔 


I’ve in the past done a lot of video compressions already (wrote about if before as well, even made it to Hacker News by accident). It just never occurred to me that my beloved FFMPEG tool might not be using the best settings out-of-the-box.  


FFMPEG is an incredibly powerful tool, a real “power tool” and it is really easy to “shoot yourself in the foot” with it.


The thing is, you have to specify EVERYTHING. So you are using a new Apple workstation with the very powerful M1 processor, which contains a built-in H.264 and H.265 dedicated encoder/decoder? Good for you, but FFMPEG will not, I repeat, NOT, use it, unless you ask it, kindly, in the command line, every time you do a conversion.


Having said that… the standard settings kind of do make a lot of sense for most situations…


Using M1 dedicated encoder/decoder


For everyone who found this page by just Ducking and wants to know the magical incantations required to make the M1 tango with FFMPEG, here it is:


h264_videotoolbox


or, in context:


ffmpeg -i input.mp4 -c:v h264_videotoolbox output_h264_VT.mp4


 


The conversion is happening at a staggering 7.56x, so a 29 minutes file only takes 3:50 to re-encode! 






Unfortunately, I immediately notice that the video now has visible artifacts… it really looks a lot worse 😢 





We do get a very small file though, it’s only 90,8 MB for half an hour of full-HD video. But like I said, it’s no fun to watch a horrible mangled video like this.

 
H.265 to the rescue?


Maybe using the newer H.265/HEVC codec will make the video look better?


The full command now reads:


ffmpeg -i input.mp4 -c:v hevc_videotoolbox -tag:v hvc1 output_h265_VT.mp4


  • The option -tag:v hvc1” is added to make sure QuickTime will still recognize the file format… again, don’t ask me why this is necessary. 



Okay… We get a similar compression time of 3:46 minutes. The output file is now 203,5 MB, which is still a 50% reduction from the original. 






There are still artifacts visible in the resulting video file. 🤔 


Now, if we instead use the old-fashioned and laptop boiling method of running the compression over the CPU instead of the dedicated circuitry, maybe the file will be higher quality?


ffmpeg -i input.mp4 -c:v libx265 -tag:v hvc1 output_libx_h265.mp         


40 minutes and a burnt lap later…


Yes, we do have an even smaller file (just 184.6MB now), which also has a better looking video quality 😊 


Conclusions

Hevc_videotoolbox gives you a very fast compression but with an abysmal video quality.
Libx265 will make you wait, for a much better looking and smaller video.







No comments: