From aa91fb68c7a40fb563004d2a73ed1fa4a7e9d288 Mon Sep 17 00:00:00 2001 From: nholtman Date: Mon, 8 Apr 2024 15:25:23 +0200 Subject: [PATCH] Turn the quality label text generator into a config option --- src/quality/quality.js | 50 ++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/quality/quality.js b/src/quality/quality.js index 97b5cd5..8ef30a8 100644 --- a/src/quality/quality.js +++ b/src/quality/quality.js @@ -20,6 +20,30 @@ Object.assign(mejs.MepDefaults, { * @type {String} */ qualityText: null, + /** + * @type {autoQualityLabelTextGenerator} cb + * @param {object} level - contains at least a width and height field with the resolution of that stream + */ + autoQualityLabelTextGenerator (level) { + // Return it as named qualities + const height = level.height + if (height >= 4320) { + return "8K UHD"; + } else if (height >= 2160) { + return "UHD"; + } else if (height >= 1440) { + return "QHD"; + } else if (height >= 1080) { + return "FHD"; + } else if (height >= 720) { + return "HD"; + } else { + return "SD"; + } + + // return it as 1080p, 720p etc. + // return level.height + 'p' + }, /** * @type {boolean} */ @@ -106,8 +130,7 @@ Object.assign(MediaElementPlayer.prototype, { const levels = media.hlsPlayer.levels; if (t.options.autoGenerate && levels.length > 1) { levels.forEach(function (level) { - const height = level.height; - const quality = t.getQualityFromHeight(height); + const quality = t.options.autoQualityLabelTextGenerator(level); t.addValueToKey(qualityMap, quality, ''); }); t.options.autoHLS = true; @@ -118,8 +141,7 @@ Object.assign(MediaElementPlayer.prototype, { const bitrates = media.dashPlayer.getBitrateInfoListFor("video"); if (t.options.autoGenerate && bitrates.length > 1) { bitrates.forEach(function (level) { - const height = level.height; - const quality = t.getQualityFromHeight(height); + const quality = t.options.autoQualityLabelTextGenerator(level); t.addValueToKey(qualityMap, quality, ''); }); t.options.autoDash = true; @@ -455,25 +477,5 @@ Object.assign(MediaElementPlayer.prototype, { } return newQuality; - }, - - /** - * Returns the quality represnetaion base on the height of the loaded video - * @param {Number} height the pixel height of the video - **/ - getQualityFromHeight (height) { - if (height >= 4320) { - return "8K UHD"; - } else if (height >= 2160) { - return "UHD"; - } else if (height >= 1440) { - return "QHD"; - } else if (height >= 1080) { - return "FHD"; - } else if (height >= 720) { - return "HD"; - } else { - return "SD"; - } } });