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=hevc">
   <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

CoreMedia Adds Cloudinary to its CoreMedia Studio Platform

Today we’re pleased to announce a new technology partnership with CoreMedia, a leading Content Experience Platform provider. CoreMedia users can now leverage Cloudinary’s web-based digital asset management (DAM) solution to organize, search, manage and optimize their media assets, including images and videos, and to orchestrate, preview and deliver digital experiences consistently and optimized across all channels and browsers. The official press release is available here.

Read more
Facial-Surveillance System for Restricted Zones

In Africa, where Internet access and bandwidth are limited, it’s not cost-effective or feasible to establish and maintain a connectivity for security and surveillance applications. That challenge makes it almost impossible to build a service that detects, with facial-recognition technology, if someone entering a building is authorized to do so. To meet the final-year research requirement for my undergraduate studies, I developed a facial-surveillance system. Armed with a background in computer vision, I decided to push the limits and see if I could build a surveillance system that does not require recording long video footage.

Read more
Complex Networks Case Study

Complex Networks has been using Cloudinary since 2014 to manage and optimize images across seven websites and two mobile apps, making editorial workflow more efficient, improving page performance and load time, and increasing user engagement. Cloudinary was instrumental in enabling Complex Networks to redesign its web properties. Without the flexibility that Cloudinary offers to both creative and development teams, it would not have been possible for Complex Networks to achieve such a fast time to market.

Read more
Automate Placeholder Generation and Accelerate Page Loads

If you run a Google search on LQIP you’ll see very few relevant articles, very little guidance, and definitely no Wikipedia articles. In this post, we’ll discuss some of the feedback on LQIP we have gathered from the community and suggest and open for conversation a few approaches based on the built-in capabilities of the Cloudinary service. Specifically, we’ll explain what LQIP are, where they are best used, and how you can leverage them to accelerate page loads and optimize user experience.

Read more
Best Practices for Optimizing Web Page Speed

If you're like most consumers today, you engage more with pictures or videos on a website than text. The stats don't lie - four times as many visitors would rather watch a video about a product than read about it, and sites with compelling images average twice as many views as text-heavy ones.

Read more
A day of fun with Girls Who Code and Cloudinary

During both my computer science studies and work in the tech field, there have not been a lot of women present. While our ranks have grown, women still make up only a small percentage. In many ways, I think the traditionally male-dominated world can be intimidating to women and girls who may be interested in pursuing these types of tech careers.

Read more