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)

Providing fall-back support using a polyfill (should know)


So far, we've concentrated on how to use the HTML5 video tags—this works well in current browsers, but what about older ones? In this age of responsive design, visitors to a website will be using any one of a number of different platforms to view content, and it is not necessarily going to be the same platform from one day to the next. To get around this, we have to provide some form of fall-back support, which will be the subject of our next task.

Getting ready

For this task, you will need to download a copy of mediaElement.js, which is available from http://www.mediaelementjs.com. You will also need copies of your videos; you can reuse the ones created earlier, as long as you have copies in WebM, MP4, and Ogg format. The download contains a number of files, of which you only need a few—you need to extract copies of the following files into a separate folder that we will call mediaelement demo:

  • mediaelement-and-player.js and jquery.js – store in a folder called js

  • flashmediaelement.swf – store in a folder called media; put your video files in the same folder

  • mediaelementplayer.css, background.png, bigplay.png, controls.png – store in the top level media element demo folder

How to do it...

  1. Let's begin with the basic framework, so open up your text editor, and save the following lines into a file named mediaelementdemo.js:

    <!DOCType html>
    <html>
      <head>
        <code>
          <script src="js/jquery.js"></script>
          <script src="js/mediaelement-and-player.min.js"> 
          </script>
          <link rel="stylesheet" href="mediaelementplayer.css" />
        </code>
      </head>
      <body>
      </body>
    </html>
  2. Let's go ahead and add in the video links, beginning with the HTML5 video tag as shown in the following code snippet:

    <video width="700" height="400" controls="controls
      poster="media/trailer_480p.jpg" " preload="none">
      <!-- MP4 for Safari, IE9, iPhone, iPad, Android, and 
        Windows Phone 7 -->
      <source type="video/mp4" src="media/trailer_480p.mp4" />
      <!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
      <source type="video/webm" src="media/trailer_480p.webm" />
      <!-- Ogg/Vorbis for older Firefox and Opera versions -->
      <source type="video/ogg" src="media/trailer_480p.ogv" />
      <!-- Flash fallback for non-HTML5 browsers without 
      JavaScript -->
  3. The second part of this is the fall-back video code—add in the following code snippet immediately below the first one:

    <object width="320" height="240" type="application/x-
      shockwave-flash" data="flashmediaelement.swf">
      <param name="movie" value="flashmediaelement.swf" />
      <param name="flashvars" 
      value="controls=true&file=media/trailer_480p.mp4" />
      <!-- Image as a last resort -->
      <img src="media/trailer_480p.jpg" width="320" 
      height="240" title="No video playback capabilities" />
    </object>
  4. The last step is to add in the script call to .mediaelementplayer():

    </video>
    <script>
      $('video').mediaelementplayer();
    </script>

    If all is well, you will end up with something similar to the following screenshot:

How it works...

The observant amongst you will have noticed something—you would be correct in thinking that the preceding code snippet is very similar to the one we used in one of our earlier recipes. If this is the case, then what's the reason for including it here?

The answer to this forms the key reason for this task: MediaElement.js acts as a polyfill, to provide a form of compatibility with older browsers, while still using the <video> tags. It works by not trying to retrofit backward compatibility to a HTML5 video player, but instead providing forward compatibility and consistency; it starts with a baseline of using the MP4 format in the standard <object> tags, but switching this to the <video> tag if it can detect that the browser being used is able to use this tag.

There's more...

One of the drawbacks of the <video> tags at present is the need to provide fall-back support for older browsers, at least for the next few years. The problem with this is that while development of the tags is still in something of a state of flux, there is no standard fall-back method to use. It's for this reason that people have provided their own polyfills. Some notable examples you can try include Modernizr (http://www.modernizr.com) or html5shiv (http://code.google.com/p/html5shiv/).