HTML5 Video Adventures: One Month In, What I've Picked Up!
I had a chance to work with HTML5
<video>
in a project over a month and am going to tell you my experiences and knowledge on it.The project is like a playful spin on those cool choose-your-own-adventure Netflix shows, like Bandersnatch. Imagine making fun decisions like adding tomatoes or pickles, going for brioche or classic bread, and topping it off with an egg or avocado sauce—or just keeping it simple. You catch my drift, right?
This article will be an introduction. Let's get started.
1. What is it for?
A
<video>
element is used for playing videos or movies and specifying a video on an HTML document. For example, you could embed a music video on your web page for your visitors to listen to and watch. It is now widely implemented in the major browsers and it has support from major websites such as YouTube and Netflix.It accepts attributes that specify how the video should be played. Attributes include src, preload, autoplay, loop, and more. We'll cover these later on.
Here is a quick example to use it:
That's it! Piece of cake, right? 👏🏼
2. Browser Support
See here for more detailed browser support.
3. Sources
You can define multiple sources by using the
<source>
element as a fallback in case the user's browser does not support one of them.Sort the sources as webm, then ogg, and then mp4, since the mp4 is more widely supported from the browsers. It's a good practice of using it as playback.
Any text between the
<video>
and </video>
tags will be displayed in browsers that do not support the <video>
element. For example:When the browser parses the
<source>
tag, it uses the optional type attribute to help decide which file to download and play. If the browser supports WebM, it will play WebM video, if not, it will check if it can play MPEG-4 videos, and so on.If you're using the<source>
element, I recommend you to add the type attribute to it. This way, the browser can immediately skip a format it doesn't support without trying to play it and wasting resources.
4. Formats and Browser Support
4.1. MP4
The MP4 container format with the H.264 video codec and the AAC audio codec is natively supported by desktop/mobile Internet Explorer, Safari, and Chrome, but Chromium and Opera do not support the format. IE and Chrome also support the MP3 audio codec in the MP4 container, but Safari does not. Firefox/Firefox for Android/Firefox OS supports the format in some cases, but only when a third-party decoder is available, and the device hardware can handle the profile used to encode the MP4.
See here for more detailed browser support.
4.2. WebM
The WebM format is based on a restricted version of the Matroska container format. It always uses the VP8 or VP9 video codec and the Vorbis or Opus audio codec. WebM is natively supported in desktop and mobile Gecko (Firefox), Chrome, and Opera, and support for the format can be added to Internet Explorer and Safari (but not on iOS) by installing an add-on.
See here for more detailed browser support.
4.3. Ogg
The Ogg container format with the Theora video codec and the Vorbis audio codec is supported in desktop/mobile Gecko (Firefox), Chrome, and Opera, and support for the format can be added to Safari (but not on iOS) by installing an add-on. The format is not supported in Internet Explorer in any way.
See here for more detailed browser support.
5. Codecs
You can think of a video file as a container (like a ZIP file), that contains the encoded video stream and an audio stream. There are many different types of container formats and unfortunately, there isn't a single 'one-ring' format that will work in all browsers. Thankfully, we can get coverage for all modern (and mobile) browsers using only two formats: WebM typically packages Vorbis or Opus audio with VP8/VP9 video. This is supported in all modern browsers, though older versions may not work. MP4 often packages AAC or MP3 audio with H.264 video. This is supported also supported in all modern browsers, as well as Internet Explorer. Ogg tends to use Vorbis audio and Theora video. This is best supported in Firefox and Chrome but has been superseded by the better quality WebM format.
5.1. Common Values with Codecs
- video/ogg: codecs="theora, vorbis"
- video/mp4: codecs="avc1.4D401E, mp4a.40.2"
- video/webm: codecs="vp8.0, vorbis"
6. Detecting Support
The canPlayType()method checks and returns a string whether the browser can play the specified video type or not. It can return one of the following values: "probably": The browser most likely supports this video type. "maybe": The browser might support this video type. "" (empty string): The browser does not support this video type.
Here is example:
7. Media Fragment
Adding a media fragment to the media URL, you can specify the exact portion you want to play. To add a media fragment, you simply add #t=[start_time],[end_time]to the media URL.
8. Methods
Methods that can be used with the
<video>
element are listed below:9. Attributes
You can find the attributes that can be used with the
<video>
element below:9.1. Autoplay
Tells the browser whether a video should start playing as soon as it is ready. If you do not specify, it takes false as default.
Be aware: Chrome's autoplay policies are changed in April of 2018. It is simple: If you would like to start the video immediately (using autoplay attribute), then you should include the muted attribute as well or the user has to get interacted with the video first (click, tap, etc.). It's very rational when you think from the user's perspective. Because, you know, nobody wants to get puzzled with the loud sound on entering a website.
9.2. Controls
Tells the browser to include its built-in video controls like play/pause, volume, etc. If you do not specify, it takes false as default which means no controls will be shown.
9.3. ControlsList
Using the controlsList, you can customize(hide) Chrome's native media controls such as the download, fullscreen and remoteplayback buttons (Chrome 58+ only). It can include nodownload, nofullscreen, and noremoteplayback attributes.
9.4. Crossorigin
It indicates whether to use CORS to fetch the related content or not.
9.5. Loop
It tells the browser whether the video should be playing over again, every time it has finished. If you do not specify, it takes false as default.
9.6. Muted
Sets or gets whether the sound of a video should be turned off. If you do not specify, it takes false as default.
9.7. playsinline - webkit-playsinline
A boolean attribute indicating that the video is to be played "inline", that is within the element's playback area. See here for detailed information.
9.8. Poster
Sets or gets the value of the poster attribute of a video. You can think of the poster attribute as a savior of your
<video>
element. If the video stops playing or crashes, the poster image will be shown. Or if the file of the video element does not support the browser, the poster image will be shown again. Or, if you want to show the video is loading/buffering, then using a loading gif in the poster attribute will be rational.The absence of this attribute does not imply that the video will always be played in fullscreen.
9.9. Preload
It intends to provide a hint to the browser about what the UA thinks will lead to the best user experience. Sets or gets the value of the preload attribute of a video. If you do not specify, its value depends on the browser default.
- none: It tells the browser should NOT load the video when the page loads.
- metadata: It tells the browser should load only metadata when the page loads.
- auto: It tells the browser should load the entire video when the page loads.
10. Events
10.1 Caplay
Fires when the browser can start playing the video.
10.2. CanplayThrough
Fires when the browser can play through the video up to its end without stopping for content buffering.
10.3. Ended
Fires when the video ends because the end of the media was reached.
11. Best Practice
Adding videos to your website can sometimes cause pain. Users love high-resolution videos, but if your website isn't optimized to handle them, such content can cause performance problems.
Here are a few tips:
- Do not send a 1080p video to a mobile device like a phone: That doesn't make much sense, the video will likely be too big for the screen, and the bandwidth required to send the video may use all of the user's limited data connection extremely quickly. Use 720p or 480p instead.
- Use a CDN: CDN is a network of servers that store cached copies of your site’s static assets. In addition to improving redundancy, CDNs ensure faster content delivery as the content is served from the nearest edge server. This is essential for large files such as videos. That way, users don’t have to wait forever for your videos to download and buffer.
- Offer both standard (MP4) and edge source file types (WebM): You reward users who have a client with advanced compression and quality capabilities (WebM) but provide a fallback to widely supported formats (MP4s).
- Remove audio from muted videos: If you have a video with the audio muted, you can remove the audio data from the video file to save on the size of the video.
- Use data compression tools: Data compression tools like Blazemp and HandBrake reduce the size of video files without degrading image quality. Smaller media files load faster, so you can save users a huge amount of time by compressing all of your videos.
- Use poster image: A poster image that can be displayed until playback begins. This gives viewers a meaningful idea of the content without needing to download the video or start playback.
- Specify the video size: In your HTML or CSS, be sure to define the width and height of videos so that the browser can allocate the necessary bandwidth with no extra work.
12. Centering Video
Centering the
<video>
can be quite annoying if you are trying to make it responsive for both desktop, mobile, or tablet, etc. Don't worry, you can find a demo below using just CSS.There will be a video and a parent of it as a container for styling.
13. Controlling Fullscreen
If you are planning the control fullscreen mode on your own, you should not forget the behavior of all browsers, because the native functions in every one of them are different. Here are the two quick snippets for getting in to and out from fullscreen mode.
14. Bonus: HandBreak — Open Source Video Transcoder
HandBrake is a tool for converting video from nearly any format to a selection of modern, widely supported codecs. You can download it directly from here.
Here are a few great features:
- You can transcode multiple videos with batch scanning and queueing of encodes.
- Supports many presets.
- Constant Quality or Average Bitrate Video Encoding.
- Check out the all features.
Conclusion
HTML5 Video players started simple but have grown quite advanced (think YouTube and Netflix), and since it's all just HTML, CSS, and JavaScript, any developer can create impressive players. Including video on the modern web is easier than ever before and opens some amazing new possibilities.
What will you do now and next? 🙌🏼
Do not stop learning!
I hope, it helps you to understand and get started with HTML5 Video. If you know anyone that would benefit from this guide I would love if you recommended it to them. Thank you for following this guide and reading through to the end, see you on another one.