From c3fccc58cfce89cd8758c357d8265775e7f36c87 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Fri, 26 Apr 2024 15:26:26 -0700 Subject: [PATCH] Updated logic for determining file extensions --- yt_dlp/YoutubeDL.py | 12 ++++---- .../postprocessor/movefilesafterdownload.py | 29 ++++++++++++++----- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 2ace14a3b..dca355b02 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -3697,6 +3697,7 @@ class YoutubeDL: info['filepath'] = filename info = self.run_all_pps('post_process', info, additional_pps=info.get('__postprocessors')) info = self.run_pp(MoveFilesAfterDownloadPP(self), info) + del info['__multiple_thumbnails'] return self.run_all_pps('after_move', info) def _make_archive_id(self, info_dict): @@ -4287,7 +4288,7 @@ class YoutubeDL: if existing_sub: self.to_screen(f'[info] Video subtitle {sub_lang}.{sub_format} is already present') sub_info['filepath'] = existing_sub - ret.append((existing_sub, sub_filename_final)) + ret.append(existing_sub) continue self.to_screen(f'[info] Writing video subtitles to: {sub_filename}') @@ -4298,7 +4299,7 @@ class YoutubeDL: with open(sub_filename, 'w', encoding='utf-8', newline='') as subfile: subfile.write(sub_info['data']) sub_info['filepath'] = sub_filename - ret.append((sub_filename, sub_filename_final)) + ret.append(sub_filename) continue except OSError: self.report_error(f'Cannot write video subtitles file {sub_filename}') @@ -4309,7 +4310,7 @@ class YoutubeDL: sub_copy.setdefault('http_headers', info_dict.get('http_headers')) self.dl(sub_filename, sub_copy, subtitle=True) sub_info['filepath'] = sub_filename - ret.append((sub_filename, sub_filename_final)) + ret.append(sub_filename) except (DownloadError, ExtractorError, IOError, OSError, ValueError) + network_exceptions as err: msg = f'Unable to download video subtitles for {sub_lang!r}: {err}' if self.params.get('ignoreerrors') is not True: # False or 'only_download' @@ -4329,6 +4330,7 @@ class YoutubeDL: self.to_screen(f'[info] There are no {label} thumbnails to download') return ret multiple = write_all and len(thumbnails) > 1 + info_dict['__multiple_thumbnails'] = multiple if thumb_filename_base is None: thumb_filename_base = filename @@ -4350,7 +4352,7 @@ class YoutubeDL: self.to_screen('[info] %s is already present' % ( thumb_display_id if multiple else f'{label} thumbnail').capitalize()) t['filepath'] = existing_thumb - ret.append((existing_thumb, thumb_filename_final)) + ret.append(existing_thumb) else: self.to_screen(f'[info] Downloading {thumb_display_id} ...') try: @@ -4358,7 +4360,7 @@ class YoutubeDL: self.to_screen(f'[info] Writing {thumb_display_id} to: {thumb_filename}') with open(encodeFilename(thumb_filename), 'wb') as thumbf: shutil.copyfileobj(uf, thumbf) - ret.append((thumb_filename, thumb_filename_final)) + ret.append(thumb_filename) t['filepath'] = thumb_filename except network_exceptions as err: if isinstance(err, HTTPError) and err.status == 404: diff --git a/yt_dlp/postprocessor/movefilesafterdownload.py b/yt_dlp/postprocessor/movefilesafterdownload.py index e56761bc0..e8ba86d78 100644 --- a/yt_dlp/postprocessor/movefilesafterdownload.py +++ b/yt_dlp/postprocessor/movefilesafterdownload.py @@ -8,10 +8,10 @@ from ..utils import ( make_dir, replace_extension ) +import pdb class MoveFilesAfterDownloadPP(PostProcessor): - TOP_LEVEL_KEYS = ['filepath'] # Map of the keys that contain moveable files and the 'type' of the file # for generating the output filename CHILD_KEYS = { @@ -33,12 +33,7 @@ class MoveFilesAfterDownloadPP(PostProcessor): return output_file_type = output_file_type or '' - current_filepath = relevant_dict['filepath'] - # This approach is needed to preserved indexed thumbnail paths from `--write-all-thumbnails` - # and also to support user-defined extensions (eg: `%(title)s.temp.%(ext)s`) - extension = ''.join(Path(current_filepath).suffixes) - name_format = self._downloader.prepare_filename(info_dict, output_file_type) - final_filepath = replace_extension(name_format, extension) + current_filepath, final_filepath = self.determine_filepath(info_dict, relevant_dict, output_file_type) move_result = self.move_file(info_dict, current_filepath, final_filepath) if move_result: @@ -46,6 +41,17 @@ class MoveFilesAfterDownloadPP(PostProcessor): else: del relevant_dict['filepath'] + def determine_filepath(self, info_dict, relevant_dict, output_file_type): + current_filepath = relevant_dict['filepath'] + prepared_filepath = self._downloader.prepare_filename(info_dict, output_file_type) + + if (output_file_type == 'thumbnail' and info_dict['__multiple_thumbnails']) or output_file_type == 'subtitle': + desired_extension = ''.join(Path(current_filepath).suffixes[-2:]) + else: + desired_extension = Path(current_filepath).suffix + + return current_filepath, replace_extension(prepared_filepath, desired_extension) + def move_file(self, info_dict, current_filepath, final_filepath): if not current_filepath or not final_filepath: return @@ -83,10 +89,17 @@ class MoveFilesAfterDownloadPP(PostProcessor): return final_filepath def run(self, info): + # Map of the keys that contain moveable files and the 'type' of the file + # for generating the output filename + child_keys = { + 'thumbnails': 'thumbnail', + 'requested_subtitles': 'subtitle' + } + # This represents the main media file (using the 'filepath' key) self.move_file_and_write_to_info(info) - for key, output_file_type in self.CHILD_KEYS.items(): + for key, output_file_type in child_keys.items(): if key not in info: continue