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)

Extending the video code (should know)


The previous task took us through the basics of how to embed video into a web page using the HTML5 <video> tag. However, there is a lot more functionality available with this tag, such as providing controls, controlling the height or width, and adding a "poster" image. In this task, we will take a look at some of this functionality.

Getting ready

For this task, you will need a copy of the code from the previous demo—we are going to alter it to provide a more functional player.

How to do it ...

  1. Open up a copy of the code from the previous demo in your usual text editor:

    <!doctype html>
    <html>
    <head>
    <title>HTML5 Video Test</title>
    </head>
    <body>
    <video>
      <source src="trailer_480p.webm" />
    </video>
    </body>
    </html>
  2. The first change we're going to make is to specify a height and width for the video. So, make the following change to the code to set up a defined area for the video:

    <video width="720" height="400" ><source src="trailer_480p.webm" />
    </video>
  3. The next change concerns the lack of controls, which is easy to fix:

    <video width="720" height="400" controls="controls" ><source src="trailer_480p.webm" />
    </video>
  4. Another change we can make, which makes for a more rounded experience, is to add a poster image that will show whether the video is loading or the user has yet to click on play:

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

How it works...

The code will work in a similar fashion to that of the previous task, but this time we've added a request to show the standard browser controls for the <video> tag as well as asking it to load an image as a "placeholder" before the video is played.

There's more...

There are some more tags that can be used when embedding HTML5 video as shown in the following table:

Name of attribute

Value

Description of attribute

autoplay

autoplay

Specifies that the video will start playing as soon as it is ready

loop

loop

Specifies that the video will start over again every time it finishes playing

muted

muted

Specifies that the audio output of the video should be muted

preload

auto

metadata

none

Specifies if and how the author thinks the video should be loaded when the page loads

It is worth noting that these are available for use, although it is unlikely that you will want to use them by default; a good example is the autoplay option, which can place an unnecessary drain on bandwidth if not used carefully! (This is particularly true on iPhones, which will ignore autoplay by default.)

You will also notice that although we've only added one command to display the controls, the colors and design used will differ between browsers:

From top to bottom of the preceding screenshot, we have Internet Explorer, Safari, Google Chrome, and Firefox.