Skip to content

Commit c963521

Browse files
authored
[YouTube] Improve duration parsing
1 parent 1be2707 commit c963521

File tree

1 file changed

+10
-32
lines changed

1 file changed

+10
-32
lines changed

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

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -318,38 +318,16 @@ public static int parseDurationString(@Nonnull final String input)
318318
? input.split(":")
319319
: input.split("\\.");
320320

321-
String days = "0";
322-
String hours = "0";
323-
String minutes = "0";
324-
final String seconds;
325-
326-
switch (splitInput.length) {
327-
case 4:
328-
days = splitInput[0];
329-
hours = splitInput[1];
330-
minutes = splitInput[2];
331-
seconds = splitInput[3];
332-
break;
333-
case 3:
334-
hours = splitInput[0];
335-
minutes = splitInput[1];
336-
seconds = splitInput[2];
337-
break;
338-
case 2:
339-
minutes = splitInput[0];
340-
seconds = splitInput[1];
341-
break;
342-
case 1:
343-
seconds = splitInput[0];
344-
break;
345-
default:
346-
throw new ParsingException("Error duration string with unknown format: " + input);
347-
}
348-
349-
return ((convertDurationToInt(days) * 24
350-
+ convertDurationToInt(hours)) * 60
351-
+ convertDurationToInt(minutes)) * 60
352-
+ convertDurationToInt(seconds);
321+
final int[] units = {24, 60, 60, 1};
322+
final int offset = units.length - splitInput.length;
323+
if (offset < 0) {
324+
throw new ParsingException("Error duration string with unknown format: " + input);
325+
}
326+
int duration = 0;
327+
for (int i = 0; i < splitInput.length; i++) {
328+
duration = units[i + offset] * (duration + convertDurationToInt(splitInput[i]));
329+
}
330+
return duration;
353331
}
354332

355333
/**

0 commit comments

Comments
 (0)