From 11dcfe638b994fc30db3589fc1aaf273ab241362 Mon Sep 17 00:00:00 2001 From: vkay94 Date: Thu, 14 Jan 2021 20:01:06 +0100 Subject: [PATCH] Add durationPerFrame to Frameset and getFrameBoundsAt method --- .../extractors/YoutubeStreamExtractor.java | 3 +- .../newpipe/extractor/stream/Frameset.java | 48 ++++++++++++++++++- .../services/DefaultStreamExtractorTest.java | 2 + 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java index 5c225a32f..291c72d80 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java @@ -992,7 +992,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { for (int i = 1; i < spec.length; ++i) { final String[] parts = spec[i].split("#"); - if (parts.length != 8) { + if (parts.length != 8 || Integer.parseInt(parts[5]) == 0) { continue; } final int frameWidth = Integer.parseInt(parts[0]); @@ -1016,6 +1016,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { frameWidth, frameHeight, totalCount, + Integer.parseInt(parts[5]), framesPerPageX, framesPerPageY )); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Frameset.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Frameset.java index cc92cd7c8..d9bbc70ad 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Frameset.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/Frameset.java @@ -8,12 +8,14 @@ public final class Frameset { private int frameWidth; private int frameHeight; private int totalCount; + private int durationPerFrame; private int framesPerPageX; private int framesPerPageY; - public Frameset(List urls, int frameWidth, int frameHeight, int totalCount, int framesPerPageX, int framesPerPageY) { + public Frameset(List urls, int frameWidth, int frameHeight, int totalCount, int durationPerFrame, int framesPerPageX, int framesPerPageY) { this.urls = urls; this.totalCount = totalCount; + this.durationPerFrame = durationPerFrame; this.frameWidth = frameWidth; this.frameHeight = frameHeight; this.framesPerPageX = framesPerPageX; @@ -61,4 +63,48 @@ public final class Frameset { public int getFrameHeight() { return frameHeight; } + + /** + * @return duration per frame in milliseconds + */ + public int getDurationPerFrame() { + return durationPerFrame; + } + + /** + * Returns the information for the frame at stream position. + * + * @param position Position in milliseconds + * @return An int-array containing the bounds and URL where the indexes are specified as + * followed: + * + *
    + *
  • 0: Index of the URL
  • + *
  • 1: Left bound
  • + *
  • 2: Top bound
  • + *
  • 3: Right bound
  • + *
  • 4: Bottom bound
  • + *
+ */ + public int[] getFrameBoundsAt(long position) { + if (position < 0 || position > ((totalCount + 1) * durationPerFrame)) { + // Return the first frame as fallback + return new int[] { 0, 0, 0, frameWidth, frameHeight }; + } + + final int framesPerStoryboard = framesPerPageX * framesPerPageY; + final int absoluteFrameNumber = Math.min((int) (position / durationPerFrame), totalCount); + + final int relativeFrameNumber = absoluteFrameNumber % framesPerStoryboard; + + final int rowIndex = Math.floorDiv(relativeFrameNumber, framesPerPageX); + final int columnIndex = relativeFrameNumber % framesPerPageY; + + return new int[] { + /* storyboardIndex */ Math.floorDiv(absoluteFrameNumber, framesPerStoryboard), + /* left */ columnIndex * frameWidth, + /* top */ rowIndex * frameHeight, + /* right */ columnIndex * frameWidth + frameWidth, + /* bottom */ rowIndex * frameHeight + frameHeight }; + } } \ No newline at end of file diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultStreamExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultStreamExtractorTest.java index b4c2be5e3..40dc72a5d 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultStreamExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultStreamExtractorTest.java @@ -338,6 +338,8 @@ public abstract class DefaultStreamExtractorTest extends DefaultExtractorTest 0); + assertEquals(f.getFrameBoundsAt(0)[3], f.getFrameWidth()); } } else { assertTrue(frames.isEmpty());