1
mirror of https://github.com/yt-dlp/yt-dlp synced 2024-12-14 03:19:58 +01:00

Check for existing thumbnail/subtitle in final directory

Closes #2275
This commit is contained in:
pukkandan 2022-01-11 14:24:25 +05:30
parent 8bcd404818
commit e04938ab88
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698

View File

@ -2758,6 +2758,16 @@ class YoutubeDL(object):
new_info['http_headers'] = self._calc_headers(new_info) new_info['http_headers'] = self._calc_headers(new_info)
return fd.download(name, new_info, subtitle) return fd.download(name, new_info, subtitle)
def existing_file(self, filepaths, *, default_overwrite=True):
existing_files = list(filter(os.path.exists, orderedSet(filepaths)))
if existing_files and not self.params.get('overwrites', default_overwrite):
return existing_files[0]
for file in existing_files:
self.report_file_delete(file)
os.remove(file)
return None
def process_info(self, info_dict): def process_info(self, info_dict):
"""Process a single resolved IE result. (Modified it in-place)""" """Process a single resolved IE result. (Modified it in-place)"""
@ -2903,26 +2913,14 @@ class YoutubeDL(object):
info_dict.setdefault('__postprocessors', []) info_dict.setdefault('__postprocessors', [])
try: try:
def existing_file(*filepaths): def existing_video_file(*filepaths):
ext = info_dict.get('ext') ext = info_dict.get('ext')
final_ext = self.params.get('final_ext', ext) converted = lambda file: replace_extension(file, self.params.get('final_ext') or ext, ext)
existing_files = [] file = self.existing_file(itertools.chain(*zip(map(converted, filepaths), filepaths)),
for file in orderedSet(filepaths): default_overwrite=False)
if final_ext != ext: if file:
converted = replace_extension(file, final_ext, ext) info_dict['ext'] = os.path.splitext(file)[1][1:]
if os.path.exists(encodeFilename(converted)): return file
existing_files.append(converted)
if os.path.exists(encodeFilename(file)):
existing_files.append(file)
if not existing_files or self.params.get('overwrites', False):
for file in orderedSet(existing_files):
self.report_file_delete(file)
os.remove(encodeFilename(file))
return None
info_dict['ext'] = os.path.splitext(existing_files[0])[1][1:]
return existing_files[0]
success = True success = True
if info_dict.get('requested_formats') is not None: if info_dict.get('requested_formats') is not None:
@ -2976,7 +2974,7 @@ class YoutubeDL(object):
# Ensure filename always has a correct extension for successful merge # Ensure filename always has a correct extension for successful merge
full_filename = correct_ext(full_filename) full_filename = correct_ext(full_filename)
temp_filename = correct_ext(temp_filename) temp_filename = correct_ext(temp_filename)
dl_filename = existing_file(full_filename, temp_filename) dl_filename = existing_video_file(full_filename, temp_filename)
info_dict['__real_download'] = False info_dict['__real_download'] = False
downloaded = [] downloaded = []
@ -3039,7 +3037,7 @@ class YoutubeDL(object):
files_to_move[file] = None files_to_move[file] = None
else: else:
# Just a single file # Just a single file
dl_filename = existing_file(full_filename, temp_filename) dl_filename = existing_video_file(full_filename, temp_filename)
if dl_filename is None or dl_filename == temp_filename: if dl_filename is None or dl_filename == temp_filename:
# dl_filename == temp_filename could mean that the file was partially downloaded with --no-part. # dl_filename == temp_filename could mean that the file was partially downloaded with --no-part.
# So we should try to resume the download # So we should try to resume the download
@ -3758,10 +3756,11 @@ class YoutubeDL(object):
sub_format = sub_info['ext'] sub_format = sub_info['ext']
sub_filename = subtitles_filename(filename, sub_lang, sub_format, info_dict.get('ext')) sub_filename = subtitles_filename(filename, sub_lang, sub_format, info_dict.get('ext'))
sub_filename_final = subtitles_filename(sub_filename_base, sub_lang, sub_format, info_dict.get('ext')) sub_filename_final = subtitles_filename(sub_filename_base, sub_lang, sub_format, info_dict.get('ext'))
if not self.params.get('overwrites', True) and os.path.exists(sub_filename): existing_sub = self.existing_file((sub_filename_final, sub_filename))
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'] = sub_filename sub_info['filepath'] = existing_sub
ret.append((sub_filename, sub_filename_final)) ret.append((existing_sub, sub_filename_final))
continue continue
self.to_screen(f'[info] Writing video subtitles to: {sub_filename}') self.to_screen(f'[info] Writing video subtitles to: {sub_filename}')
@ -3810,11 +3809,12 @@ class YoutubeDL(object):
thumb_filename = replace_extension(filename, thumb_ext, info_dict.get('ext')) thumb_filename = replace_extension(filename, thumb_ext, info_dict.get('ext'))
thumb_filename_final = replace_extension(thumb_filename_base, thumb_ext, info_dict.get('ext')) thumb_filename_final = replace_extension(thumb_filename_base, thumb_ext, info_dict.get('ext'))
if not self.params.get('overwrites', True) and os.path.exists(thumb_filename): existing_thumb = self.existing_file((thumb_filename_final, thumb_filename))
ret.append((thumb_filename, thumb_filename_final)) if existing_thumb:
t['filepath'] = thumb_filename
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
ret.append((existing_thumb, thumb_filename_final))
else: else:
self.to_screen(f'[info] Downloading {thumb_display_id} ...') self.to_screen(f'[info] Downloading {thumb_display_id} ...')
try: try: