From fcafc429275f85d1ba82c1946a1148c53aa52206 Mon Sep 17 00:00:00 2001 From: ds Date: Thu, 24 Aug 2023 20:40:34 +0300 Subject: [PATCH] fix for --trim-file-names bug (#5526) --- test/test_YoutubeDL.py | 9 +++++++++ yt_dlp/YoutubeDL.py | 12 ++++++------ yt_dlp/utils/_utils.py | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py index 3cfb61fb26..2f1a94a6ba 100644 --- a/test/test_YoutubeDL.py +++ b/test/test_YoutubeDL.py @@ -844,6 +844,15 @@ class TestYoutubeDL(unittest.TestCase): # test('%(foo|)s.%(ext)s', ('.mp4', '_.mp4')) # fixme # test('%(foo|)s', ('', '_')) # fixme + # Trim filename + test('%(id)s.%(filesize)s.%(ext)s', ('1234.1024.mp4', '123.mp4'), trim_file_name=3) + test( + '%(id)s.%(filesize)s.%(ext)s', + ('1234.1024.info.json', '123.info.json'), + info=dict(self.outtmpl_info, ext='info.json'), trim_file_name=3 + ) + test('12 34.%(filesize)s.%(ext)s', ('12 34.1024.mp4', '12.mp4'), trim_file_name=3) + # Environment variable expansion for prepare_filename os.environ['__yt_dlp_var'] = 'expanded' envvar = '%__yt_dlp_var%' if compat_os_name == 'nt' else '$__yt_dlp_var' diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 666d89b461..f6e4a2cce8 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -1379,6 +1379,12 @@ class YoutubeDL: if not filename: return None + trim_file_name = self.params.get('trim_file_name', False) + if trim_file_name: + # https://github.com/yt-dlp/yt-dlp/issues/5526#issuecomment-1312783517 + no_ext, *ext = filename.rsplit('.', info_dict.get('ext', '').count('.') + 1) + filename = join_nonempty(no_ext[:trim_file_name], *ext, delim='.') + if tmpl_type in ('', 'temp'): final_ext, ext = self.params.get('final_ext'), info_dict.get('ext') if final_ext and ext and final_ext != ext and filename.endswith(f'.{final_ext}'): @@ -1388,12 +1394,6 @@ class YoutubeDL: if force_ext: filename = replace_extension(filename, force_ext, info_dict.get('ext')) - # https://github.com/blackjack4494/youtube-dlc/issues/85 - trim_file_name = self.params.get('trim_file_name', False) - if trim_file_name: - no_ext, *ext = filename.rsplit('.', 2) - filename = join_nonempty(no_ext[:trim_file_name], *ext, delim='.') - return filename except ValueError as err: self.report_error('Error in output template: ' + str(err) + ' (encoding: ' + repr(preferredencoding()) + ')') diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py index f5552ce802..3026300583 100644 --- a/yt_dlp/utils/_utils.py +++ b/yt_dlp/utils/_utils.py @@ -4733,7 +4733,7 @@ def number_of_digits(number): def join_nonempty(*values, delim='-', from_dict=None): if from_dict is not None: values = (traversal.traverse_obj(from_dict, variadic(v)) for v in values) - return delim.join(map(str, filter(None, values))) + return delim.join(str(v).strip() for v in values if v) def scale_thumbnails_to_max_format_width(formats, thumbnails, url_width_re):