Skip to content

Commit

Permalink
Handle ss:xxx in parse_duration(), based on yt-dlp 8bd1c00
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkf committed Jan 27, 2022
1 parent 398dc75 commit d7d8e0c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ def test_parse_duration(self):
self.assertEqual(parse_duration('PT1H0.040S'), 3600.04)
self.assertEqual(parse_duration('PT00H03M30SZ'), 210)
self.assertEqual(parse_duration('P0Y0M0DT0H4M20.880S'), 260.88)
self.assertEqual(parse_duration('01:02:03:050'), 3723.05)
self.assertEqual(parse_duration('103:050'), 103.05)

def test_fix_xml_ampersands(self):
self.assertEqual(
Expand Down
15 changes: 9 additions & 6 deletions youtube_dl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3755,12 +3755,14 @@ def parse_duration(s):
if not isinstance(s, compat_basestring):
return None

s = s.strip()

days, hours, mins, secs, ms = [None] * 5
m = re.match(r'(?:(?:(?:(?P<days>[0-9]+):)?(?P<hours>[0-9]+):)?(?P<mins>[0-9]+):)?(?P<secs>[0-9]+)(?P<ms>\.[0-9]+)?Z?$', s)
m = re.match(r'''(?x)\s*
(?P<before_secs>
(?:(?:(?P<days>[0-9]+):)?(?P<hours>[0-9]+):)?(?P<mins>[0-9]+):)?
(?P<secs>(?(before_secs)[0-9]{1,2}|[0-9]+))
(?P<ms>[.:][0-9]+)?Z?\s*$
''', s)
if m:
days, hours, mins, secs, ms = m.groups()
days, hours, mins, secs, ms = m.group('days', 'hours', 'mins', 'secs', 'ms')
else:
m = re.match(
r'''(?ix)(?:P?
Expand Down Expand Up @@ -3792,6 +3794,7 @@ def parse_duration(s):
m = re.match(r'(?i)(?:(?P<hours>[0-9.]+)\s*(?:hours?)|(?P<mins>[0-9.]+)\s*(?:mins?\.?|minutes?)\s*)Z?$', s)
if m:
hours, mins = m.groups()
days = secs = ms = None
else:
return None

Expand All @@ -3805,7 +3808,7 @@ def parse_duration(s):
if days:
duration += float(days) * 24 * 60 * 60
if ms:
duration += float(ms)
duration += float(ms.replace(':', '.'))
return duration


Expand Down

0 comments on commit d7d8e0c

Please sign in to comment.