From e4f0ffe2618d135901a93dca796e7f7a0dba5abb Mon Sep 17 00:00:00 2001 From: Rasmus M Date: Tue, 3 Mar 2020 23:57:49 +0100 Subject: [PATCH] Add new feature: option to choose temp folder --- youtube_dl/YoutubeDL.py | 7 +++++-- youtube_dl/__init__.py | 1 + youtube_dl/options.py | 3 +++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 19370f62b..8f9606234 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -631,7 +631,7 @@ class YoutubeDL(object): except UnicodeEncodeError: self.to_screen('[download] The file has already been downloaded') - def prepare_filename(self, info_dict): + def prepare_filename(self, info_dict, temp=False): """Generate the output filename.""" try: template_dict = dict(info_dict) @@ -710,6 +710,9 @@ class YoutubeDL(object): # title "Hello $PATH", we don't want `$PATH` to be expanded. filename = expand_path(outtmpl).replace(sep, '') % template_dict + if temp and self.params.get("tempdir", None) is not None: + filename = os.path.join(self.params["tempdir"], os.path.basename(filename)) + # Temporary fix for #4787 # 'Treat' all problem characters by passing filename through preferredencoding # to workaround encoding issues with subprocess on python2 @ Windows @@ -1912,7 +1915,7 @@ class YoutubeDL(object): new_info = dict(info_dict) new_info.update(f) fname = prepend_extension( - self.prepare_filename(new_info), + self.prepare_filename(new_info, temp=True), 'f%s' % f['format_id'], new_info['ext']) if not ensure_dir_exists(fname): return diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 9a659fc65..a831acc74 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -392,6 +392,7 @@ def _real_main(argv=None): 'max_views': opts.max_views, 'daterange': date, 'cachedir': opts.cachedir, + 'tempdir': opts.tempdir, 'youtube_print_sig_code': opts.youtube_print_sig_code, 'age_limit': opts.age_limit, 'download_archive': download_archive_fn, diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 8826b382c..ba10ab85d 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -756,6 +756,9 @@ def parseOpts(overrideArguments=None): filesystem.add_option( '--cache-dir', dest='cachedir', default=None, metavar='DIR', help='Location in the filesystem where youtube-dl can store some downloaded information permanently. By default $XDG_CACHE_HOME/youtube-dl or ~/.cache/youtube-dl . At the moment, only YouTube player files (for videos with obfuscated signatures) are cached, but that may change.') + filesystem.add_option( + '--temp-dir', dest='tempdir', default=None, metavar='DIR', + help='Directory where youtube-dl can store some temporary files.') filesystem.add_option( '--no-cache-dir', action='store_const', const=False, dest='cachedir', help='Disable filesystem caching')