Book Image

HTML5 Video How-To

By : Alex Libby
Book Image

HTML5 Video How-To

By: Alex Libby

Overview of this book

<p>Publishing videos online has been around for a number of years, although has really taken off with the advent of video sharing sites such as YouTube. The power of video is huge if done well; it can present a lot of information in a more visually engaging manner than using written text and pictures. Done badly though – it can present real problems to the company or individual hosting the video, that have the potential to cause some real harm!<br /><br />"HTML5 Video How-To" is a practical, hands-on guide that provides you with a number of clear step-by-step exercises, which will help you take advantage of the real power that is behind HTML5 video, and give you a good grounding in using it in your web pages.<br /><br />This book looks at the HTML5 video formats available, and breaks down the mystery and confusion that surrounds which format to use. It will take you through a number of clear, practical recipes that will help you to take advantage of the new HTML5 video standard, quickly and painlessly. <br /><br />You will also learn how to build your own video player using jQuery, or by using one of the pre-built libraries available. We will also take a look at adding functionality such as lightbox effects, or subtitles, as well as how to publish videos to popular hosting sites, such as YouTube or VideoBin. If you want to take advantage of using the new HTML5 video format, then this is the book for you.<br /><br />You will learn everything you need to know to convert videos into the right format, as well as how to display them in your browser or web pages, across multiple platforms.</p>
Table of Contents (8 chapters)

Adding cross-browser support (should know)


In all of the tasks so far, we've looked at support for one video using the WebM format. While this is arguably one of the better formats that is unencumbered with patents (being open source), it means that playback will be limited by default to Opera and Chrome as the other browsers don't support this format by default. In this task, we will take a look at modifying our code to make it work across multiple platforms.

Getting ready

For this task, you need to avail yourself of a copy of the code from the Extending the video code task as we are going to make some changes to this code. Other than this, make sure you have copies of your video in MP4 and OGG formats (while the latter format is not used as much, we will still include it in our code).

How to do it...

  1. The first step is to crack open your text editor of choice and load up a copy of the code from the Extending the video code task:

    <!doctype html>
    <html>
    <head>
    <title>HTML5 Video Test</title>
    </head>
    <body>
    <video width="720" height="400" controls="controls"   poster="trailer_480p.jpg">
      <source src="trailer_480p.webm" />
    </video>
    </body>
    </html>
  2. The first step is to add support for the MP4 format (used by all browsers except Firefox and Opera), so alter your code as follows:

    <video width="720" height="400" controls="controls"   poster="trailer_480p.jpg">
      <source src="trailer_480p.webm" />
    <source src="trailer_480p.mp4"; type="video/mp4" /></video>
  3. We're also going to add in support for OGG format videos:

    <video width="720" height="400" controls="controls"   poster="trailer_480p.jpg">
      <source src="trailer_480p.webm" />
      <source src="trailer_480p.mp4" />
    <source src="trailer_480p.ogv" type="video/ogg" /></video>
  4. The eagle-eyed amongst you will spot that the type and codec settings for the WebM format are not present, so let's correct that now:

    <video width="720" height="400" controls="controls"   poster="trailer_480p.jpg">
    <source src="trailer_480p.webm" 
    type="video/webm" /><source src="trailer_480p.mp4" type="video/mp4" />
      <source src="trailer_480p.ogv" type="video/ogg" />
    </video>

How it works...

The reason for specifying a number of different sources is to allow the browser to pick the right video to play; not all formats are compatible with all browsers. You will see that we've also specified the MIME types for each video format; this is to help the browser to decide whether it can play that video and also which format the video should be played in. If a MIME type is not specified, then the browser has to start downloading it in order to find out whether it can play the video; this can be wasteful of bandwidth.