Working with HTML5 Video
Working with HTML5 Video
source
tags for cross browser support.<video controls preload="metadata">
<source src="../video/sintel-short.mp4" type="video/mp4">
<source src="../video/sintel-short.webm" type="video/webm">
<p>It appears that your browser doesn't support HTML5 video.
Here's a <a href="../video/sintel-short.mp4">direct link to
the video instead</a>.</p>
</video>
Code time: tutorial start
<track>
elements<track label="English" kind="subtitles" srclang="en"
src="vtt/sintel-subtitles-en.vtt" default>
<track label="Deutsch" kind="subtitles" srclang="de"
src="vtt/sintel-subtitles-de.vtt">
<track label="Español" kind="subtitles" srclang="es"
src="vtt/sintel-subtitles-es.vtt">
<track>
attributesdefault
: Sets the default text track to display (only one element per video can have this), which can be overridden by JavaScript or user preferences.kind
: Indicates what kind of text track each track is. Here we only have subtitles, but you can also have captions, descriptions and others.label
: A user-readable title for the text track, which is displayed by the browser when listing available text tracks.src
: The URL of the text track file.srclang
: A language code indicating to the browser what language the text tracks are written in.Code time: tutorial step 2
Implement a custom UI for <track>
elements using JavaScript!
WEBVTT
0
00:00:00.000 --> 00:00:12.000
[Test]
1
00:00:18.700 --> 00:00:21.500
This blade has a dark past.
hh:mm:ss:msmsms
, allowing for very precise times.We are using a select menu:
<form>
<select name="select">
</select>
</form>
video.textTracks
.textTrack.label
, textTrack.kind
, or textTrack.language
.textTrack.mode = showing
or hidden
.Code time: tutorial step 3
var video = document.querySelector('video');
var select = document.querySelector('select');
function hideTracks() {
for (var i = 0; i < video.textTracks.length; i++) {
video.textTracks[i].mode = 'hidden';
}
}
hideTracks();
var tracksOff = document.createElement('option');
tracksOff.setAttribute('value','off');
tracksOff.textContent = 'Tracks off';
select.appendChild(tracksOff);
for (var i = 0; i < video.textTracks.length; i++) {
var curTrack = video.textTracks[i];
var addTrackOpt = document.createElement('option');
addTrackOpt.setAttribute('value',curTrack.kind + '-' + curTrack.language);
addTrackOpt.textContent = curTrack.label + ' ' + curTrack.kind;
select.appendChild(addTrackOpt);
}
This runs the trackChange()
function whenever the a new select value is selected
select.addEventListener('change',function() {
trackChange(select.value);
});
trackChange()
functionfunction trackChange(value) {
if(value === 'off') {
hideTracks();
} else {
hideTracks();
var splitValue = value.split('-');
for (var i = 0; i < video.textTracks.length; i++) {
if(video.textTracks[i].kind === splitValue[0]) {
if(video.textTracks[i].language === splitValue[1]) {
video.textTracks[i].mode = 'showing';
}
}
}
}
}
trackChange()
workskind
and language
.kind
and language
.<track>
example<track label="English" kind="captions" srclang="en"
src="vtt/sintel-captions-en.vtt">
Code time: tutorial step 4
1
00:00:18.700 --> 00:00:21.500 line:20% align:end
<c.man><b>Man</b>: This blade has a dark past. </c>
vertical
: Specifies vertical text, not horizontal.line
: Specifies the line the text appears on.position
: Specifies the position the text will appear at along the line.size
: Specifies the size (width) of the text.align
: Specifies the text alignment.Caption text cues have some special tags marking them up, stylable via CSS Extensions.
<c.man><b>Man</b>: This blade has a dark past. </c>
The ::cue
pseudo-element is the key to targetting text track cues for styling.
video::cue { whitespace: pre; }
video::cue(.man) { color:yellow }
color
opacity
visibility
text-decoration
text-shadow
background
shorthand propertiesoutline
shorthand propertiesfont
shorthand properties, and line-height
whitespace
moon <00:17.500>hits your eye
<c.classname>text</c>
<i>text</i>
<b>text</b>
<u>text</u>
<ruby>WWW<rt>World Wide Web</rt>oui<rt>yes</rt></ruby>
<v bob>text</v>
if(curTrack.language === 'en' && curTrack.kind === 'subtitles') {
addTrackOpt.setAttribute('selected','selected');
trackChange(select.value);
}
Further resources