From 47b660671a0e6e8d7fe06576dbe3bff782a55c05 Mon Sep 17 00:00:00 2001 From: Lauren Liberda Date: Fri, 16 Apr 2021 00:25:43 +0200 Subject: [PATCH] codec support check --- webroot/js/player.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/webroot/js/player.js b/webroot/js/player.js index 892f25f..a7452f5 100644 --- a/webroot/js/player.js +++ b/webroot/js/player.js @@ -110,8 +110,33 @@ window.addEventListener('DOMContentLoaded', (event) => { } } + function getMimeType(fmt) { + const mainType = fmt.vcodec !== 'none' ? 'video' : 'audio'; + let secondary = { + m4a: 'mp4', + }[fmt.ext] || fmt.ext; + const codecs = [fmt.vcodec, fmt.acodec].filter(c => c && c !== 'none').join(', '); + return `${mainType}/${secondary}; codecs="${codecs}"`; + } + + function isFormatSupported(fmt) { + if (!['http', 'https'].includes(fmt.protocol)) + return false; + + // must be very old: https://developer.mozilla.org/en-US/docs/Web/API/MediaSource/isTypeSupported#browser_compatibility + if (!('MediaSource' in window)) { + if (['avc1', 'none'].includes(fmt.vcodec.substring(0, 4)) + && ['mp4a', 'none'].includes(fmt.acodec.substring(0, 4))) { + return true; + } + return false; + } + + return MediaSource.isTypeSupported(getMimeType(fmt)); + } + const formats = JSON.parse(document.getElementById('yt_formats').innerText) - .filter((fmt) => ['http', 'https'].includes(fmt.protocol)) + .filter((fmt) => isFormatSupported(fmt) === true) .sort((a, b) => { if (a.vcodec === 'none' && b.vcodec !== 'none') { return -1;