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)

Adapting for iPads/iPhones/Android (should know)


Throughout this chapter, we've looked at the various elements required to play back HTML5 video in your pages. Whilst the code will work in modern desktop browsers, it will likely fail on handheld devices—we are going to rectify this in our next task.

Getting ready

For this task, you will need to ensure that you have videos that are of a suitable size for playback on mobile devices; you can try using a free conversion service such as Online-Convert.com at http://www.online-convert.com to convert the videos down to a preset size depending on which platform(s) you want to support.

How to do it...

  1. Open up your text editor of choice and add in the following lines replacing trailer_480p with the name of your video:

    <!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 first change we need to make is to the poster—iOS 3.x doesn't like the poster attribute, so unfortunately this needs to be removed:

    <video width="720" height="400" controls="controls">
  3. iOS 3.x is fussy about the order in which the videos appear—as it (and other handheld devices) can only play back MP4 format videos, this needs to appear as the first video source:

    <video width="720" height="400" controls="controls">
    <source src="trailer_480p.mp4" type="video/mp4" /><source src="trailer_480p.webm" type="video/webm" />
      <source src="trailer_480p.ogv" type="video/ogg" />
    </video>
  4. Android < 2.3 doesn't like the type attribute, so we need to remove it from the MP4 source tag (the only container Android currently supports). Here, the type attribute has been removed and the relevant codecs for the other two video source tags have also been added:

    <video width="320" height="240" controls> 
    <source src="vid.mp4"> 
    <source src="trailer_480p.webm" type='video/webm;
      codecs="vp8, vorbis"'> 
    <source src="trailer_480p.ogv" type='video/ogg;
      codecs="theora, vorbis"'> </video>

How it works...

The changes made in this task are designed to get around some browser or OS quirks that still exist as well as the (still) limited support for video formats on the mobile platform. This task highlights one of the reasons HTML5 video can still be confusing for some—this is still largely due to HTML5 being an emerging technology for which support is still evolving.

The key to this task is that while the changes required are simple to make, you should expect to have to make changes in the future, until HTML5 for the mobile platform has matured. For example, as you will have seen from this task, support only exists for the MP4 format for most mobile platforms—this is likely to remain until mobile devices appear with hardware decoders for the WebM format built in. When this happens, it would mean that there would be no need to have to include the MP4 format as the first source; it is likely that the order would then not matter, although that is up to the mobile device manufacturers!

Note

If you are interested in the state of developments for the WebM project and particularly in respect of hardware support, then it is worth reading the WebM blog at http://blog.webmproject.org.