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['filepath'] = filename
|
||||||
info = self.run_all_pps('post_process', info, additional_pps=info.get('__postprocessors'))
|
info = self.run_all_pps('post_process', info, additional_pps=info.get('__postprocessors'))
|
||||||
info = self.run_pp(MoveFilesAfterDownloadPP(self), info)
|
info = self.run_pp(MoveFilesAfterDownloadPP(self), info)
|
||||||
|
del info['__multiple_thumbnails']
|
||||||
return self.run_all_pps('after_move', info)
|
return self.run_all_pps('after_move', info)
|
||||||
|
|
||||||
def _make_archive_id(self, info_dict):
|
def _make_archive_id(self, info_dict):
|
||||||
|
@ -4287,7 +4288,7 @@ def _write_subtitles(self, info_dict, filename):
|
||||||
if existing_sub:
|
if existing_sub:
|
||||||
self.to_screen(f'[info] Video subtitle {sub_lang}.{sub_format} is already present')
|
self.to_screen(f'[info] Video subtitle {sub_lang}.{sub_format} is already present')
|
||||||
sub_info['filepath'] = existing_sub
|
sub_info['filepath'] = existing_sub
|
||||||
ret.append((existing_sub, sub_filename_final))
|
ret.append(existing_sub)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self.to_screen(f'[info] Writing video subtitles to: {sub_filename}')
|
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:
|
with open(sub_filename, 'w', encoding='utf-8', newline='') as subfile:
|
||||||
subfile.write(sub_info['data'])
|
subfile.write(sub_info['data'])
|
||||||
sub_info['filepath'] = sub_filename
|
sub_info['filepath'] = sub_filename
|
||||||
ret.append((sub_filename, sub_filename_final))
|
ret.append(sub_filename)
|
||||||
continue
|
continue
|
||||||
except OSError:
|
except OSError:
|
||||||
self.report_error(f'Cannot write video subtitles file {sub_filename}')
|
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'))
|
sub_copy.setdefault('http_headers', info_dict.get('http_headers'))
|
||||||
self.dl(sub_filename, sub_copy, subtitle=True)
|
self.dl(sub_filename, sub_copy, subtitle=True)
|
||||||
sub_info['filepath'] = sub_filename
|
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:
|
except (DownloadError, ExtractorError, IOError, OSError, ValueError) + network_exceptions as err:
|
||||||
msg = f'Unable to download video subtitles for {sub_lang!r}: {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'
|
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')
|
self.to_screen(f'[info] There are no {label} thumbnails to download')
|
||||||
return ret
|
return ret
|
||||||
multiple = write_all and len(thumbnails) > 1
|
multiple = write_all and len(thumbnails) > 1
|
||||||
|
info_dict['__multiple_thumbnails'] = multiple
|
||||||
|
|
||||||
if thumb_filename_base is None:
|
if thumb_filename_base is None:
|
||||||
thumb_filename_base = filename
|
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' % (
|
self.to_screen('[info] %s is already present' % (
|
||||||
thumb_display_id if multiple else f'{label} thumbnail').capitalize())
|
thumb_display_id if multiple else f'{label} thumbnail').capitalize())
|
||||||
t['filepath'] = existing_thumb
|
t['filepath'] = existing_thumb
|
||||||
ret.append((existing_thumb, thumb_filename_final))
|
ret.append(existing_thumb)
|
||||||
else:
|
else:
|
||||||
self.to_screen(f'[info] Downloading {thumb_display_id} ...')
|
self.to_screen(f'[info] Downloading {thumb_display_id} ...')
|
||||||
try:
|
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}')
|
self.to_screen(f'[info] Writing {thumb_display_id} to: {thumb_filename}')
|
||||||
with open(encodeFilename(thumb_filename), 'wb') as thumbf:
|
with open(encodeFilename(thumb_filename), 'wb') as thumbf:
|
||||||
shutil.copyfileobj(uf, thumbf)
|
shutil.copyfileobj(uf, thumbf)
|
||||||
ret.append((thumb_filename, thumb_filename_final))
|
ret.append(thumb_filename)
|
||||||
t['filepath'] = thumb_filename
|
t['filepath'] = thumb_filename
|
||||||
except network_exceptions as err:
|
except network_exceptions as err:
|
||||||
if isinstance(err, HTTPError) and err.status == 404:
|
if isinstance(err, HTTPError) and err.status == 404:
|
||||||
|
|
|
@ -8,10 +8,10 @@
|
||||||
make_dir,
|
make_dir,
|
||||||
replace_extension
|
replace_extension
|
||||||
)
|
)
|
||||||
|
import pdb
|
||||||
|
|
||||||
|
|
||||||
class MoveFilesAfterDownloadPP(PostProcessor):
|
class MoveFilesAfterDownloadPP(PostProcessor):
|
||||||
TOP_LEVEL_KEYS = ['filepath']
|
|
||||||
# Map of the keys that contain moveable files and the 'type' of the file
|
# Map of the keys that contain moveable files and the 'type' of the file
|
||||||
# for generating the output filename
|
# for generating the output filename
|
||||||
CHILD_KEYS = {
|
CHILD_KEYS = {
|
||||||
|
@ -33,12 +33,7 @@ def move_file_and_write_to_info(self, info_dict, relevant_dict=None, output_file
|
||||||
return
|
return
|
||||||
|
|
||||||
output_file_type = output_file_type or ''
|
output_file_type = output_file_type or ''
|
||||||
current_filepath = relevant_dict['filepath']
|
current_filepath, final_filepath = self.determine_filepath(info_dict, relevant_dict, output_file_type)
|
||||||
# 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)
|
|
||||||
move_result = self.move_file(info_dict, current_filepath, final_filepath)
|
move_result = self.move_file(info_dict, current_filepath, final_filepath)
|
||||||
|
|
||||||
if move_result:
|
if move_result:
|
||||||
|
@ -46,6 +41,17 @@ def move_file_and_write_to_info(self, info_dict, relevant_dict=None, output_file
|
||||||
else:
|
else:
|
||||||
del relevant_dict['filepath']
|
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):
|
def move_file(self, info_dict, current_filepath, final_filepath):
|
||||||
if not current_filepath or not final_filepath:
|
if not current_filepath or not final_filepath:
|
||||||
return
|
return
|
||||||
|
@ -83,10 +89,17 @@ def move_file(self, info_dict, current_filepath, final_filepath):
|
||||||
return final_filepath
|
return final_filepath
|
||||||
|
|
||||||
def run(self, info):
|
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)
|
# This represents the main media file (using the 'filepath' key)
|
||||||
self.move_file_and_write_to_info(info)
|
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:
|
if key not in info:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue