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 codec definitions (must know)


Throughout most of this section, we've looked at code that will allow you to play a number of different videos. In most cases, the browser will work out automatically which video to play; in some instances, it may not get it right or refuse to play any of the videos listed in the code. In this task, we take a look at how to specify a codec for each video, which will help the browser determine how to play the chosen video properly.

Getting ready

All we need for this is a copy of the code from the previous task—the codecs should already be present on your PC if you installed them in the first section of this book.

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 Adding cross-browser su pport 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" type="video/webm" />
      <source src="trailer_480p.mp4" type="video/mp4" />
      <source src="trailer_480p.ogv" type="video/ogg" />
    </video>
    </body>
    </html>
  2. The next 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; 
      codecs='avc1.42E01E, mp4a.40.2'" /></video>
  3. We're also going to add in support for OGG format videos 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; 
      codecs="avc1.42E01E, mp4a.40.2"' />
    <source src="trailer_480p.ogv" type='video/ogg; 
      codecs="theora, vorbis"' /></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; 
      codecs="vp8, vorbis"' /><source src='trailer_480p.mp4; type='video/mp4; 
      codecs="avc1.42E01E, mp4a.40.2"' />
      <source src='trailer_480p.ogv; type='video/ogg; 
      codecs="theora, vorbis"' />
    </video>

How it works...

Working with codecs, particularly within the HTML5 arena, can open up a real minefield of confusion. The key to understanding it is that enabling codec support increases the likelihood that a browser will be able to correctly determine whether it is able to play that particular video correctly.

In the previous code, I've specified some typical codecs that can be used—while the codecs for WebM and Ogg are straightforward, the one for MP4 (or MPEG-4) is less so; the codec used here represents the Baseline profile for H.264 and the Low-complexity profile for AAC.

Note

You can use other higher-level profiles but these will demand more CPU processing power to decode, although they will be better compressed.

There's more...

It is perfectly possible to use a number of different codecs with particular video types—one good example is the MP4 format. Care should be exercised when choosing an appropriate codec. If one is chosen that is not a popular codec, then it is possible that the video may not play. The reason for this is that if the browser recognizes the file type (either inline in the code or via the .htaccess file), but doesn't "see" the appropriate codec installed, it will then refuse to play the video.