From 545c1db57969764c36b5a4a9878fc841f6344756 Mon Sep 17 00:00:00 2001 From: Michael Zh <50182620+mikooomich@users.noreply.github.com> Date: Wed, 30 Apr 2025 11:57:27 -0400 Subject: [PATCH] [2/2] Treat multiple consecutive segments as one block --- .../org/schabi/newpipe/player/Player.java | 53 +++++++++++++++---- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/Player.java b/app/src/main/java/org/schabi/newpipe/player/Player.java index edb768524..98802ee51 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.java +++ b/app/src/main/java/org/schabi/newpipe/player/Player.java @@ -997,15 +997,16 @@ private void triggerCheckForSponsorBlockSegments(final int currentProgress, // show/hide manual skip buttons if (showManualButtons && secondaryMode != SponsorBlockSecondaryMode.HIGHLIGHT) { - if (currentProgress < sponsorBlockSegment.endTime - && currentProgress > sponsorBlockSegment.startTime) { + if (currentProgress < getChainSkipEndTime(sponsorBlockSegment) + && currentProgress > getChainStartTime(sponsorBlockSegment)) { UIs.call(PlayerUi::showAutoSkip); } else { UIs.call(PlayerUi::hideAutoSkip); } - if (currentProgress > sponsorBlockSegment.startTime - && currentProgress < sponsorBlockSegment.endTime + UNSKIP_WINDOW_MILLIS) { + if (currentProgress > getChainStartTime(sponsorBlockSegment) + && currentProgress < getChainSkipEndTime(sponsorBlockSegment) + + UNSKIP_WINDOW_MILLIS) { UIs.call(PlayerUi::showAutoUnskip); } else { UIs.call(PlayerUi::hideAutoUnskip); @@ -1041,8 +1042,8 @@ private void triggerCheckForSponsorBlockSegments(final int currentProgress, } int skipTarget = isRewind - ? (int) Math.ceil((sponsorBlockSegment.startTime)) - 1 - : (int) Math.ceil((sponsorBlockSegment.endTime)); + ? (int) Math.ceil(getChainStartTime(sponsorBlockSegment)) - 1 + : (int) Math.ceil(getChainSkipEndTime(sponsorBlockSegment)); if (skipTarget < 0) { skipTarget = 0; @@ -2489,11 +2490,11 @@ public Optional getSkippableSponsorBlockSegment(final int p continue; } - if (progress < sponsorBlockSegment.startTime) { + if (progress < getChainStartTime(sponsorBlockSegment)) { continue; } - if (progress > sponsorBlockSegment.endTime) { + if (progress > getChainSkipEndTime(sponsorBlockSegment)) { continue; } @@ -2502,13 +2503,13 @@ public Optional getSkippableSponsorBlockSegment(final int p // fallback on old SponsorBlockSegment (for un-skip) if (lastSegment != null - && progress > lastSegment.endTime + UNSKIP_WINDOW_MILLIS) { + && progress > getChainSkipEndTime(lastSegment) + UNSKIP_WINDOW_MILLIS) { // un-skip window is over hideUnskipButtons(); destroyUnskipVars(); } else if (lastSegment != null - && progress < lastSegment.endTime + UNSKIP_WINDOW_MILLIS - && progress >= lastSegment.startTime) { + && progress < getChainSkipEndTime(lastSegment) + UNSKIP_WINDOW_MILLIS + && progress >= getChainStartTime(lastSegment)) { // use old sponsorBlockSegment if exists AND currentProgress in bounds return lastSegment; } @@ -2518,6 +2519,36 @@ public Optional getSkippableSponsorBlockSegment(final int p }); } + public double getChainStartTime(final SponsorBlockSegment segment) { + if (segment.chain.isEmpty()) { + return segment.startTime; + } else { + for (int i = segment.chain.indexOf(segment) - 1; i >= 0; i--) { + // look for automatically skipped segments before this one + if (segment.chain.get(i).action != SponsorBlockAction.SKIP + || getSecondaryMode(segment) != SponsorBlockSecondaryMode.ENABLED) { + return segment.chain.get(i + 1).endTime; + } + } + return segment.chain.get(0).startTime; + } + } + + public double getChainSkipEndTime(final SponsorBlockSegment segment) { + if (segment.chain.isEmpty()) { + return segment.endTime; + } else { + for (int i = segment.chain.indexOf(segment) + 1; i < segment.chain.size(); i++) { + // look for automatically skipped segments after this one + if (segment.chain.get(i).action != SponsorBlockAction.SKIP + || getSecondaryMode(segment) != SponsorBlockSecondaryMode.ENABLED) { + return segment.chain.get(i - 1).endTime; + } + } + return segment.chain.get(segment.chain.size() - 1).endTime; + } + } + private void hideUnskipButtons() { if (DEBUG) { Log.d("SPONSOR_BLOCK", "Hiding manual skip buttons (UNSKIP)");