mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2025-03-09 12:50:23 -05:00
Updated logic for determining file extensions
This commit is contained in:
parent
28d5051546
commit
c3fccc58cf
2 changed files with 28 additions and 13 deletions
|
@ -3697,6 +3697,7 @@ def post_process(self, filename, info, files_to_move=None):
|
|||
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 @@ def _write_subtitles(self, info_dict, filename):
|
|||
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 @@ def _write_subtitles(self, info_dict, filename):
|
|||
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 @@ def _write_subtitles(self, info_dict, filename):
|
|||
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 @@ def _write_thumbnails(self, label, info_dict, filename, thumb_filename_base=None
|
|||
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 @@ def _write_thumbnails(self, label, info_dict, filename, thumb_filename_base=None
|
|||
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 @@ def _write_thumbnails(self, label, info_dict, filename, thumb_filename_base=None
|
|||
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:
|
||||
|
|
|
@ -8,10 +8,10 @@
|
|||
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 @@ def move_file_and_write_to_info(self, info_dict, relevant_dict=None, output_file
|
|||
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 @@ def move_file_and_write_to_info(self, info_dict, relevant_dict=None, output_file
|
|||
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 @@ def move_file(self, info_dict, current_filepath, final_filepath):
|
|||
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
|
||||
|
||||
|
|
Loading…
Reference in a new issue