Fix SponsorBlock segments where video duration is not provided (#102)

* Fix SponsorBlock segments where video duration is not provided

* add another null check
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2023-07-04 15:21:19 -04:00
1 parent 936eefeeb9
commit a224b86fca
5 files changed
+28 -21

No files matched your search

+1
View File
@@ -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
@@ -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 {
@@ -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
@@ -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)
@@ -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