diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index f6e4a2cce8..77008694df 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -1379,11 +1379,13 @@ def _prepare_filename(self, info_dict, *, outtmpl=None, tmpl_type=None): if not filename: return None - trim_file_name = self.params.get('trim_file_name', False) + trim_file_name = self.params.get('trim_file_name') 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='.') + # cut filename and remove trailing spaces and extra dots + name = no_ext[:trim_file_name].strip().rstrip('.') + filename = join_nonempty(name, *ext, delim='.') if tmpl_type in ('', 'temp'): final_ext, ext = self.params.get('final_ext'), info_dict.get('ext') diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py index 3026300583..f5552ce802 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(str(v).strip() for v in values if v) + return delim.join(map(str, filter(None, values))) def scale_thumbnails_to_max_format_width(formats, thumbnails, url_width_re):