Skip to content

Commit

Permalink
Workaround for incorrect duration for "YT shorts" videos in channels
Browse files Browse the repository at this point in the history
As a workaround 0 is returned as duration for such videos.
See also TeamNewPipe/NewPipe#8034
  • Loading branch information
litetex committed Mar 26, 2022
1 parent 164e21b commit 7598b40
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,34 @@ public static int parseDurationString(@Nonnull final String input)
throw new ParsingException("Error duration string with unknown format: " + input);
}

return ((Integer.parseInt(Utils.removeNonDigitCharacters(days)) * 24
+ Integer.parseInt(Utils.removeNonDigitCharacters(hours))) * 60
+ Integer.parseInt(Utils.removeNonDigitCharacters(minutes))) * 60
+ Integer.parseInt(Utils.removeNonDigitCharacters(seconds));
return ((convertDurationToInt(days) * 24
+ convertDurationToInt(hours)) * 60
+ convertDurationToInt(minutes)) * 60
+ convertDurationToInt(seconds);
}

/**
* Tries to convert a duration string to an integer without throwing an exception.
* <br/>
* Helper method for {@link #parseDurationString(String)}.
* <br/>
* Note: This method is also used as a workaround for NewPipe#8034 (YT shorts no longer
* display any duration in channels).
*
* @param input The string to process
* @return The converted integer or 0 if the conversion failed.
*/
private static int convertDurationToInt(final String input) {
if (input == null || input.isEmpty()) {
return 0;
}

final String clearedInput = Utils.removeNonDigitCharacters(input);
try {
return Integer.parseInt(clearedInput);
} catch (final NumberFormatException ex) {
return 0;
}
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public long getDuration() throws ParsingException {
}
}

// NewPipe#8034 - YT returns not a correct duration for "YT shorts" videos
if ("SHORTS".equals(duration)) {
return 0;
}

return YoutubeParsingHelper.parseDurationString(duration);
}

Expand Down

0 comments on commit 7598b40

Please sign in to comment.