Smooth video playback @Youtube [Linux]

So recently I’ve been researching about how to get a better video playback experience on Linux when browsing sites like Youtube. Video playback from the early days of Linux was most of the times a headache, especially because there were not many “good” alternative to a lot of proprietary codecs. Especially now that flashplayer is long dead and is no longer being supported widely, we look up to html5 to solve our media playback woes online.

But the thing is html5 doesn’t solve our problem yet. On sites like Youtube, vp9 is the codec that is being served by default when html5 is enabled on your browser. Vp9 is a successor to vp8 and is about 5-15% improvement over it’s predecessor but on the downside hardware acceleration for vp9 is not built onto most hardware other than the latest smartphones like the Samsung Galaxy S6, etc.

Hardware based decoding is important because it is more efficient than software based decoding, not only in terms of raw performance but the overall cpu usage goes down resulting in less energy being used on your laptops or mobile devices. Therefore hardware based decoding also helps conserve battery life by a big margin when watching videos.

Even if hardware acceleration of vp9 is built into your hardware, the software side of things aren’t ready yet on almost all platforms since vp9 is still a pretty recent development and the industry needs some time to catch up.

Anyway long story short, I turned towards various browsers at first without doing much research hoping that they would magically solve my problem but that was not the case. After a while I learned about this open sourced api being developed by intel (vaapi) thats allows hardware decoding of various codecs using the built in intel gpu on most CPUs. Vaapi on Arch Linux can be accessed by installing the packages “libva-intel-driver” and “libva”. On other distros should be something similar.

Another thing I discovered while testing the browsers is that Firefox for some reason uses a lot more cpu during video playback than a Google chrome based browser like Chromium although they both rely on software based decoding methods. It probably has something to do with how Firefox renders the video internally.

Anyway, the bad news is that vaapi doesn’t yet support vp9 decoding but what you can do is install a browser plugin that forces h264 codec only and then take advantage of hardware acceleration using vaapi as h264 decoding is supported. In my case I used an external player (mpv) with my custom config  (placed in ~/.config/mpv/config) and a firefox plugin called open-with and I can just right click on any video and select open_with mpv to have awesome video playback with negligible cpu usage.

Before hardware accelerated video playback my average cpu usage on Chromium was around 20-25% where as on Firefox was around 40-50%. After hardware acceleration on my netbook with n3700 processor, cpu usage is less than 10% using Firefox. Power usage dropped from 9-10W to 5-6W. That’s a pretty big improvement not to mention the video playback seemed more crisp compared to the software decoded version.

It might seem that I was complaining about Firefox at the start, but after switching video playback to mpv it seems to be a much better solution than even chromium.

If you have some tricks of your own, feel free to comment and share below! 🙂