diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index eb2e5211aa..9b58d80395 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) - max_file_name: Limit length of filename (extension included) filesystem_encoding: Encoding to use when calculating filename length in bytes windowsfilenames: True: Force filenames to be Windows compatible False: Sanitize filenames only minimally @@ -1441,31 +1440,31 @@ def evaluate_outtmpl_for_filename(self, outtmpl, info_dict, *args, **kwargs): filename = outtmpl % info_dict def parse_max_file_name(max_file_name: str): + # old --trim-filenames format + try: + return 'c', int(max_file_name) + except ValueError: + pass + try: max_length = int(max_file_name[:-1]) except ValueError: - raise ValueError('Invalid --max-filename-length specified') + raise ValueError('Invalid --trim-filenames specified') if max_file_name[-1].lower() == 'c': return 'c', max_length elif max_file_name[-1].lower() == 'b': return 'b', max_length else: - raise ValueError("--max-filename-length must end with 'b' or 'c'") + raise ValueError("--trim-filenames must end with 'b' or 'c'") - max_file_name = self.params.get('max_file_name', DEFAULT_MAX_FILE_NAME) + max_file_name = self.params.get('trim_file_name') or DEFAULT_MAX_FILE_NAME mode, max_file_name = parse_max_file_name(max_file_name) - encoding = self.params.get('filesystem_encoding', sys.getfilesystemencoding()) - - # extension may be replaced later - if mode == 'b': - max_suffix_len = len('.annotations.xml'.encode(encoding)) - else: - max_suffix_len = len('.annotations.xml') + if max_file_name < 1: + raise ValueError('Invalid --trim-filenames specified') + encoding = self.params.get('filesystem_encoding') or sys.getfilesystemencoding() def trim_filename(name: str, length: int): - if length < 1: - raise ValueError('Cannot trim filename to such short length') if mode == 'b': name = name.encode(encoding) name = name[:length] @@ -1475,7 +1474,7 @@ def trim_filename(name: str, length: int): # only trim last component of path - assume the directories are valid names head, tail = os.path.split(filename) - tail = trim_filename(tail, max_file_name - max_suffix_len) + tail = trim_filename(tail, max_file_name) filename = os.path.join(head, tail) return filename + suffix @@ -1498,13 +1497,6 @@ def _prepare_filename(self, info_dict, *, outtmpl=None, tmpl_type=None): force_ext = OUTTMPL_TYPES[tmpl_type] 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/__init__.py b/yt_dlp/__init__.py index 00817b0e5e..69d1e9ed36 100644 --- a/yt_dlp/__init__.py +++ b/yt_dlp/__init__.py @@ -886,7 +886,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, - 'max_file_name': opts.max_file_name, 'filesystem_encoding': opts.filesystem_encoding, 'verbose': opts.verbose, 'dump_intermediate_pages': opts.dump_intermediate_pages, diff --git a/yt_dlp/options.py b/yt_dlp/options.py index 7025928a05..49387a8216 100644 --- a/yt_dlp/options.py +++ b/yt_dlp/options.py @@ -1378,12 +1378,8 @@ def _alias_callback(option, opt_str, value, parser, opts, nargs): help='Sanitize filenames only minimally') filesystem.add_option( '--trim-filenames', '--trim-file-names', metavar='LENGTH', - dest='trim_file_name', default=0, type=int, - help='Limit the filename length (excluding extension) to the specified number of characters') - filesystem.add_option( - '--max-filename-length', metavar='LENGTH', - dest='max_file_name', - help='Limit the filename length (including extension) to the specified number of characters or bytes') + dest='trim_file_name', + help='Limit the filename length (excluding extension) to the specified number of characters or bytes') filesystem.add_option( '--filesystem-encoding', metavar='ENCODING', dest='filesystem_encoding', diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py index 90172125cd..15e92dfecf 100644 --- a/yt_dlp/utils/_utils.py +++ b/yt_dlp/utils/_utils.py @@ -2854,9 +2854,9 @@ def q(qid): # https://en.m.wikipedia.org/wiki/Comparison_of_file_systems#Limits if platform.system() in ('Darwin', 'Windows'): - DEFAULT_MAX_FILE_NAME = '255c' + DEFAULT_MAX_FILE_NAME = f'{255 - len(".annotations.xml")}c' else: - DEFAULT_MAX_FILE_NAME = '255b' + DEFAULT_MAX_FILE_NAME = f'{255 - len(".annotations.xml".encode(sys.getfilesystemencoding()))}b' # As of [1] format syntax is: # %[mapping_key][conversion_flags][minimum_width][.precision][length_modifier]type