From c6030064f15c62a2e9e9fb5804dd9dcce44bcece Mon Sep 17 00:00:00 2001 From: Carbrex <95964955+Carbrex@users.noreply.github.com> Date: Wed, 7 Feb 2024 01:57:23 +0530 Subject: [PATCH] Added video quality feature (#43) * Fixed docker compose errors * Added quality selector * Remove log statement Co-authored-by: Matthew Esposito * Show kbps in quality Co-authored-by: Matthew Esposito * Make highest quality default * Add styling, default option to highest --------- Co-authored-by: Matthew Esposito --- docker-compose.dev.yml | 3 +-- static/playHLSVideo.js | 37 +++++++++++++++++++++++++++++++++++-- static/style.css | 16 ++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 6f5a367..c2698da 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -12,12 +12,11 @@ services: read_only: true security_opt: - no-new-privileges:true + - seccomp="seccomp-redlib.json" cap_drop: - ALL networks: - redlib - security_opt: - - seccomp="seccomp-redlib.json" healthcheck: test: ["CMD", "wget", "--spider", "-q", "--tries=1", "http://localhost:8080/settings"] interval: 5m diff --git a/static/playHLSVideo.js b/static/playHLSVideo.js index bb6ebeb..d0da746 100644 --- a/static/playHLSVideo.js +++ b/static/playHLSVideo.js @@ -30,12 +30,21 @@ function initializeHls() { newVideo.removeEventListener('play', initializeHls); - var hls = new Hls({ autoStartLoad: false }); hls.loadSource(playlist); hls.attachMedia(newVideo); hls.on(Hls.Events.MANIFEST_PARSED, function () { hls.loadLevel = hls.levels.length - 1; + var availableLevels = hls.levels.map(function(level) { + return { + height: level.height, + width: level.width, + bitrate: level.bitrate, + }; + }); + + addQualitySelector(newVideo, hls, availableLevels); + hls.startLoad(); newVideo.play(); }); @@ -61,6 +70,30 @@ }); } + function addQualitySelector(videoElement, hlsInstance, availableLevels) { + var qualitySelector = document.createElement('select'); + qualitySelector.classList.add('quality-selector'); + var last = availableLevels.length - 1; + availableLevels.forEach(function (level, index) { + var option = document.createElement('option'); + option.value = index.toString(); + var bitrate = (level.bitrate / 1_000).toFixed(0); + option.text = level.height + 'p (' + bitrate + ' kbps)'; + if (index === last) { + option.selected = "selected"; + } + qualitySelector.appendChild(option); + }); + qualitySelector.selectedIndex = availableLevels.length - 1; + qualitySelector.addEventListener('change', function () { + var selectedIndex = qualitySelector.selectedIndex; + hlsInstance.nextLevel = selectedIndex; + hlsInstance.startLoad(); + }); + + videoElement.parentNode.appendChild(qualitySelector); + } + newVideo.addEventListener('play', initializeHls); if (autoplay) { @@ -74,4 +107,4 @@ }); } })(); -// @license-end \ No newline at end of file +// @license-end diff --git a/static/style.css b/static/style.css index a9d8d3c..95ad789 100644 --- a/static/style.css +++ b/static/style.css @@ -1714,3 +1714,19 @@ td, th { justify-content: initial; } } + +.quality-selector { + border: 2px var(--outside) solid; + margin-top: 8px; + float: right; +} + +.quality-selector option { + background-color: var(--background); + color: var(--text); +} + +.quality-selector option:hover { + background-color: var(--accent); + color: var(--text); +} \ No newline at end of file