-
- V I D E O . J S - 0.0
- a library to work with the HTML5 <video> tag
-
- //////////////////////////////////////////////////////////////////////////////
-
- License MIT (see included LICENSE)
- Original Authors Center for Development of Open Technology @ Seneca College
-
- Web Site http://wiki.mozilla.org/Video_API
- Github Repo. http://github.com/annasob/video-js
- Bug Tracking https://processing-js.lighthouseapp.com/projects/52212-videojs/overview
-
- IMPORTANT! - NOTE FOR DEVELOPERS
- Please read the guidelines before pushing your code to the repository. The
- function(s) you are working on may already be finished and queued for push.
-
- GUIDELINES
- https://processing-js.lighthouseapp.com/projects/52212/project-workflow
-
- IRC CHANNEL
- Join the development team at irc://irc.mozilla.org/processing.js for more info
-
- //////////////////////////////////////////////////////////////////////////////
-
- PLATFORM AND BROWSER COMPATIBILITY
-
- Video.js is explicitly developed for and actively tested on browsers that
- support the HTML5 <Video> element.
-
- Video.js aims for 100 percent compatibility across all supported browsers;
- however, differences between individual video implementations may give
- slightly different results.
-
- //////////////////////////////////////////////////////////////////////////////
+POPCORN. JS - 0.1
+a library to work with the HTML5 <video> tag
+//////////////////////////////////////////////////////////////////////////////
+License MIT (see included LICENSE)
+Original Authors Center for Development of Open Technology @ Seneca College
+Web Site https://wiki.mozilla.org/PopcornOpenVideoAPI
+Github Repo. http://github.com/annasob/popcorn-js/
+Bug Tracking https://processing-js.lighthouseapp.com/projects/52212-popcornjs/overview
+
+IMPORTANT! - NOTE FOR DEVELOPERS
+Please read the guidelines before pushing your code to the repository. The function(s) you are working on may already be finished and queued for push.
+
+GUIDELINES
+https://processing-js.lighthouseapp.com/projects/52212/project-workflow
+
+IRC CHANNEL
+Join the development team at irc://irc.mozilla.org/popcorn for more info
+
+//////////////////////////////////////////////////////////////////////////////
+PLATFORM AND BROWSER COMPATIBILITY
+Popcorn.js is explicitly developed for and actively tested on browsers that support the HTML5 <Video> element.
+Popcorn.js aims for 100 percent compatibility across all supported browsers; however, differences between individual video implementations may give slightly different results.
+//////////////////////////////////////////////////////////////////////////////
<html>
- <head>
- <title>html5 video demo</title>
- <script src="http://code.jquery.com/jquery.js"></script>
- <script src="http://google.com/jsapi"></script>
- <script>google.load("language","1");</script>
- <script src="video.js"></script>
- <style type="text/css" media="screen">
- body { background-color:#eee; }
- #container {
- width:850px;
- margin:auto 50px;
- border:1px solid #ccc;
- background:white;
- padding:20px;
- position:relative;
- }
- #sub {
- position:absolute;
- top:435px;
- left:40px;
- color:white;
- font-weight:bold;
- font-family:sans-serif;
- text-shadow:black 1px 1px 3px;
- width:820px;
- font-size:25px;
- }
- select { font-size:30px; float:left; }
- #credit { font-size:25px; color:#999; float:right; padding-top:3px; }
- #credit a { color:#555; }
- .clear { clear:both; }
- </style>
- </head>
- <body>
- <div id="container">
-
- <video src="video/years.ogv" ontimeupdate="update(this)" controls>
- <subtitles>
- <subtitle from="0:02" to="0:05">Intro. Lyrics start at ~0:16</subtitle>
- <subtitle from="0:16" to="0:22.5">I'm fifteen for a moment, caught in between ten and twenty</subtitle>
- <subtitle from="0:23" to="0:30">and I'm just dreaming, counting the ways to where you are.</subtitle>
- <subtitle from="0:32" to="0:39">I'm twenty-two for a moment, and she feels better than ever and we're</subtitle>
- <subtitle from="0:39" to="0:46.5">on fire, making our way back from Mars.</subtitle>
- <subtitle from="0:48" to="0:56">Fifteen there's still time for you, time to buy and time to lose.</subtitle>
- <subtitle from="0:56.5" to="1:08">Fifteen, there's never a wish better than this, when you've only got a hundred years to live</subtitle>
- </subtitles>
- </video>
- <div id="sub" border="2"></div><br/>
-
- <div id="bottom">
- <select id="language">
- <option val="en">English</option>
- <option val="es">Spanish</option>
- <option val="fr">French</option>
- <option val="de">German</option>
- <option val="ja">Japanese</option>
-
- </select>
- <div id="credit">with help from <a href="http://google.com/translate">Google Translate</a> and <a href="http://jquery.com">jQuery</a></div>
- </div>
- <div class="clear"></div>
- </div>
- </body>
-
+ <head>
+ <title>html5 video demo</title>
+ <script src="popcorn.js"></script>
+ <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
+ <style type="text/css" media="screen">
+ body {
+ background-color:#eee;
+ }
+ #container {
+ width:850px;
+ margin:auto 50px;
+ border:1px solid #ccc;
+ background:white;
+ padding:20px;
+ position:relative;
+ }
+ #sub {
+ position:absolute;
+ top:435px;
+ left:40px;
+ color:white;
+ font-weight:bold;
+ font-family:sans-serif;
+ text-shadow:black 1px 1px 3px;
+ width:820px;
+ font-size:25px;
+ }
+ #map {
+ border: solid 1px #000;
+ width: 200px;
+ height: 200px;
+ }
+ </style>
+ </head>
+ <body>
+ <div id="container">
+ <video src="video/webMadeMovies.ogg" data-timeline-sources="xml/webMadeMovies.xml" controls></video>
+ <div id="sub"></div><br />
+ <div id="tagthisvideo">In this video:<span id="inthisvideo"></span></div><br />
+ <div id="map"></div>
+ </div>
+ </body>
</html>
--- /dev/null
+(function() {
+
+ // The video manager manages a single video element, and all it's commands.
+ var VideoManager = this.VideoManager = function(videoElement) {
+ this.commandObjects = {};
+ this.videoElement = videoElement;
+ videoElement.videoManager = this;
+ VideoManager.addInstance(this);
+ videoElement.setAttribute("ontimeupdate", "VideoManager.update(this, this.videoManager);");
+ };
+ VideoManager.prototype.addCommand = function(command) {
+ this.commandObjects[command.id] = command;
+ };
+ VideoManager.prototype.removeCommand = function(command) {
+ delete this.commandObjects[command.id];
+ };
+ // Update is called on the video every time it's time changes.
+ VideoManager.update = function(vid, manager) {
+ var t = vid.currentTime;
+ // Loops through all commands in the manager, preloading data, and calling in() or out().
+ var commandObject = {};
+ for (var i in manager.commandObjects) {
+ commandObject = manager.commandObjects[i];
+ if (commandObject.running && (commandObject.params.in > t || commandObject.params.out < t)) {
+ commandObject.running = false;
+ commandObject.out();
+ }
+ }
+ for (var i in manager.commandObjects) {
+ commandObject = manager.commandObjects[i];
+ if (!commandObject.loaded && (commandObject.params.in-5) < t && commandObject.params.out > t) {
+ commandObject.loaded = true;
+ commandObject.preload();
+ }
+ if (!commandObject.running && commandObject.params.in < t && commandObject.params.out > t) {
+ commandObject.running = true;
+ commandObject.in();
+ }
+ }
+ };
+
+ // Store VideoManager instances
+ VideoManager.instances = [];
+ VideoManager.instanceIds = {};
+ VideoManager.addInstance = function(manager) {
+ if (typeof manager.videoElement.id === 'undefined' || !manager.videoElement.id.length) {
+ manager.videoElement.id = "__video" + VideoManager.instances.length;
+ }
+ VideoManager.instanceIds[manager.videoElement.id] = VideoManager.instances.length;
+ VideoManager.instances.push(manager);
+ };
+ VideoManager.getInstanceById = function(name) {
+ return VideoManager.instances[VideoManager.instanceIds[name]];
+ };
+ VideoManager.getInstance = function(index) {
+ return VideoManager.instances[index];
+ };
+
+ // Simple function to convert 0:05 to 0.5 in seconds
+ var to_secs = function(time) {
+ var t = time.split(":");
+ return parseInt(t[0] * 60, 10) + parseInt(t[1], 10);
+ };
+
+ ////////////////////////////////////////////////////////////////////////////
+ // Command objects
+ ///////////////////////////////////////////////////////////////////////////
+
+ // Base class for all commands, SubtitleCommand, MapCommand, etc.
+ var VideoCommand = function(name, params, text) {
+ this.params = {};
+ this.text = text;
+ this.running = false;
+ this.loaded = false;
+ this.in = function() {};
+ this.out = function() {};
+ this.preload = function() {};
+ this.id = name + VideoCommand.count++;
+ for (var i = 0, pl = params.length; i < pl; i++) {
+ for (var j = 0, nl = params[i].length; j < nl; j++) {
+ var key = params[i].item(j).nodeName,
+ data = params[i].item(j).nodeValue;
+ if (key === "in" || key === "out") {
+ this.params[key] = to_secs(data);
+ } else {
+ this.params[key] = data;
+ }
+ }
+ }
+ };
+ VideoCommand.count = 0;
+
+ // Child commands. Uses in() and out() to do time based operations
+ var SubtitleCommand = function(name, params, text) {
+ VideoCommand.call(this, name, params, text);
+ this.in = function() {
+ document.getElementById("sub").innerHTML = this.text;
+ };
+ this.out = function() {
+ document.getElementById("sub").innerHTML = "";
+ };
+ };
+
+ var TagCommand = function(name, params, text) {
+ VideoCommand.call(this, name, params, text);
+ this.in = function() {
+ TagCommand.people.contains[this.text] = this.text;
+ document.getElementById("inthisvideo").innerHTML = TagCommand.people.toString();
+ };
+ this.out = function() {
+ delete TagCommand.people.contains[this.text];
+ document.getElementById("inthisvideo").innerHTML = TagCommand.people.toString();
+ };
+ };
+ TagCommand.people = {
+ contains: {},
+ toString: function() {
+ var r = [];
+ for (var i in this.contains) {
+ r.push(" " + this.contains[i]);
+ }
+ return r.toString();
+ }
+ };
+
+ var MapCommand = function(name, params, text) {
+ VideoCommand.call(this, name, params, text);
+ this.params.zoom = parseInt(this.params.zoom);
+ // load the map
+ // http://code.google.com/apis/maps/documentation/javascript/reference.html#MapOptions <-- Map API
+ this.location = new google.maps.LatLng(this.params.lat, this.params.long);
+ if (!MapCommand.map) {
+ MapCommand.map = new google.maps.Map(document.getElementById(this.params.target), {mapTypeId: google.maps.MapTypeId.HYBRID});
+ MapCommand.map.setCenter(new google.maps.LatLng(0, 0))
+ MapCommand.map.setZoom(0);
+ }
+ this.in = function() {
+ MapCommand.map.setCenter(this.location);
+ MapCommand.map.setZoom(this.params.zoom);
+ };
+ this.out = function() {
+ MapCommand.map.setCenter(new google.maps.LatLng(0, 0))
+ MapCommand.map.setZoom(0);
+ };
+ };
+
+ var TwitterCommand = function(name, params, text) {}; // http://twitter.com/celinecelines
+
+ // Wrapper for accessing commands by name
+ // commands[name].create() returns a new command of type name
+ // Not sure if this is the best way; maybe it's too fancy?
+ // I liked it more than a switch, though
+ var commands = {
+ subtitle: {
+ create: function(name, params, text) {
+ return new SubtitleCommand(name, params, text);
+ }
+ },
+ tagThisVideo: {
+ create: function(name, params, text) {
+ return new TagCommand(name, params, text);
+ }
+ },
+ location: {
+ create: function(name, params, text) {
+ return new MapCommand(name, params, text);
+ }
+ }
+ };
+
+ // Parses xml into command objects and adds them to the video manager
+ var parse = function(xmlDoc, videoManager) {
+ var parseNode = function(node, attributes) {
+ var allAttributes = attributes.slice(0);
+ allAttributes.push(node.attributes);
+ var childNodes = node.childNodes;
+ if (childNodes.length < 1 || (childNodes.length === 1 && childNodes[0].nodeType === 3)) {
+ videoManager.addCommand(commands[node.nodeName].create(node.nodeName, allAttributes, node.textContent));
+ } else {
+ for (var i = 0; i < childNodes.length; i++) {
+ childNodes[i].nodeType === 1 && parseNode(childNodes[i], allAttributes);
+ }
+ }
+ };
+
+ for (var j = 0, dl = xmlDoc.length; j < dl; j++) {
+ var x = xmlDoc[j].documentElement.childNodes;
+ for (var i = 0, xl = x.length; i < xl; i++) {
+ x[i].nodeType === 1 && parseNode(x[i], []);
+ }
+ }
+ };
+
+ // Loads an external xml file, and returns the xml object
+ var loadXMLDoc = function(name) {
+ var xhttp = new XMLHttpRequest();
+ if (xhttp) {
+ xhttp.open("GET", name, false);
+ xhttp.send();
+ return xhttp.responseXML;
+ } else {
+ return false;
+ }
+ };
+
+ // Automatic Initialization Method
+ var init = function() {
+ var video = document.getElementsByTagName('video');
+ for (var i = 0, l = video.length; i < l; i++) {
+ var videoSources = video[i].getAttribute('data-timeline-sources');
+ if (videoSources) {
+ var filenames = videoSources.split(' ');
+ var xml = [];
+ for (var j=0, fl=filenames.length; j<fl; j++) {
+ if (filenames[j]) {
+ xml.push(loadXMLDoc(filenames[j]));
+ }
+ }
+ var manager = new VideoManager(video[i]);
+ parse(xml, manager);
+ }
+ }
+ };
+ document.addEventListener('DOMContentLoaded', function() {
+ init();
+ }, false);
+
+}());
+
+++ /dev/null
-// fire when DOM is ready, jQuery shortcut for $(document).ready
-$(function() {
- $("video").each(function() {
- if (lang=window.location.search.substring(1)) {
- var ret=true;
- $("select option").each(function() {
- if ($(this).attr("val") == lang) { $(this).attr("selected",true); ret=false; }
- });
- if (!ret) {
- $("subtitle").each(function() {
- var sub = $(this);
- google.language.translate($(this).html(),"en",lang, function(r) {
- sub.html(r.translation);
- });
- });
- }
- }
-
- $("#language").change(function() { window.location="?" + $("#language option:selected").attr("val"); });
- });
-});
-
-var lt = 0;
-// fires when ontimeupdate() runs from <video> tag
-function update(vid) {
- var has_sub=false,
- t = vid.currentTime;
- if (lt>t) $("#sub span").fadeTo(1,.3);
- $("subtitle").each(function() {
- var from = to_secs($(this).attr("from")),
- to = to_secs($(this).attr("to"));
- $(this).removeClass("good");
- if (from<t&&to>t) {
- has_sub=true;
- $(this).addClass("good");
- }
- });
- if (!has_sub) {
- if (!$("#sub").hasClass("fading"))
- $("#sub").addClass("fading").stop().animate({ opacity:0, top:"-=30px" }, "medium", "swing");
- } else {
- if ($("#sub").hasClass("fading"))
- $("#sub").removeClass("fading").stop().animate({opacity:1,top:"410px"},1).html('');
- }
- $("subtitle.good:last").each(function() {
- if (!$(this).hasClass("on")) {
- $("subtitle.on").removeClass("on");
- $(this).addClass("on");
- $("#sub").html('').hide();
- var letters = $(this).text().split('');
- $.each(letters,function(i, val) {
- $("#sub").append('<span>' + val + '</span>');
- });
- $("#sub span").stop().fadeTo(1,.3, function() { $(this).show(); });
- }
- var from = to_secs($(this).attr("from")),
- to = to_secs($(this).attr("to")),
- perc = ((t-from)/(to-from)),
- num_letters = $(this).text().length*perc,
- i = 0;
- $("#sub span").each(function() {
- if (i++>num_letters) return;
- $(this).stop().fadeTo("fast",1);
- }).parent().show();
- });
- lt = t;
-}
-
-// convert time format mm:ss to seconds integer
-function to_secs(time) {
- var t = time.split(":");
- return parseInt(t[0]*60,10) + parseInt(t[1],10);
-}
-