Skip to content

A feature-rich command-line audio/video downloader

[utils] `parse_resolution`: Support fps suffixes (#17073)

doe10801249676e98ae
diff --git a/test/test_utils.py b/test/test_utils.py
index 8f86d151b2..14dcdd9cb6 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -1405,7 +1405,11 @@ class TestUtil(unittest.TestCase):
self.assertEqual(parse_resolution('1920×1080 '), {'width': 1920, 'height': 1080})
self.assertEqual(parse_resolution('1920 x 1080'), {'width': 1920, 'height': 1080})
self.assertEqual(parse_resolution('720p'), {'height': 720})
+ self.assertEqual(parse_resolution('1080p60'), {'height': 1080})
+ self.assertEqual(parse_resolution('1080p120', parse_fps=True), {'height': 1080, 'fps': 120})
self.assertEqual(parse_resolution('4k'), {'height': 2160})
+ self.assertEqual(parse_resolution('4K60'), {'height': 2160})
+ self.assertEqual(parse_resolution('4K120', parse_fps=True), {'height': 2160, 'fps': 120})
self.assertEqual(parse_resolution('8K'), {'height': 4320})
self.assertEqual(parse_resolution('pre_1920x1080_post'), {'width': 1920, 'height': 1080})
self.assertEqual(parse_resolution('ep1x2'), {})
diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py
index 7c6dee7cc1..edaf7a665f 100644
--- a/yt_dlp/utils/_utils.py
+++ b/yt_dlp/utils/_utils.py
@@ -1873,7 +1873,8 @@ def parse_count(s):
return str_to_int(mobj.group(1))
-def parse_resolution(s, *, lenient=False):
+@partial_application
+def parse_resolution(s, *, lenient=False, parse_fps=False):
if s is None:
return {}
@@ -1887,13 +1888,19 @@ def parse_resolution(s, *, lenient=False):
'height': int(mobj.group('h')),
}
- mobj = re.search(r'(?<![a-zA-Z0-9])(\d+)[pPiI](?![a-zA-Z0-9])', s)
+ fps_suffix = r'(?P<fps>\d{2,3})?'
+ mobj = re.search(rf'(?<![a-zA-Z0-9])(?P<height>\d+)[pPiI]{fps_suffix}(?![a-zA-Z0-9])', s)
+ scale = 1
+ if not mobj:
+ mobj = re.search(rf'\b(?P<height>[48])[kK]{fps_suffix}\b', s)
+ scale = 540
if mobj:
- return {'height': int(mobj.group(1))}
+ res = {'height': int(mobj.group('height')) * scale}
+ if parse_fps:
+ if fps := mobj.group('fps'):
+ res['fps'] = int(fps)
- mobj = re.search(r'\b([48])[kK]\b', s)
- if mobj:
- return {'height': int(mobj.group(1)) * 540}
+ return res
if lenient:
mobj = re.search(r'(?<!\d)(\d{2,5})w(?![a-zA-Z0-9])', s)