Skip to content

Commit 2211a24

Browse files
authored
Merge pull request #971 from lrusso96/patch-1
[YouTube] Improve duration parsing
2 parents f6eefdc + c963521 commit 2211a24

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
@@ -316,38 +316,16 @@ public static int parseDurationString(@Nonnull final String input)
316316
? input.split(":")
317317
: input.split("\\.");
318318

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

353331
/**

0 commit comments

Comments
 (0)