diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ccd763f..a1c67aa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - (Removed) Workaround for Invidious token expiring due to misconfigured instance - Crash when loading the home screen [#83](https://github.com/iBicha/playlet/issues/83) - Performance of home screen in web app +- SponsorBlock segments where video duration is not provided (follow up to [#23](https://github.com/iBicha/playlet/pull/23)) ## [0.10.1] - 2023-06-28 diff --git a/playlet-lib/src/components/VideoPlayer/SponsorBlockTask.bs b/playlet-lib/src/components/VideoPlayer/SponsorBlockTask.bs index 9746468a..6f0e5ec1 100644 --- a/playlet-lib/src/components/VideoPlayer/SponsorBlockTask.bs +++ b/playlet-lib/src/components/VideoPlayer/SponsorBlockTask.bs @@ -3,26 +3,14 @@ import "pkg:/source/services/SponsorBlock.bs" @asynctask function SponsorBlockTask(input as object) as object videoId = input.videoId + lengthSeconds = input.metadata.lengthSeconds - sponsorBlockResponse = SponsorBlock.GetSkipSegmentsForVideo(videoId) + skipSegments = SponsorBlock.GetSkipSegmentsForVideo(videoId) barPath = invalid - skipSegments = [] - if sponsorBlockResponse <> invalid - for each segment in sponsorBlockResponse - if segment.videoDuration > 0 - skipSegments.push(segment) - end if - end for - end if - - if skipSegments.Count() = 0 - skipSegments = invalid - end if - if skipSegments <> invalid barPath = `tmp:/sponsorblock_bar_${videoId}.png` - SponsorBlock.GenerateProgressBarBackground(skipSegments, barPath) + SponsorBlock.GenerateProgressBarBackground(skipSegments, lengthSeconds, barPath) end if return { diff --git a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs index 7cc207b5..88fc2c19 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs @@ -153,6 +153,8 @@ function OnVideoDetailsTaskResults(output as object) as void m.top.control = "play" m.videoContainer@.watchedVideosAdd(videoId) + + StartSponsorBlockTask() end function function AddDashUrls(playletStreamUrls as object, metadata as object, settings as object) as void diff --git a/playlet-lib/src/components/VideoPlayer/VideoPlayerSponsorBlock.bs b/playlet-lib/src/components/VideoPlayer/VideoPlayerSponsorBlock.bs index c7e0c592..5c687f4d 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoPlayerSponsorBlock.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoPlayerSponsorBlock.bs @@ -18,7 +18,6 @@ function SetupSponsorBlock() as void m.top.trickPlayBar.observeField("visible", "OnTrickPlayBarVisible") m.chapterLabelTimer = m.top.findNode("chapterLabelTimer") m.chapterLabelTimer.observeField("fire", "OnChapterLabelTimer") - m.top.observeField("videoId", "StartSponsorBlockTask") end function function LoadSponsorBlockSettings() as void @@ -103,12 +102,19 @@ function UpdateSponsorBlockChapter(time as integer) as void m.top.chapter = "" end function -function StartSponsorBlockTask() +function StartSponsorBlockTask() as void + if not m.sponsorBlockEnabled = true + return + end if + if m.sponsorBlockTask <> invalid m.sponsorBlockTask.control = "stop" end if - m.sponsorBlockTask = StartAsyncTask(SponsorBlockTask, { videoId: m.top.videoId }, OnSponsorBlockTaskResults) + m.sponsorBlockTask = StartAsyncTask(SponsorBlockTask, { + videoId: m.top.videoId, + metadata: m.top.content.metadata + }, OnSponsorBlockTaskResults) end function function OnSponsorBlockTaskResults(output as object) diff --git a/playlet-lib/src/source/services/SponsorBlock.bs b/playlet-lib/src/source/services/SponsorBlock.bs index dd3222b3..c7cac0eb 100644 --- a/playlet-lib/src/source/services/SponsorBlock.bs +++ b/playlet-lib/src/source/services/SponsorBlock.bs @@ -81,14 +81,24 @@ namespace SponsorBlock return response.statuscode = 200 end function - function GenerateProgressBarBackground(segments as object, path as string) + function GenerateProgressBarBackground(segments as object, lengthSeconds as integer, path as string) bar = CreateObject("roBitmap", { width: 1000, height: 20, AlphaEnable: true }) bar.Clear(&hFFFFFF80) width = bar.GetWidth() height = bar.GetHeight() for each segment in segments - pixelStart = (segment.segment[0] / segment.videoDuration) * width - pixelEnd = (segment.segment[1] / segment.videoDuration) * width + videoDuration = lengthSeconds + if videoDuration = 0 or videoDuration = invalid + ' We should ignore segment.videoDuration https://github.com/iBicha/playlet/pull/23#issuecomment-1431520676 + ' But in case where we don't have lengthSeconds from Invidious, it's better than nothing + videoDuration = segment.videoDuration + end if + if videoDuration = 0 or videoDuration = invalid + continue for + end if + + pixelStart = (segment.segment[0] / videoDuration) * width + pixelEnd = (segment.segment[1] / videoDuration) * width color = SponsorBlock.SegmentColor(segment.category) ' highlight's duration is zero, so it is not visble on the bar. ' Add a couple of pixels to see it