42
INTERACTIVE HTML5 <VIDEO> November 20, 2013 Charles Hudson [email protected]

Making the HTML5 Video element interactive

Embed Size (px)

DESCRIPTION

From Streaming Media West Conference Huntington Beach, CA November 2013 C202: HOW TO: Making the HTML5 Video Element Interactive The HTML5 Video element has now become widely used by browsers and supported in a broad set of websites for streaming video content. With some JavaScript and CSS, we can leverage the HTML video element to create highly interactive experiences for the viewer in both traditional and mobile browsing environments. This hands-on session explores the integration of events with the video timeline, creating positioned hotspots with links and dynamic content, and capturing user input. In addition, the session examines supporting mobile platform browsers along with future opportunities with the HTML5 video tag. Get sample code, ideas, and best practices for making the HTML5 video element an engaging interactive experience for your viewers. Speaker: Chuck Hudson, Co-Author, HTML5 Developer's Cookbook

Citation preview

Page 1: Making the HTML5 Video element interactive

42 1 HTML5 Interactive Video • November 20, 2013

INTERACTIVE HTML5 <VIDEO> November 20, 2013

Charles Hudson [email protected]

Page 2: Making the HTML5 Video element interactive

42 2 HTML5 Interactive Video • November 20, 2013

CANVAS CANVAS AND VIDEO BINDING

CUES DIGGING DEEP INTO CUES

TRACK META META DATA TRACK USAGE

OVERLAYS PLACING CONTROLS OVER

TIMELINE CONTROLLING TIME

SUBTITLES LEVERAGING SUBTITLES

Page 3: Making the HTML5 Video element interactive

42 3 HTML5 Interactive Video • November 20, 2013

WHAT IS INTERACTION

•  Supplemental information on demand – hotspots, Ajax info.

•  Enhanced navigation possibilities – menu, bookmarks, thumbnailing.

•  Validating knowledge retention – inline quizzes.

•  V-commerce – item in context viewing.

•  Improved story telling opportunities.

•  More engaging user experiences.

•  Incorporation of social networking.

FOR WHAT PURPOSE?

Page 4: Making the HTML5 Video element interactive

42 4 HTML5 Interactive Video • November 20, 2013

CROSS ORIGIN ERRORS In the examples provided multiple files will be used including text track files. These files must be served from the same origin as the video media files to prevent browser cross origin errors.

Page 5: Making the HTML5 Video element interactive

42 5 HTML5 Interactive Video • November 20, 2013

<VIDEO> BASIS OF THE INTERACTIONS

Page 6: Making the HTML5 Video element interactive

42 6 HTML5 Interactive Video • November 20, 2013

HTML5 VIDEO ELEMENT

•  Added in HTML5 to mainstream media video playback with out plugins.

•  Built in controls available.

•  Multiple sources to provide various browser support.

•  Fallback messaging allowed for unsupported browsers.

REFRESH

<video controls>! <source src="/video/big_buck_bunny_480p_surround.avi" type='video/mp4’>! <source src="/video/big_buck_bunny_480p_stereo.ogg" type='video/ogg’>! <p>HTML5 Video is not supported by this browser.</p>!</video>!

Page 7: Making the HTML5 Video element interactive

42 7 HTML5 Interactive Video • November 20, 2013

VIDEO

TRACKS

CUES

DIGGING DEEP GOING TO THE EDGE

HTML5 Video structures are still young.

As we dig deeper this will be more and more evident.

Present inconsistencies will be noted.

Browsers of course have various degrees of support as we go deeper.

WATCH THAT LAST STEP…

Page 8: Making the HTML5 Video element interactive

42 8 HTML5 Interactive Video • November 20, 2013

OVERLAYING ELEMENTS ON TOP OF VIDEO

Page 9: Making the HTML5 Video element interactive

42 9 HTML5 Interactive Video • November 20, 2013

OVERLAYING HTML ELEMENTS

•  Leverage CSS position to place elements.

•  Canvas an option as well.

•  Provides for interactions –  While video loading.

–  During playback or timeline navigation.

–  When play ends.

•  Can overlay entire video element or partial.

BASIC ELEMENTS

<video>

<div>

Page 10: Making the HTML5 Video element interactive

42 10 HTML5 Interactive Video • November 20, 2013

EXAMPLE: HOTSPOTS Overlaying hotspots on a video.

Page 11: Making the HTML5 Video element interactive

42 11 HTML5 Interactive Video • November 20, 2013

TIMELINE SYNCHRONIZING MANUAL EVENTS

Page 12: Making the HTML5 Video element interactive

42 12 HTML5 Interactive Video • November 20, 2013

VIDEO API EVENTS & PROPERTIES

•  Key events

•  Properties Include –  currentTime – read/write of the playing pointer on the video media.

SMALL BUT KEY SUBSET

play Video is played.

pause Video playback is paused.

ended Video playback has ended.

timeupdate Video currentTime is updated.

Page 13: Making the HTML5 Video element interactive

42 13 HTML5 Interactive Video • November 20, 2013

Get currentTime Returns the current pointer time

TIMELINE METHOD / EVENTS A SAMPLE FLOW

00:00:00:000

pause() Pause event fired

autoplay Play event fired

Ended event fired

•  Methods and current time fired automatically or manually.

Set currentTime Moves the play pointer

play() Play event fired

Page 14: Making the HTML5 Video element interactive

42 14 HTML5 Interactive Video • November 20, 2013

TIMEUPDATE EVENT

•  timeupdate fired when currentTime updated.

•  By definition; –  Continually fired while media playing.

–  Current media position adjusted.

–  Frequency not specified or guaranteed.

•  In practice, events fires several times per second.

SYNCHRONIZING WITH THE TIMELINE

