Cloudinary Blog

Optimizing Video with Cloudinary and the HTML5 <video> Player: Part 1

Video Optimization With the HTML5 <video> Player

Short-form videos are starting to pop up on the web in places never seen before--hero banners, product pages, ads, social content, and the like. This trend could be problematic because of the many formats and codecs, let alone inadequate expertise on what best to adopt for web consumption. Nowadays, most people are familiar with image formats (JPG, PNG, and so forth) but ask them what HEVC, Vorbis, and VP9 are and their eyes glaze over.

Video Compression and High-Efficiency Coding Matter

Lack of experience and compression knowhow can cause significant user-experience problems. For instance, on a major retail site, I recently ran into a 48 MB video-hero banner. Pulling out the video and encoding it as an H.264 MP4 reduces the size to 1.9 MB. So, despite the desire for more video content, developers have not yet caught up to best practices. How do we get the best of both worlds without creating a disaster like the one above?

Fortunately, Cloudinary offers a simple solution. Requesting different formats and codecs simply involves adding a couple of parameters to the video URL.

Take this original video at 8.67 MB:

Ruby:
cl_video_tag("dog")
PHP:
cl_video_tag("dog")
Python:
CloudinaryVideo("dog").video()
Node.js:
cloudinary.video("dog")
Java:
cloudinary.url().videoTag("dog");
JS:
cloudinary.videoTag('dog').toHtml();
jQuery:
$.cloudinary.video("dog")
React:
<Video publicId="dog" >

</Video>
Angular:
<cl-video public-id="dog" >

</cl-video>
.Net:
cloudinary.Api.UrlVideoUp.BuildVideoTag("dog")
Android:
MediaManager.get().url().resourceType("video").generate("dog.mp4");
iOS:
cloudinary.createUrl().setResourceType("video").generate("dog.mp4")

That's a lot of bytes for a simple non-HD, 13-second-long video. Optimization is clearly called for.

Enter the HTML5 Video Player and Cloudinary

First, have a look at the markup:

<video controls>
   <source src="https://res.cloudinary.com/demo/video/upload/vc_h265,w_1280,c_limit/dog.mp4" type="video/mp4; codecs=hvc1">
   <source src="https://res.cloudinary.com/demo/video/upload/vc_vp9,w_1280,c_limit/dog.webm" type="video/webm; codecs=vp9">
   <source src="https://res.cloudinary.com/demo/video/upload/vc_auto,w_1280,c_limit/dog.mp4" type="video/mp4"> 
</video>

With the HTML5 Video Player, you can include multiple sources for the browser to choose from. The browser then starts at the top of the list and goes down until it finds a version it can play. This way, you can get optimal cross-browser support for video compression with the best possible file delivered for each visitor, with only some simple HTML markup.

Take the first , an H265-encoded MP4 file. (H265 is also known as HEVC, if that wasn’t confusing enough). The reason we list that source first is that it's about 30 percent more efficient than the universally supported conventional standard, H.264 (also known as AVC). Currently, H265 works only on IE, Edge, Safari,and iOS Safari 11 and up; all other browsers would move on to the second listed.

Thankfully, VP9 codec enjoys support from Firefox and Chrome, which is nice because those are the big gaps in H265 support. They are relatively similar in video compression though it can vary by video content, length, resolution, and other aspects. Nonetheless, both codecs do a far better job at video compression than H264. Hence, be sure to give those options to the browser before falling back to that old standard, which is listed as the third option for all outlier browsers that support neither VP9 nor H265.

Why go through this effort? The answer lies in the file sizes for the dog video in each codec:

  • H264: 801kb
  • VP9: 515kb
  • H265: 436kb

There is a ~300kb savings in using H265 or VP9 versus H264. That’s significant. You could also look at this another way-- deliver a higher quality video for the same file size. It's really up to you, perhaps A/B test and see what your visitors prefer: A) Less bandwidth overhead -or- B) Better visuals

One last thing you'll notice is the w_1280,c_limit transformation, a parameter that tells Cloudinary to resize the video to 1280px wide unless the original is smaller than that, which is the case in the dog video. Such a resizing ensures that you're not delivering something way too big, like a 4k video, which most screens cannot actually fully display. You can change that 1280 value to whatever you wish; it's just a good idea to have something there.

Feel free to copy the above code snippet and replace cloud_name with your own and the video public_id with the one that pertains to your case. You can then rest assured that you're delivering the best possible user experience.

Preview Part II

In Part II of this post, we’ll discuss how adaptive bitrate streaming plays into this topic and recommend best practices for longer-form videos. In the meantime, check out our Video Transformation Reference Guide.

Recent Blog Posts

An Eye-Opening Talk: Building Apps for the Next Billion Users in Africa

William (iChuloo) Imoh, who hails from Lagos, Nigeria, recently embarked on a U.S. speaking tour, February 20-March 12, during which he powwowed with technical and product teams and communities at such renowned enterprises as Netlify, Pluralsight, Lucidchart, Twilio, and more in Salt Lake City, Dallas, Las Vegas, and San Francisco. On March 5, he gave an enlightening talk, entitled International Developers and Development: Building for the Next Billion Users at Cloudinary in Santa Clara, California. Below is a synopsis. For details, see the related slides.

Read more
The Debut of the Cloudinary Customer Advisory Board

Focus on customers has always been Cloudinary’s mantra. Because we owe them our success, we are constantly reaching out to our customers, not just for feedback on our offerings, but also for their vision, wish list, and buy-in of what Cloudinary can do to meet their needs and make them succeed. About six months ago, it occurred to us that it would be beneficial if we could meet regularly with those who are behind innovation at our key customers—executives, product gurus, developers, content managers—to swap strategies, product roadmaps, best practices, and such. In particular, we’d like to solicit actionable feedback as a foundation for our plans of product enhancements.

Read more
Media Management With the Cloudinary-Netlify CMS Integration

Static sites and the JAMstack are quickly becoming a standard for developing safe and performant websites with an optimal workflow for developers. Netlify CMS (not to be confused with the company that created it, Netlify) is an open source content management solution that works especially with static site generators such as Gatsby, Hugo, etc... enabling content storage in your Git repository along with your code for easier versioning, multichannel publishing, and direct content updates in Git.

Read more
Vitaly Friedman's Insights on Media Conferences

Vitaly Friedman is a die-hard devotee of beautiful content. Born in Minsk, Belarus, he studied Computer Science and Mathematics in Germany, unearthing in himself a passion for typography, writing, and design in the interim. After a six-year stint as a freelance designer and developer, he co-founded Smashing Magazine, a leading online publication on web design and development. You can follow SmashingMag on Twitter @SmashingMag.

Read more