From 385a27fad18e64ded70cfe0e14044d9b5038ec99 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Wed, 9 Jun 2021 19:47:50 +0530 Subject: [PATCH] Improve offset parsing in outtmpl --- test/test_YoutubeDL.py | 1 + yt_dlp/YoutubeDL.py | 60 ++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py index e6508b889..0ffcaed91 100644 --- a/test/test_YoutubeDL.py +++ b/test/test_YoutubeDL.py @@ -752,6 +752,7 @@ class TestYoutubeDL(unittest.TestCase): test('%(formats.3)s', 'NA') test('%(formats.:2:-1)r', repr(FORMATS[:2:-1])) test('%(formats.0.id.-1+id)f', '1235.000000') + test('%(formats.0.id.-1+formats.1.id.-1)d', '3') # Empty filename test('%(foo|)s-%(bar|)s.%(ext)s', '-.mp4') diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 72fc9ad52..0edbb4119 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -847,23 +847,24 @@ class YoutubeDL(object): 'autonumber': self.params.get('autonumber_size') or 5, } - EXTERNAL_FORMAT_RE = STR_FORMAT_RE.format('[^)]*') + TMPL_DICT = {} + EXTERNAL_FORMAT_RE = re.compile(STR_FORMAT_RE.format('[^)]*')) + MATH_FUNCTIONS = { + '+': float.__add__, + '-': float.__sub__, + } # Field is of the form key1.key2... # where keys (except first) can be string, int or slice - FIELD_RE = r'\w+(?:\.(?:\w+|[-\d]*(?::[-\d]*){0,2}))*' + FIELD_RE = r'\w+(?:\.(?:\w+|{num}|{num}?(?::{num}?){{1,2}}))*'.format(num=r'(?:-?\d+)') + MATH_FIELD_RE = r'''{field}|{num}'''.format(field=FIELD_RE, num=r'-?\d+(?:.\d+)?') + MATH_OPERATORS_RE = r'(?:%s)' % '|'.join(map(re.escape, MATH_FUNCTIONS.keys())) INTERNAL_FORMAT_RE = re.compile(r'''(?x) (?P-)? - (?P{0}) - (?P(?:[-+]-?(?:\d+(?:\.\d+)?|{0}))*) + (?P{field}) + (?P(?:{math_op}{math_field})*) (?:>(?P.+?))? (?:\|(?P.*?))? - $'''.format(FIELD_RE)) - MATH_OPERATORS_RE = re.compile(r'(?