diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py index 5242cf88f..c873af4b4 100644 --- a/test/test_YoutubeDL.py +++ b/test/test_YoutubeDL.py @@ -874,32 +874,33 @@ class TestYoutubeDL(unittest.TestCase): }), r'^30fps$') def test_postprocessors(self): - filename = 'post-processor-testfile.mp4' - audiofile = filename + '.mp3' + filename = 'post-processor-testfile' + video_file = filename + '.mp4' + audio_file = filename + '.mp3' class SimplePP(PostProcessor): def run(self, info): - with open(audiofile, 'w') as f: + with open(audio_file, 'w') as f: f.write('EXAMPLE') return [info['filepath']], info def run_pp(params, PP): - with open(filename, 'w') as f: + with open(video_file, 'w') as f: f.write('EXAMPLE') ydl = YoutubeDL(params) ydl.add_post_processor(PP()) - ydl.post_process(filename, {'filepath': filename}) + ydl.post_process(video_file, {'filepath': video_file}) - run_pp({'keepvideo': True}, SimplePP) - self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) - self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) - os.unlink(filename) - os.unlink(audiofile) + run_pp({'keepvideo': True, 'outtmpl': filename}, SimplePP) + self.assertTrue(os.path.exists(video_file), '%s doesn\'t exist' % video_file) + self.assertTrue(os.path.exists(audio_file), '%s doesn\'t exist' % audio_file) + os.unlink(video_file) + os.unlink(audio_file) - run_pp({'keepvideo': False}, SimplePP) - self.assertFalse(os.path.exists(filename), '%s exists' % filename) - self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) - os.unlink(audiofile) + run_pp({'keepvideo': False, 'outtmpl': filename}, SimplePP) + self.assertFalse(os.path.exists(video_file), '%s exists' % video_file) + self.assertTrue(os.path.exists(audio_file), '%s doesn\'t exist' % audio_file) + os.unlink(audio_file) class ModifierPP(PostProcessor): def run(self, info): @@ -907,9 +908,9 @@ class TestYoutubeDL(unittest.TestCase): f.write('MODIFIED') return [], info - run_pp({'keepvideo': False}, ModifierPP) - self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) - os.unlink(filename) + run_pp({'keepvideo': False, 'outtmpl': filename}, ModifierPP) + self.assertTrue(os.path.exists(video_file), '%s doesn\'t exist' % video_file) + os.unlink(video_file) def test_match_filter(self): first = { diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index ea4e4fe08..f6921297d 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -1982,7 +1982,7 @@ class YoutubeDL: 'playlist', ie_result, self.prepare_filename(ie_copy, 'pl_infojson')) if _infojson_written is None: return - + description_file = self._write_description('playlist', ie_result, self.prepare_filename(ie_copy, 'pl_description')) if description_file is None: return @@ -4265,7 +4265,6 @@ class YoutubeDL: self.to_screen(f'[info] Writing {label} description to: {filename}') with open(filename, 'w', encoding='utf-8') as descfile: descfile.write(info_dict['description']) - info_dict['description_filepath'] = filename except OSError: self.report_error(f'Cannot write {label} description file {filename}') return None diff --git a/yt_dlp/postprocessor/movefilesafterdownload.py b/yt_dlp/postprocessor/movefilesafterdownload.py index b80ba6898..e56761bc0 100644 --- a/yt_dlp/postprocessor/movefilesafterdownload.py +++ b/yt_dlp/postprocessor/movefilesafterdownload.py @@ -8,7 +8,7 @@ from ..utils import ( make_dir, replace_extension ) -import pdb + class MoveFilesAfterDownloadPP(PostProcessor): TOP_LEVEL_KEYS = ['filepath'] @@ -26,8 +26,9 @@ class MoveFilesAfterDownloadPP(PostProcessor): @classmethod def pp_key(cls): return 'MoveFiles' - - def move_file_and_write_to_info(self, info_dict, relevant_dict, output_file_type): + + def move_file_and_write_to_info(self, info_dict, relevant_dict=None, output_file_type=None): + relevant_dict = relevant_dict or info_dict if 'filepath' not in relevant_dict: return @@ -39,14 +40,6 @@ class MoveFilesAfterDownloadPP(PostProcessor): 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) - - print('*******************') - print("output_file_type", output_file_type) - print("name_format", name_format) - print("current_filepath", current_filepath) - print("final_filepath", final_filepath) - print("move_result", move_result) - print('*******************') if move_result: relevant_dict['filepath'] = move_result @@ -56,9 +49,9 @@ class MoveFilesAfterDownloadPP(PostProcessor): def move_file(self, info_dict, current_filepath, final_filepath): if not current_filepath or not final_filepath: return - - dl_path, _dl_name = os.path.split(info_dict['filepath']) - finaldir = info_dict.get('__finaldir', os.path.abspath(dl_path)) + + dl_parent_folder = os.path.split(info_dict['filepath'])[0] + finaldir = info_dict.get('__finaldir', os.path.abspath(dl_parent_folder)) if not os.path.isabs(current_filepath): current_filepath = os.path.join(finaldir, current_filepath) @@ -86,24 +79,12 @@ class MoveFilesAfterDownloadPP(PostProcessor): 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 - + return final_filepath def run(self, info): - dl_path, dl_name = os.path.split(info['filepath']) - finaldir = info.get('__finaldir', os.path.abspath(dl_path)) - # TODO: add one single key to infodict with ALL downloaded files - # TODO: test with --path temp and stuff - # TODO: make the below work with not-currently-written filepaths like description, annotations, etc - # - Descriptions work, have to do all the other ones too - # - I lied, this should become another post-processor - # TODO: [DONE] probably something with relative paths into absolute again? - # TODO: remove all __files_to_move stuff when done - # TODO: add net-new filepaths to `sanitize_info` - # TODO: consider adding a `infojson_filepath` key in addition to the `infojson_filename` key where the former is the fullpath - - for filepath_key in self.TOP_LEVEL_KEYS: - self.move_file_and_write_to_info(info, info, None) + # 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(): if key not in info: @@ -116,89 +97,3 @@ class MoveFilesAfterDownloadPP(PostProcessor): self.move_file_and_write_to_info(info, file_dict, output_file_type) return [], info - -# class MoveFilesAfterDownloadPP(PostProcessor): -# FILETYPE_KEYS = ['media', 'thumbnails', 'requested_subtitles'] - -# def __init__(self, downloader=None, downloaded=True): -# PostProcessor.__init__(self, downloader) -# self._downloaded = downloaded - -# @classmethod -# 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: -# files_to_move[filetype] = [] - -# 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'] - -# 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(info['filepath']) -# finaldir = info.get('__finaldir', os.path.abspath(dl_path)) - -# th = self._downloader.prepare_filename(info, 'thumbnail') -# pdb.set_trace() -# print("th", th) - -# if self._downloaded: -# info['__files_to_move']['media'] = [{'current_filepath': info['filepath'], 'final_filepath': dl_name}] - -# files_to_move = self.expand_relative_paths(info['__files_to_move'], finaldir) - -# for filetype in self.FILETYPE_KEYS: -# 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 - -# 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