diff options
Diffstat (limited to 'youtube_dlc/postprocessor')
-rw-r--r-- | youtube_dlc/postprocessor/__init__.py | 2 | ||||
-rw-r--r-- | youtube_dlc/postprocessor/movefilesafterdownload.py | 52 |
2 files changed, 54 insertions, 0 deletions
diff --git a/youtube_dlc/postprocessor/__init__.py b/youtube_dlc/postprocessor/__init__.py index e160909a7..840a83b0e 100644 --- a/youtube_dlc/postprocessor/__init__.py +++ b/youtube_dlc/postprocessor/__init__.py @@ -17,6 +17,7 @@ from .ffmpeg import ( from .xattrpp import XAttrMetadataPP from .execafterdownload import ExecAfterDownloadPP from .metadatafromtitle import MetadataFromTitlePP +from .movefilesafterdownload import MoveFilesAfterDownloadPP from .sponskrub import SponSkrubPP @@ -39,6 +40,7 @@ __all__ = [ 'FFmpegVideoConvertorPP', 'FFmpegVideoRemuxerPP', 'MetadataFromTitlePP', + 'MoveFilesAfterDownloadPP', 'SponSkrubPP', 'XAttrMetadataPP', ] diff --git a/youtube_dlc/postprocessor/movefilesafterdownload.py b/youtube_dlc/postprocessor/movefilesafterdownload.py new file mode 100644 index 000000000..3f7f529a9 --- /dev/null +++ b/youtube_dlc/postprocessor/movefilesafterdownload.py @@ -0,0 +1,52 @@ +from __future__ import unicode_literals +import os +import shutil + +from .common import PostProcessor +from ..utils import ( + encodeFilename, + make_dir, + PostProcessingError, +) +from ..compat import compat_str + + +class MoveFilesAfterDownloadPP(PostProcessor): + + def __init__(self, downloader, files_to_move): + PostProcessor.__init__(self, downloader) + self.files_to_move = files_to_move + + @classmethod + def pp_key(cls): + return 'MoveFiles' + + def run(self, info): + if info.get('__dl_filename') is None: + return [], info + self.files_to_move.setdefault(info['__dl_filename'], '') + outdir = os.path.dirname(os.path.abspath(encodeFilename(info['__final_filename']))) + + for oldfile, newfile in self.files_to_move.items(): + if not os.path.exists(encodeFilename(oldfile)): + self.report_warning('File "%s" cannot be found' % oldfile) + continue + if not newfile: + newfile = compat_str(os.path.join(outdir, os.path.basename(encodeFilename(oldfile)))) + if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)): + continue + if os.path.exists(encodeFilename(newfile)): + if self.get_param('overwrites', True): + self.report_warning('Replacing existing file "%s"' % newfile) + os.path.remove(encodeFilename(newfile)) + else: + self.report_warning( + 'Cannot move file "%s" out of temporary directory since "%s" already exists. ' + % (oldfile, newfile)) + continue + make_dir(newfile, PostProcessingError) + self.to_screen('Moving file "%s" to "%s"' % (oldfile, newfile)) + shutil.move(oldfile, newfile) # os.rename cannot move between volumes + + info['filepath'] = info['__final_filename'] + return [], info |