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)

Building the video player—the framework (must know)


In the previous section, we took a look at the basic HTML code required to play videos using the <video> tag, and how it was crucial to include multiple sources to allow play back through a number of different platforms. In this task, we are going to take a copy of that code and extend it, in preparation for developing our own player.

Getting ready

All you need for this task is your favorite text editor, and copies of the videos we converted earlier in this book—the rest will be added during the task.

How to do it...

  1. Create a new folder for storing your demo—call it video demo. Open up the folder, and create the following new folders: js, css, media, and images.

  2. Crack open the text editor of your choice, and add in the following lines:

    <!doctype html>
    <html>
    <head>
    <title>HTML5 Video Plugin Test</title>
    </head>
    <body>
    <video controls="controls" poster="trailer_480p.jpg">
       <source src="media/trailer_480p.ogv" type="video/ogv" />
       <source src="media/trailer_480p.mp4" type="video/mp4" />
       <source src="media/trailer_480p.webm" type="video/webm" />
    </video>
    </body>
    </html>
  3. Save this as videoplayer.html in the root of the video demo folder—we now need to set up the basic framework ready for the jQuery plugin, so create a new blank document, add in the following lines, and save that document as videoplayer.js. This will be used later in this section:

    (function($) {
        $.fn.oPlayer = function(options) {
             return this.each(function() {
            });
        };
    })(jQuery);
  4. We also need to alter the HTML structure to add in the framework for customizing the controls—first, add in this (noting the addition of an ID for the video control):

    <body>
    <div class="video_player">
    <video controls="controls" id="mainvideo" 
             poster="media/trailer_480p.jpg" >
    then alter the code accordingly:
    </video>
    <div class="custom_controls"></div>
    </div>
    </body>
    

If all is well, the browser will play the video when viewed in a browser using the inbuilt context menu controls.

How it works...

We've taken a copy of the original HTML required to display videos using the HTML5 <video> tag and set it up with the names of the videos we will use, depending on which browser the page is viewed in. We've also created a blank template for the jQuery plugin that we will start to develop over this section. We've altered the code to allow for some changes that will be required for later tasks such as adding in the custom controls. You may also notice that there are no controls showing by default—this is because we've added in our own custom controls placeholder, but as there is nothing included within them, nothing will show!