From eed78c960d01a6945403d82ec262087e9da0b8e3 Mon Sep 17 00:00:00 2001 From: saltycrys <73420320+saltycrys@users.noreply.github.com> Date: Sat, 19 Dec 2020 17:48:20 +0100 Subject: [PATCH] Improve DASH quality preference Besides `auto`, `best` and `worst` it is now possible to select a target height. If the target height is not available the closest lower height is selected. --- assets/js/player.js | 21 +++++++++++++++++++-- src/invidious/views/preferences.ecr | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/assets/js/player.js b/assets/js/player.js index 930680d6..fcba43d8 100644 --- a/assets/js/player.js +++ b/assets/js/player.js @@ -159,9 +159,26 @@ if (!video_data.params.listen && video_data.params.quality === 'dash') { player.ready(() => { player.on("loadedmetadata", () => { const qualityLevels = Array.from(player.qualityLevels()).sort((a, b) => a.height - b.height); - const targetQualityLevel = video_data.params.quality_dash == "best" ? qualityLevels.length - 1 : 0; + let targetQualityLevel; + switch (video_data.params.quality_dash) { + case "best": + targetQualityLevel = qualityLevels.length - 1; + break; + case "worst": + targetQualityLevel = 0; + break; + default: + const targetHeight = Number.parseInt(video_data.params.quality_dash, 10); + for (let i = 0; i < qualityLevels.length; i++) { + if (qualityLevels[i].height <= targetHeight) { + targetQualityLevel = i; + } else { + break; + } + } + } for (let i = 0; i < qualityLevels.length; i++) { - qualityLevels[i].enabled = (i == targetQualityLevel) + qualityLevels[i].enabled = (i == targetQualityLevel); } }); }); diff --git a/src/invidious/views/preferences.ecr b/src/invidious/views/preferences.ecr index 46aa714c..9eac10a7 100644 --- a/src/invidious/views/preferences.ecr +++ b/src/invidious/views/preferences.ecr @@ -61,7 +61,7 @@