Skip to content

Commit 7598b40

Browse files
committed
Workaround for incorrect duration for "YT shorts" videos in channels
As a workaround 0 is returned as duration for such videos. See also TeamNewPipe/NewPipe#8034
1 parent 164e21b commit 7598b40

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,34 @@ public static int parseDurationString(@Nonnull final String input)
219219
throw new ParsingException("Error duration string with unknown format: " + input);
220220
}
221221

222-
return ((Integer.parseInt(Utils.removeNonDigitCharacters(days)) * 24
223-
+ Integer.parseInt(Utils.removeNonDigitCharacters(hours))) * 60
224-
+ Integer.parseInt(Utils.removeNonDigitCharacters(minutes))) * 60
225-
+ Integer.parseInt(Utils.removeNonDigitCharacters(seconds));
222+
return ((convertDurationToInt(days) * 24
223+
+ convertDurationToInt(hours)) * 60
224+
+ convertDurationToInt(minutes)) * 60
225+
+ convertDurationToInt(seconds);
226+
}
227+
228+
/**
229+
* Tries to convert a duration string to an integer without throwing an exception.
230+
* <br/>
231+
* Helper method for {@link #parseDurationString(String)}.
232+
* <br/>
233+
* Note: This method is also used as a workaround for NewPipe#8034 (YT shorts no longer
234+
* display any duration in channels).
235+
*
236+
* @param input The string to process
237+
* @return The converted integer or 0 if the conversion failed.
238+
*/
239+
private static int convertDurationToInt(final String input) {
240+
if (input == null || input.isEmpty()) {
241+
return 0;
242+
}
243+
244+
final String clearedInput = Utils.removeNonDigitCharacters(input);
245+
try {
246+
return Integer.parseInt(clearedInput);
247+
} catch (final NumberFormatException ex) {
248+
return 0;
249+
}
226250
}
227251

228252
@Nonnull

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ public long getDuration() throws ParsingException {
138138
}
139139
}
140140

141+
// NewPipe#8034 - YT returns not a correct duration for "YT shorts" videos
142+
if ("SHORTS".equals(duration)) {
143+
return 0;
144+
}
145+
141146
return YoutubeParsingHelper.parseDurationString(duration);
142147
}
143148

0 commit comments

Comments
 (0)