Merge branch 'codec-filter-fix' into 'master'

codec support check

See merge request domi/bashtube!4
18l-fix
Dominika Liberda 2021-04-20 17:01:21 +00:00
commit 783f9e0970
1 changed files with 26 additions and 1 deletions

View File

@ -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;