diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py index 4348cac6f..d65cc1153 100644 --- a/test/test_YoutubeDL.py +++ b/test/test_YoutubeDL.py @@ -714,7 +714,7 @@ def test_add_extra_info(self): def test_prepare_outtmpl_and_filename(self): def test(tmpl, expected, *, info=None, **params): if 'trim_file_name' not in params: - params['trim_file_name'] = 0 # disable trimming + params['trim_file_name'] = 'notrim' # disable trimming params['outtmpl'] = tmpl ydl = FakeYDL(params) ydl._num_downloads = 1 @@ -935,7 +935,7 @@ def gen(): test('%(title6)s.%(ext)s', 'あ' * 3 + '.mp4', trim_file_name='11b', filesystem_encoding='utf-8') test('%(title6)s.%(ext)s', 'あ' * 4 + '.mp4', trim_file_name='12b', filesystem_encoding='utf-8') test('%(title6)s.%(ext)s', 'あ' * 6 + '.mp4', trim_file_name='12b', filesystem_encoding='utf-16le') - test('folder/%(title6)s.%(ext)s', f'folder{os.path.sep}あああ.mp4', trim_file_name='3c') + test('folder/%(title6)s.%(ext)s', f'fol{os.path.sep}あああ.mp4', trim_file_name='3c') def test_format_note(self): ydl = YoutubeDL() diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 72250fb34..287556075 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -267,7 +267,6 @@ class YoutubeDL: outtmpl_na_placeholder: Placeholder for unavailable meta fields. restrictfilenames: Do not allow "&" and spaces in file names trim_file_name: Limit length of filename (extension excluded) - trim_file_name_mode: Mode of filename trimming ('c' for characters or 'b' for bytes) filesystem_encoding: Encoding to use when calculating filename length in bytes windowsfilenames: True: Force filenames to be Windows compatible False: Sanitize filenames only minimally @@ -1439,8 +1438,13 @@ def evaluate_outtmpl(self, outtmpl, info_dict, *args, trim_filename=False, **kwa outtmpl = self.escape_outtmpl(outtmpl) filename = outtmpl % info_dict - max_file_name = self.params.get('trim_file_name') - mode = self.params.get('trim_file_name_mode') + def parse_trim_file_name(trim_file_name): + if trim_file_name is None or trim_file_name == 'notrim': + return 0, None + mobj = re.match(r'(?:(?P\d+)(?Pb|c)?|notrim)', trim_file_name) + return int(mobj.group('length')), mobj.group('mode') or 'c' + + max_file_name, mode = parse_trim_file_name(self.params.get('trim_file_name')) if max_file_name == 0: # no maximum return filename + suffix diff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.py index b22c4c1dd..69fb2c7e5 100644 --- a/yt_dlp/__init__.py +++ b/yt_dlp/__init__.py @@ -430,15 +430,8 @@ def metadataparser_actions(f): } # Other options - opts.trim_file_name_mode = 'c' - if opts.trim_file_name is not None: - mobj = re.match(r'(?:(?P\d+)(?Pb|c)?|notrim)', opts.trim_file_name) - validate(mobj, 'trim filenames', opts.trim_file_name) - if opts.trim_file_name == 'notrim': - opts.trim_file_name = 0 - else: - opts.trim_file_name = int(mobj.group('length')) - opts.trim_file_name_mode = mobj.group('mode') or 'c' + validate_regex('trim filenames', opts.trim_file_name, r'(?:\d+[bc]?|notrim)') + if opts.filesystem_encoding is not None: try: codecs.lookup(opts.filesystem_encoding) @@ -902,7 +895,6 @@ def parse_options(argv=None): 'max_downloads': opts.max_downloads, 'prefer_free_formats': opts.prefer_free_formats, 'trim_file_name': opts.trim_file_name, - 'trim_file_name_mode': opts.trim_file_name_mode, 'filesystem_encoding': opts.filesystem_encoding, 'verbose': opts.verbose, 'dump_intermediate_pages': opts.dump_intermediate_pages,