cleanup getVideoFormat and getVideoInfo

This commit is contained in:
rolux 2013-07-10 01:32:08 +02:00
parent b0f9558845
commit 564603a20e

View file

@ -2,46 +2,44 @@
/*@ /*@
Ox.getVideoFormat <f> Get supported video formats Ox.getVideoFormat <f> Get supported video formats
(formats) -> <a> of supported formats (formats) -> <a> List of supported formats
formats <a> list of available formats formats <a> List of potential formats
@*/ @*/
Ox.getVideoFormat = function(formats) { Ox.getVideoFormat = function(formats) {
var aliases = { var aliases = {
'mp4': 'h264', mp4: 'h264',
'm4v': 'h264', m4v: 'h264',
'ogv': 'ogg' ogv: 'ogg'
}, },
format,
tests = { tests = {
'h264': 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"', h264: 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"',
'ogg': 'video/ogg; codecs="theora, vorbis"', ogg: 'video/ogg; codecs="theora, vorbis"',
'webm': 'video/webm; codecs="vp8, vorbis"' webm: 'video/webm; codecs="vp8, vorbis"'
}, },
userAgent = navigator.userAgent.toLowerCase(), userAgent = navigator.userAgent.toLowerCase(),
video = document.createElement('video'); video = document.createElement('video'),
Ox.forEach(formats, function(f) { videoFormat;
var alias = aliases[f] || f; Ox.forEach(formats, function(format) {
if (!!(video.canPlayType && video.canPlayType(tests[alias]).replace('no', ''))) { var alias = aliases[format] || format;
if (video.canPlayType && video.canPlayType(tests[alias]).replace('no', '')) {
// disable WebM on Safari/Perian, seeking does not work // disable WebM on Safari/Perian, seeking does not work
if (!( if (!(
alias == 'webm' && /safari/.test(userAgent) alias == 'webm' && /safari/.test(userAgent)
&& !/chrome/.test(userAgent) && !/linux/.test(userAgent) && !/chrome/.test(userAgent) && !/linux/.test(userAgent)
)) { )) {
format = f; videoFormat = format;
return false; // break return false; // break
} }
} }
}); });
return format; return videoFormat;
}; };
/*@ /*@
Ox.getVideoInfo <f> Ox.getVideoInfo <f>
url <s> video url url <s> video url
callback <f> gets called with object containing duration, width, height callback <f> gets called with object containing duration, width, height
@*/ @*/
Ox.getVideoInfo = Ox.queue(function(url, callback) { Ox.getVideoInfo = Ox.queue(function(url, callback) {
var video = document.createElement('video'); var video = document.createElement('video');
video.addEventListener('loadedmetadata', function(event) { video.addEventListener('loadedmetadata', function(event) {
@ -51,11 +49,9 @@ Ox.getVideoInfo = Ox.queue(function(url, callback) {
height: this.videoHeight, height: this.videoHeight,
}; };
this.src = ''; this.src = '';
callback(info);
video = null; video = null;
callback(info);
}); });
video.preload = 'metadata'; video.preload = 'metadata';
video.src = url; video.src = url;
}, { }, 4);
maxThreads: 4
});