You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

237 lines
5.9 KiB

var padmaConfig = {
'background': 'rgba(0,0,0,0.85)',
'color': '#fff',
'links_url': 'pad.ma/',
'data_url': document.location.protocol + '//pad.ma/',
'data_re': /https?:\/\/pad.ma\//,
'default_layers': ['transcripts', 'descriptions'],
};
(function($) {
var BASE_URL = padmaConfig.data_url;
var padmaVideo = function(videoId) {
this.id = videoId;
// this.videoUrl = BASE_URL + videoId + "/video.js";
// this.layersUrl = BASE_URL + videoId + "/layers.js";
// this.infoUrl = BASE_URL + videoId + "/info";
// this.hasVideoData = false;
// this.hasLayersData = false;
this.hasAllData = false;
this.video = {};
this.layers = {};
var that = this;
padmaVideo.cache.push(that);
}
padmaVideo.prototype.getAllData = function(callback, data) {
var that = this;
if (!this.hasAllData) {
var keys = ['layers', 'title', 'duration']
var d = JSON.stringify({
'id': that.id,
'keys': keys
});
var url = BASE_URL + "api/";
$.post(url, {'action': 'get', 'data': d}, function(response) {
var v = response.data;
that.video.title = v.title;
that.video.duration = v.duration;
that.infoUrl = padmaConfig.data_url + that.id + "/info";
that.layers = flattenLayers(v.layers);
that.hasAllData = true;
that.hasLayersData = true;
that.videoSrc = that.getVideo(0, '240');
callback(that, data);
}, "json");
/*
$.getScript(that.videoUrl, function() {
that.video = video;
that.hasVideoData = true;
$.getScript(that.layersUrl, function() {
that.layers = layers;
that.hasLayersData = true;
that.hasAllData = true;
callback(that, data);
});
});
*/
} else {
callback(that, data);
}
}
//tc in npt
padmaVideo.prototype.getFrame = function(tc) {
tc = npt2ms(tc) / 1000
return BASE_URL + this.id + "/240p" + tc + ".jpg";
}
//tc in npt
padmaVideo.prototype.getVideo = function(tc, size) {
if (!this.hasAllData) {
return false;
}
if (typeof(size) == 'undefined') {
size = '240';
}
return "http://video.pad.ma/" + this.id + "/" + size + "p.webm";
// return BASE_URL + this.id + "/" + size + "p.webm";
// return this.video.urls[size] + "?t=npt:" + tc;
}
padmaVideo.prototype.getDownloadLink = function(tcIn, tcOut) {
var inSeconds = npt2ms(tcIn) / 1000;
var outSeconds = npt2ms(tcOut) / 1000;
return this.getVideo(0, "480") + "?t=" + inSeconds + "," + outSeconds;
// return BASE_URL + this.id + "/download/" + tcIn + "-" + tcOut + ".ogv";
}
padmaVideo.prototype.getLayerVideo = function(tcIn, tcOut) {
return this.video.urls['320'] + "?t=" + tcIn + "/" + tcOut;
}
padmaVideo.prototype.noop = function() {
// console.log("I'm done:)");
$.noop();
}
padmaVideo.prototype.getVideoData = function(callback, data) {
var that = this;
if (this.hasVideoData) {
callback(that, data);
} else {
$.getScript(that.videoUrl, function() {
that.video = video;
that.hasVideoData = true;
callback(that, data);
});
}
}
padmaVideo.prototype.getLayersData = function(callback, data) {
var that = this;
if (this.hasLayersData) {
callback(that, data);
} else {
$.getScript(that.layersUrl, function() {
that.layers = layers;
that.hasLayersData = true;
callback(that, data);
});
}
}
padmaVideo.prototype.getLayerById = function(id) {
var that = this;
if (!this.hasLayersData) {
return false;
}
for (var i=0; i < that.layers.length; i++) {
var thisLayer = that.layers[i];
if (thisLayer.id == id) {
return thisLayer;
}
}
return null;
}
//timecode in ms
padmaVideo.prototype.getLayersAtTimecode = function(tc) {
var that = this;
if (!this.hasLayersData) {
return false;
}
// console.log(tc);
var layers = [];
for (var i=0; i < that.layers.length; i++) {
var thisLayer = that.layers[i];
// thisLayer.time_in = thisLayer.in * 1000;
// thisLayer.time_out = thisLayer.out * 1000;
if (tc > thisLayer.time_in && tc < thisLayer.time_out) {
layers.push(thisLayer);
}
}
return layers;
}
padmaVideo.prototype.getLayerById = function(id) {
var that = this;
var id = $.trim(id);
for (var i=0; i < that.layers.length; i++) {
var thisLayer = that.layers[i];
if (thisLayer.id == id) {
return thisLayer;
}
}
return false;
}
//convert layers format from new structure to old
function flattenLayers(layers) {
var ret = [];
for (var track in layers) {
if (layers.hasOwnProperty(track)) {
for (var i=0; i<layers[track].length; i++) {
var thisLayer = layers[track][i];
thisLayer.track = track;
thisLayer.value_html = thisLayer.value;
thisLayer.time_in = parseInt(thisLayer['in'] * 1000);
thisLayer.time_out = parseInt(thisLayer['out'] * 1000);
thisLayer.creator = thisLayer.user;
ret.push(thisLayer);
}
}
}
return ret;
}
function filterLayersByTracks(layers, tracks) {
var matchedLayers = []
for (var i=0; i < layers.length; i++) {
var layer = layers[i];
// console.log(layer.track);
if ($.inArray(layer.track, tracks) != -1) {
matchedLayers.push(layer);
}
}
return matchedLayers;
}
function filterLayersByContributors(layers, users) {
var matchedLayers = [];
for (var i=0; i < layers.length; i++) {
var layer = layers[i];
if ($.inArray(layer.contributor, users)) {
matchedLayers.push(layer);
}
}
return matchedLayers;
}
padmaVideo.cache = [];
padmaVideo.getFromCache = function(videoId) {
var cache = padmaVideo.cache;
for (var i=0; i<cache.length; i++) {
var v = cache[i];
if (v.id === videoId) {
return v;
}
}
return false;
}
window.padmaVideo = padmaVideo;
window.filterLayersByTracks = filterLayersByTracks;
window.filterLayersByContributors = filterLayersByContributors;
})(jQuery);