mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2025-12-24 17:08:53 +00:00
Refactored MoveFilesPP to respect non-video files
This commit is contained in:
@@ -1016,8 +1016,10 @@ class FFmpegSubtitlesConvertorPP(FFmpegPostProcessor):
|
||||
'filepath': new_file,
|
||||
}
|
||||
|
||||
info['__files_to_move'][new_file] = replace_extension(
|
||||
info['__files_to_move'][sub['filepath']], new_ext)
|
||||
for sub_info in info['__files_to_move']['requested_subtitles']:
|
||||
if sub_info['lang'] == lang and sub_info['ext'] == sub['ext']:
|
||||
sub_info['current_filepath'] = replace_extension(sub_info['current_filepath'], new_ext)
|
||||
sub_info['final_filepath'] = replace_extension(sub_info['final_filepath'], new_ext)
|
||||
|
||||
return sub_filenames, info
|
||||
|
||||
@@ -1083,16 +1085,20 @@ class FFmpegThumbnailsConvertorPP(FFmpegPostProcessor):
|
||||
return imghdr.what(path) == 'webp'
|
||||
|
||||
def fixup_webp(self, info, idx=-1):
|
||||
thumbnail_filename = info['thumbnails'][idx]['filepath']
|
||||
thumbnail = info['thumbnails'][idx]
|
||||
thumbnail_filename = thumbnail['filepath']
|
||||
_, thumbnail_ext = os.path.splitext(thumbnail_filename)
|
||||
if thumbnail_ext:
|
||||
if thumbnail_ext.lower() != '.webp' and imghdr.what(thumbnail_filename) == 'webp':
|
||||
self.to_screen('Correcting thumbnail "%s" extension to webp' % thumbnail_filename)
|
||||
webp_filename = replace_extension(thumbnail_filename, 'webp')
|
||||
os.replace(thumbnail_filename, webp_filename)
|
||||
info['thumbnails'][idx]['filepath'] = webp_filename
|
||||
info['__files_to_move'][webp_filename] = replace_extension(
|
||||
info['__files_to_move'].pop(thumbnail_filename), 'webp')
|
||||
thumbnail['filepath'] = webp_filename
|
||||
|
||||
for thumb_info in info['__files_to_move']['thumbnails']:
|
||||
if thumb_info['id'] == thumbnail['id']:
|
||||
thumb_info['current_filepath'] = replace_extension(thumbnail_filename, 'webp')
|
||||
thumb_info['final_filepath'] = replace_extension(thumb_info['final_filepath'], 'webp')
|
||||
|
||||
@staticmethod
|
||||
def _options(target_ext):
|
||||
@@ -1130,8 +1136,11 @@ class FFmpegThumbnailsConvertorPP(FFmpegPostProcessor):
|
||||
continue
|
||||
thumbnail_dict['filepath'] = self.convert_thumbnail(original_thumbnail, target_ext)
|
||||
files_to_delete.append(original_thumbnail)
|
||||
info['__files_to_move'][thumbnail_dict['filepath']] = replace_extension(
|
||||
info['__files_to_move'][original_thumbnail], target_ext)
|
||||
|
||||
for thumb_info in info['__files_to_move']['thumbnails']:
|
||||
if thumb_info['id'] == thumbnail_dict['id']:
|
||||
thumb_info['current_filepath'] = replace_extension(thumb_info['current_filepath'], target_ext)
|
||||
thumb_info['final_filepath'] = replace_extension(thumb_info['final_filepath'], target_ext)
|
||||
|
||||
if not has_thumbnail:
|
||||
self.to_screen('There aren\'t any thumbnails to convert')
|
||||
|
||||
@@ -4,13 +4,11 @@ from .common import PostProcessor
|
||||
from ..compat import shutil
|
||||
from ..utils import (
|
||||
PostProcessingError,
|
||||
decodeFilename,
|
||||
encodeFilename,
|
||||
make_dir,
|
||||
)
|
||||
|
||||
|
||||
class MoveFilesAfterDownloadPP(PostProcessor):
|
||||
FILETYPE_KEYS = ['media', 'thumbnails', 'requested_subtitles']
|
||||
|
||||
def __init__(self, downloader=None, downloaded=True):
|
||||
PostProcessor.__init__(self, downloader)
|
||||
@@ -20,34 +18,77 @@ class MoveFilesAfterDownloadPP(PostProcessor):
|
||||
def pp_key(cls):
|
||||
return 'MoveFiles'
|
||||
|
||||
def expand_relative_paths(self, files_to_move, finaldir):
|
||||
for filetype in self.FILETYPE_KEYS:
|
||||
if filetype not in files_to_move:
|
||||
continue
|
||||
for file_attrs in files_to_move[filetype]:
|
||||
if not os.path.isabs(file_attrs['final_filepath']):
|
||||
file_attrs['final_filepath'] = os.path.join(finaldir, file_attrs['final_filepath'])
|
||||
if not os.path.isabs(file_attrs['current_filepath']):
|
||||
file_attrs['current_filepath'] = os.path.abspath(file_attrs['current_filepath'])
|
||||
|
||||
return files_to_move
|
||||
|
||||
def write_filepath_into_info(self, info, filetype, file_attrs):
|
||||
if filetype == 'media':
|
||||
info['filepath'] = file_attrs['final_filepath']
|
||||
return
|
||||
|
||||
elif filetype == 'thumbnails':
|
||||
for filetype_dict in info[filetype]:
|
||||
if filetype_dict['id'] == file_attrs['id']:
|
||||
filetype_dict['filepath'] = file_attrs['final_filepath']
|
||||
|
||||
elif filetype == 'requested_subtitles':
|
||||
lang = file_attrs['lang']
|
||||
if lang in info[filetype]:
|
||||
info[filetype][lang]['filepath'] = file_attrs['final_filepath']
|
||||
|
||||
def run(self, info):
|
||||
dl_path, dl_name = os.path.split(encodeFilename(info['filepath']))
|
||||
dl_path, dl_name = os.path.split(info['filepath'])
|
||||
finaldir = info.get('__finaldir', dl_path)
|
||||
finalpath = os.path.join(finaldir, dl_name)
|
||||
info['__files_to_move']['media'] = []
|
||||
|
||||
if self._downloaded:
|
||||
info['__files_to_move'][info['filepath']] = decodeFilename(finalpath)
|
||||
info['__files_to_move']['media'] = [{ 'current_filepath': info['filepath'], 'final_filepath': dl_name }]
|
||||
|
||||
make_newfilename = lambda old: decodeFilename(os.path.join(finaldir, os.path.basename(encodeFilename(old))))
|
||||
for oldfile, newfile in info['__files_to_move'].items():
|
||||
if not newfile:
|
||||
newfile = make_newfilename(oldfile)
|
||||
if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)):
|
||||
files_to_move = self.expand_relative_paths(info['__files_to_move'], finaldir)
|
||||
|
||||
for filetype in self.FILETYPE_KEYS:
|
||||
if filetype not in files_to_move:
|
||||
continue
|
||||
if not os.path.exists(encodeFilename(oldfile)):
|
||||
self.report_warning('File "%s" cannot be found' % oldfile)
|
||||
continue
|
||||
if os.path.exists(encodeFilename(newfile)):
|
||||
if self.get_param('overwrites', True):
|
||||
self.report_warning('Replacing existing file "%s"' % newfile)
|
||||
os.remove(encodeFilename(newfile))
|
||||
else:
|
||||
self.report_warning(
|
||||
'Cannot move file "%s" out of temporary directory since "%s" already exists. '
|
||||
% (oldfile, newfile))
|
||||
|
||||
for file_attrs in files_to_move[filetype]:
|
||||
current_filepath = file_attrs['current_filepath']
|
||||
final_filepath = file_attrs['final_filepath']
|
||||
|
||||
if not current_filepath or not final_filepath:
|
||||
continue
|
||||
make_dir(newfile, PostProcessingError)
|
||||
self.to_screen(f'Moving file "{oldfile}" to "{newfile}"')
|
||||
shutil.move(oldfile, newfile) # os.rename cannot move between volumes
|
||||
|
||||
info['filepath'] = finalpath
|
||||
if current_filepath == final_filepath:
|
||||
# This ensures the infojson contains the full filepath even
|
||||
# when --no-overwrites is used
|
||||
self.write_filepath_into_info(info, filetype, file_attrs)
|
||||
continue
|
||||
|
||||
if not os.path.exists(current_filepath):
|
||||
self.report_warning('File "%s" cannot be found' % current_filepath)
|
||||
continue
|
||||
|
||||
if os.path.exists(final_filepath):
|
||||
if self.get_param('overwrites', True):
|
||||
self.report_warning('Replacing existing file "%s"' % final_filepath)
|
||||
os.remove(final_filepath)
|
||||
else:
|
||||
self.report_warning(
|
||||
'Cannot move file "%s" out of temporary directory since "%s" already exists. '
|
||||
% (current_filepath, final_filepath))
|
||||
continue
|
||||
|
||||
make_dir(final_filepath, PostProcessingError)
|
||||
self.to_screen(f'Moving file "{current_filepath}" to "{final_filepath}"')
|
||||
shutil.move(current_filepath, final_filepath) # os.rename cannot move between volumes
|
||||
self.write_filepath_into_info(info, filetype, file_attrs)
|
||||
|
||||
return [], info
|
||||
|
||||
Reference in New Issue
Block a user