What I've learned from working with HTML5 video over a month
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 a kind of simulation of an interactive video similar to Netflix's Bandersnatch series. The story goes upon user choices: adding tomato or pickle, choosing brioche or classic bread, whether garnishing with an egg or an avocado sauce or just skipping it, etc. You get the idea…
This article will be an introduction. Let's get started.
<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? 👏🏼
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>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.
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.
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.
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.
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.
- video/ogg: codecs="theora, vorbis"
- video/mp4: codecs="avc1.4D401E, mp4a.40.2"
- video/webm: codecs="vp8.0, vorbis"
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 an example:
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.
Methods that can be used with the
<video>element are listed below:
You can find the attributes that can be used with the
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.
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.
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.
It indicates whether to use CORS to fetch the related content or not.
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.
Sets or gets whether the sound of a video should be turned off. If you do not specify, it takes false as default.
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.
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.
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.
<video src="foo.mp4" preload="auto" />is the same as
<video src="foo.mp4" preload />.
It tells the browser which/what source the video will play from. It can be a local resource within your website or something exposed through a public URL.
Sets or gets the audio volume of a video. If you do not specify, it takes 1 as default.
Sets or gets the value of the width attribute of a video in CSS pixels to control the amount of width the video will occupy on the page.
Sets or gets the value of the height attribute of a video in CSS pixels to control the amount of height the video will occupy on the page.
Properties that used mainly with the
<video>element are listed below:
Returns the URL of the current video.
Sets or gets the current playback position in a video (in seconds).
Returns a number, representing the length of the video, in seconds. If no video is set, "NaN" is returned. If the video is streamed and has no predefined length, "Inf" is returned.
Returns whether the playback of the video has ended or not.
Returns an error state of the video.
- 1 = MEDIA_ERR_ABORTED: Fetching process aborted by user.
- 2 = MEDIA_ERR_NETWORK: Error occurred when downloading.
- 3 = MEDIA_ERR_DECODE: Error occurred when decoding.
- 4 = MEDIA_ERR_SRC_NOT_SUPPORTED: Video not supported.
Returns whether a video is paused or not.
Sets or gets the speed of the video playback. If you do not specify, it takes 1 as default.
Returns a TimeRanges object representing the played parts of the video.
Returns the current ready state of a video.
- 0 = HAVE_NOTHING: No information on whether the video is ready or not.
- 1 = HAVE_METADATA: Metadata for the video is ready.
- 2 = HAVE_CURRENT_DATA: Data for the current playback position is available, but not enough data to play the next frame/millisecond.
- 3 = HAVE_FUTURE_DATA: Data for the current and at least the next frame is available.
- 4 = HAVE_ENOUGH_DATA: Enough data available to start playing.
Fires when the browser can start playing the video.
Fires when the browser can play through the video up to its end without stopping for content buffering.
Fires when the video ends because the end of the media was reached.
Fires when the current time of the video is changed.
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.
<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.
Then use the classes in your view.
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.
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.
What will you do now and next? 🙌🏼
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.