是否可以使用JS检查哪个浏览器支持哪种视频/音频“编解码器”? [英] Is it possible to check which video/audio `codec` supported by which browser using JS?
问题描述
更准确地说,我想检查一下我可以在浏览器中使用哪些编解码器来处理HTML5中的视频/音频元素。 (例如Safari支持H.264,但我也想知道可以使用哪些编解码器)
More precisely, I would like to check which codecs I can use in a browser for video/audio elements in HTML5. (e.g. Safari supports H.264, but I also would like to know which codecs I can use)
由于规范会随着时间的推移而变化,因此我想以某种方式自动进行
Since specification changes over-time I would like to do it somehow automaticaly, not based on browser-codec tables.
我认为,可以将其作为JS函数编写(如果尚未编写)。我已经尝试过使用Google,但是还没有找到答案。
I think, it can be written as a function in JS(if it is not written, yet). I have tried to Google it, but wasn't even close to find an answer.
推荐答案
参考链接: https://www.nomensa.com/blog/2011/detecting -browser-compatibility-html5-视频和音频
您可以执行以下操作:
Reference link : https://www.nomensa.com/blog/2011/detecting-browser-compatibility-html5-video-and-audio You can do something like this :
// Define the get_mime function
var get_mime = function(filetype){
var mimetype = '';
var media_container = 'video';
switch(filetype){
case 'mp4':
mimetype = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';
break;
case 'ogg':
mimetype = 'video/ogg; codecs="theora, vorbis"';
break;
case 'webm':
mimetype = 'video/webm; codecs="vp8, vorbis"';
break;
case 'mp3':
mimetype = 'audio/mpeg';
media_container = 'audio';
break;
}
return {'mimetype':mimetype,'container':media_container};
};
// Check to see if the browser can render the file type
// using HTML5
var supports_media = function(mimetype, container) {
var elem = document.createElement(container);
if(typeof elem.canPlayType == ‘function’){
var playable = elem.canPlayType(mimetype);
if((playable.toLowerCase() == 'maybe')||(playable.toLowerCase() == 'probably')){
return true;
}
}
return false;
};
// When the DOM has loaded check the file extension of each media link
// and serve up appropriate media player
$(document).ready(function(){
$(‘a.youtube-links’).each(function(){
var path = $(this).attr(‘href’);
var extension = path.substring(path.lastIndexOf('.') + 1);
var extension_info = get_mime(extension);
if(supports_media(extension_info.mimetype, extension_info.container)){
// Serve up an HTML5 Media Player and controls
serve_html5();
}else{
// Serve up a flash based video for browsers that
//will not play the file using HTML5
serve_flash();
}
});
});
这篇关于是否可以使用JS检查哪个浏览器支持哪种视频/音频“编解码器”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!