Your Digital Media Has Never Looked So Good

 
namlu
Topic Author
Posts: 6
Joined: Wed Aug 01, 2018 5:53 am

Recognising overlapping chapter times

Sun Aug 05, 2018 2:33 am

I need to handle cases when the chapter times overlap one another so that I can display the correct text for that chapter. For example, I have the following chapter array:

"chapters": Array[3][
    {
      "name": "Recap & Intro",
      "start": 0,
      "end": 145000,
      "skippable": true,
      "canSkipOnAutoplay": true
    },
    {
      "name": "Intro",
      "start": 53000,
      "end": 145000,
      "skippable": true
    },
    {
      "name": "Credits",
      "start": 3498000,
      "skippable": true,
      "canSkipOnAutoplay": true,
      "showUpNext": true
    }
  ],


So I've done this to check whether or not I'm in the current chapter:

function isInChapter(chapter as Object) as Boolean
    return m.top.videoPosition >= chapter.start and m.top.videoPosition < chapter.end
end function


 
Unfortunately, this doesn't account for the above case where the Recap & Intro overlap time with the Intro.

I've been trying something like this:
function chapterForCurrentVideoPosition(unused = invalid) as Object
    videoNode = m.top.videoContentNode
    if videoNode = invalid
        return invalid
    end if
    
    for i = 0 to (videoNode.chapters.count() - 1)
        chapter = videoNode.chapters[i]

        if isInChapter(chapter)
            if m.top.videoPosition >= videoNode.chapters[i + 1].start
                exit for
            end if
            return chapter
        end if
    end for
    return invalid
end function

But I end up with a strange loop case where the chapters become out of whack around position = 53, which is the start of Intro. 

The debug logs look something like this (Note: I removed the debug statements from the above code for simplification):
VideoSkipChapterButton: chapterForCurrentVideoPosition, [[position=52]]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton: ===videoNode.chapters[i + 1].start: 53
VideoPlayerScreen: updateSkipChapterShouldHideIfChapterChanged
VideoPlayerScreen: updateCurrentChapter
VideoSkipChapterButton: chapterForCurrentVideoPosition, [[position=52]]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton: ===videoNode.chapters[i + 1].start: 53
VideoSkipChapterButton: chapterForCurrentVideoPosition, [[position=52]]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton: ===videoNode.chapters[i + 1].start: 53
VideoPlayerScreen: onVideoPositionChanged: 53.09
VideoSkipChapterButton: chapterForCurrentVideoPosition, [[position=53]]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton: ===videoNode.chapters[i + 1].start: 53
VideoSkipChapterButton: ===i = 0
VideoSkipChapterButton:   Could not find a chapter.
VideoPlayerScreen: ---shouldShowSkipChapterButton: false
VideoPlayerScreen: ---m.settingsGroup.visible = false
VideoPlayerScreen: ---m.skipChapterShouldHide = true
VideoSkipChapterButton: chapterForCurrentVideoPosition, [[position=53]]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton: ===videoNode.chapters[i + 1].start: 53
VideoSkipChapterButton: ===i = 0
VideoSkipChapterButton:   Could not find a chapter.
VideoPlayerScreen: updateSkipChapterShouldHideIfChapterChanged
VideoPlayerScreen: updateCurrentChapter
VideoSkipChapterButton: chapterForCurrentVideoPosition, [[position=53]]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton: ===videoNode.chapters[i + 1].start: 53
VideoSkipChapterButton: ===i = 0
VideoSkipChapterButton:   Could not find a chapter.

Not sure what I'm doing wrong. Can anyone advise?
Cheers
 
namlu
Topic Author
Posts: 6
Joined: Wed Aug 01, 2018 5:53 am

Re: Recognising overlapping chapter times

Mon Aug 06, 2018 4:06 pm

Was able to fix by checking for the chapters in descending order rather than ascending. This removed the complexity of having to get the start time for the upcoming and comparing it to the current video time.

function chapterForCurrentVideoPosition(unused = invalid) as Object

    videoNode = m.top.videoContentNode
    if videoNode = invalid
        return invalid
    end if
    
    descChapters = videoNode.chapters
    descChapters.sortBy("start", "r")

    for each chapter in descChapters
        if isInChapter(chapter)
            return chapter
        end if
    end for
    
    return invalid
end function

Who is online

Users browsing this forum: No registered users and 11 guests