video.addEventListener(’timeupdate', function() {! console.log(“currentTime = “ + video.currentTime);!}, false);!

Page 15: Making the HTML5 Video element interactive

42 15 HTML5 Interactive Video • November 20, 2013

EXAMPLE: TIMELINE CONTROL A DVD style content menu controlling current time and using overlay actions.

Page 16: Making the HTML5 Video element interactive

42 16 HTML5 Interactive Video • November 20, 2013

<TRACK> TEXT TRACKS IN PARALLEL

Page 17: Making the HTML5 Video element interactive

42 17 HTML5 Interactive Video • November 20, 2013

TRACK ELEMENT OVERVIEW

•  Track elements are added under the video element.

•  Can have multiple tracks for a single video element.

•  Tracks accessible by element id attribute or textTracks array.

SUBELEMENT TO MEDIA

var allTracks = document.getElementById("sampleVideo").textTracks;!

<video controls>! <source id='mp4' src="/video/trailer.mp4" type='video/mp4’>! <track kind="subtitles" label="English" src="subtitles_en.vtt" srclang="en" default>! <track kind="subtitles" label="Italiano" src="subtitles_it.vtt" srclang="it”>! This browser does not support HTML5 video.!</video>!

Page 18: Making the HTML5 Video element interactive

42 18 HTML5 Interactive Video • November 20, 2013

TRACK ELEMENT

•  Track element attributes –  Kind The kind of track included.

–  Src The source file of the track.

–  Srclang Two letter designation for language.

–  Label User readable label for the text track.

–  Default Default text track designation for use.

•  Can be referenced with the id attribute

•  Multiple tracks with different kind attributes allowed.

•  http://www.w3.org/wiki/HTML/Elements/track

ELEMENT TAG

Page 19: Making the HTML5 Video element interactive

42 19 HTML5 Interactive Video • November 20, 2013

5 KINDS OF TEXT TRACKS TYING INFORMATION TO VIDEO

SUBTITLES transcription or

translation of the dialogue, suitable for when the soundtrack

is unavailable.

CAPTIONS transcription or

translation of the dialogue, sound effects, relevant

musical cues, and other relevant audio information, suitable

for when the soundtrack is unavailable.

DESCRIPTIONS Textual descriptions

of the video component of the media resource,

intended for audio synthesis when the visual component is

unavailable.

CHAPTERS Chapter titles,

intended to be used for navigating the media resource.

METADATA Tracks intended for

use from script.

Page 20: Making the HTML5 Video element interactive

42 20 HTML5 Interactive Video • November 20, 2013

TRACK FILES

•  Mark up layout for text track information.

•  Each subtitle cue separated by a blank line.

•  Closed captioning when on supersedes subtitles.

•  W3C Specification for WebVTT http://dev.w3.org/html5/webvtt/.

WEB VIDEO TEXT TRACKS (VTT)

WEBVTT FILE INCLUSION Since the browser is including the WebVTT for the track the WebVTT must be served from a web server the file cannot be read from a file:// directive.

Page 21: Making the HTML5 Video element interactive

42 21 HTML5 Interactive Video • November 20, 2013

TRACK FILE FORMAT

•  Header statement

•  Individual Cues –  An identifier

–  Start to end time

–  Text for cue

VTT FILE STRUCTURE

CUE TIME FORMAT Time format is critical to proper parsing of the VTT file. If a start or end time is incorrectly structured such as 00:00:9:000 the behavior is indeterminate. Eg. Chrome will skip this cue.

WEBVTT FILE!

Subtitle1!00:00:11.000 --> 00:00:14.000!Cosa ti porta nella terra dei guardiani?!

Subtitle2!00:00:18.000 --> 00:00:20.000!Sto cercando qualcuno.!

Subtitle3!00:00:36.000 --> 00:00:40.000!Una pericolosa missione per un cacciatore solitario.!

Page 22: Making the HTML5 Video element interactive

42 22 HTML5 Interactive Video • November 20, 2013

SUBTITLE TRACKS

•  One or more cues handled automatically by html5 player.

•  Cues contain an id, start time, end time, and text.

TIMELINE COORDINATION

00:01:000 00:02:000 00:03:000 00:04:000 00:05:000 00:06:000 00:07:000 00:08:000 00:09:000

cue1 startTime 00:01:250 endTime 00:03:250

Cue1 text displayed

cue2 startTime 00:05:000 endTime 00:08:000

Cue2 text displayed

Page 23: Making the HTML5 Video element interactive

42 23 HTML5 Interactive Video • November 20, 2013

EXAMPLE: SIMPLE SUBTITLES Localized subtitles for videos using subtitle tracks. Testing via default attribute.

Page 24: Making the HTML5 Video element interactive

42 24 HTML5 Interactive Video • November 20, 2013

VIDEO LOADEDMETADATA EVENT

•  Fired when track sources all loaded.

•  Track sources are loaded asynchronously.

•  Not included in the window load event.

•  Must set separate event listener for loadedmetadata!

WHEN ARE TRACKS AVAILABLE

video.addEventListener('loadedmetadata', function () { ! !

!// Perform functions on video here.!

}, false);!

Page 25: Making the HTML5 Video element interactive

42 25 HTML5 Interactive Video • November 20, 2013

TRACK MODE

•  The track visibility is set through the mode property.

SETTING TRACK VISIBILITY

Disabled Text track is disabled for the video element.

Hidden Text track is hidden from being shown.

Showing Text track is currently being used to show.

Page 26: Making the HTML5 Video element interactive

42 26 HTML5 Interactive Video • November 20, 2013

EXAMPLE: CHANGING TRACKS Allowing the user to select their language of subtitles. Leverages pulling all tracks, kind, and mode.

Page 27: Making the HTML5 Video element interactive

42 27 HTML5 Interactive Video • November 20, 2013

METADATA USING METADATA FOR TRIGGERS

Page 28: Making the HTML5 Video element interactive

42 28 HTML5 Interactive Video • November 20, 2013

METADATA TRACKS

•  Metadata track cues not shown by video player.

•  Many potential uses choreographing functionality.

LEVERAGING JAVASCRIPT

METADATA TRACK LOADING To have a metadata track loaded the default attribute must be used in the <track> element.

<video>! <source …>! …! <track kind="metadata" label=”Quiz Cues" src=“quiz-metadata-en.vtt" default>!</video>!

Page 29: Making the HTML5 Video element interactive

42 29 HTML5 Interactive Video • November 20, 2013

METADATA TRACKS

•  Text field can include complex data.

•  Read like normal text property and then parsed.

•  But first need to be able to grab each cue.

JSON INSIDE CUES

Bubble1!00:00:05.000 --> 00:00:09.000!{!"title":"Fingerless Gloves",!"text":"Madonna made it fashionable to wear fingerless gloves, also called Glovelettes, in the 1980s with the record cover photo for Like a Virgin.",!"xpos":"450px",!"ypos":"350px”!}!

Page 30: Making the HTML5 Video element interactive

42 30 HTML5 Interactive Video • November 20, 2013

CUES ACCESSING CUES FROM TRACKS

Page 31: Making the HTML5 Video element interactive

42 31 HTML5 Interactive Video • November 20, 2013

CUES

•  Cues accessed as array fromtextTrack object.

•  Properties of each cue can be accessed or modified.

CUE OBJECT STRUCTURE

for (var cueIdx = 0; cueIdx < textTrack.cues.length; cueIdx++) {! console.log(”Cue id:” + textTrack.cues[cueIdx].id);! …!}!

id The id of the specific cue.

startTime Start time as defined by cue.

endTime End time as defined by cue.

text Text description of cue.

Page 32: Making the HTML5 Video element interactive

42 32 HTML5 Interactive Video • November 20, 2013

TRAPPING CUE EVENTS

•  Cue events –  enter !(onenter) fired when a cue is entered.

–  exit !(onexit) fired when a cue is exited.

•  Text track cuechange (oncuechange). –  Fired whenever the list of active cues changes.

–  oncuechange appears to be more reliable than cuechange!

TWO METHODS TO CATCH CUE CHANGES

textTrack.oncuechange = function (){…};!

Page 33: Making the HTML5 Video element interactive

42 33 HTML5 Interactive Video • November 20, 2013

TRAPPING CUE EVENTS

•  activeCues array of one or more active cues.

•  If activeCues length is zero when cuechange fired then exiting cue.

TWO METHODS TO CATCH CUE CHANGES

console.log(textTrack.activeCues[0].id);!

CUE EVENT RELIABILITY Cue events are known to be problematic with inconsistent behaviors (events not firing, events ceasing to fire).

Page 34: Making the HTML5 Video element interactive

42 34 HTML5 Interactive Video • November 20, 2013

EXAMPLE: BUBBLES Using cue events and cuechange to effect the famous music video pop up bubbles. Fraught with trouble. Don’t look down and step away from the edge.

Page 35: Making the HTML5 Video element interactive

42 35 HTML5 Interactive Video • November 20, 2013

EXAMPLE: BUBBLES NEW Using what we know with timeline update events to create a bubble solution.

Page 36: Making the HTML5 Video element interactive

42 36 HTML5 Interactive Video • November 20, 2013

TRACKS ON THE FLY

•  In JS can also add a new text track.

•  addTextTrack takes the kind, label and option language code.

•  New cues added with addCue(cue).

•  Cues can also be removed with removeCue(cue)!

CREATING TRACKS AND CUES

newTextTrack = myVideo.addTextTrack('chapters','bookmarks');!newTextTrack.default = true; !!

var newCue = new TextTrackCue(startTime, endTime, ’label');!newCue.id = 'bookmark1’;!newCue.pauseOnExit = false;!

newTextTrack.addCue(newCue);!

Page 37: Making the HTML5 Video element interactive

42 37 HTML5 Interactive Video • November 20, 2013

EXAMPLE: BOOKMARKS Create cues dynamically and list out the cue list. Navigate to bookmarks created by user.

Page 38: Making the HTML5 Video element interactive

42 38 HTML5 Interactive Video • November 20, 2013

COMPLEX INTERACTIONS

•  Take frame images from video and place on canvas.

•  Video element is hidden while canvas is shown.

•  Leverages –  canvas getContext(“2d”)

–  drawImage, getImageData, and putImageData

SHOWING VIDEO ON CANVAS ELEMENTS

<video> <canvas>

Image Copied

Dynamic Content Injection example

Page 39: Making the HTML5 Video element interactive

42 39 HTML5 Interactive Video • November 20, 2013

COMPLEX OVERLAYING

•  Timing challenge with direct processing.

•  Frame buffer needed for holding frames from timer.

•  http://www.craftymind.com/factory/html5video/CanvasVideo.html

•  http://www.kaizou.org/2012/09/frame-by-frame-video-effects-using-html5-and/

ADDING SPECIAL EFFECTS

<video> Hidden

Copy taken On timer

<canvas> Shown

Buffer <canvas>

Effect Added

Page 40: Making the HTML5 Video element interactive

42 40 HTML5 Interactive Video • November 20, 2013

RESOURCES

•  Documentation –  Whatwg.org video element -

http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html

•  3rd Party Libraries to aid development (not used here) –  cuepoint.js cuepoint.org Jquery plugin for subtitles and creating cue points.

–  popcorn.js popcornjs.org JS Media framework for easier time-based interaction.

–  video.js videojs.com Open source html5 video player and library.

VARIOUS HTML5 VIDEO RESOURCES

Page 41: Making the HTML5 Video element interactive

42 41 HTML5 Interactive Video • November 20, 2013

Q&A [email protected]

linkedin.com/in/chuckhudson

Code samples (will be posted) https://github.com/cahudson/html5video

Page 42: Making the HTML5 Video element interactive

42 42 HTML5 Interactive Video • November 20, 2013

VIDEO CREDITS

•  Thanks to the video providers –  Blender Foundation Open Source Projects http://www.blender.org/features/projects/