]> jfr.im git - yt-dlp.git/commitdiff
Option `--windows-filenames` to force use of windows compatible filenames
authorpukkandan <redacted>
Wed, 17 Feb 2021 19:09:38 +0000 (00:39 +0530)
committerpukkandan <redacted>
Wed, 17 Feb 2021 19:36:40 +0000 (01:06 +0530)
* Also changed `--trim-file-name` to `--trim-filenames` to be similar to related options

Related: https://web.archive.org/web/20210217190806/https://old.reddit.com/r/youtubedl/comments/llc4o5/do_you_guys_also_have_this_error

:ci skip dl

README.md
youtube_dlc/YoutubeDL.py
youtube_dlc/__init__.py
youtube_dlc/options.py
youtube_dlc/utils.py

index b9ba226b776bab7e42f7895b89460f2fb00ad797..be5195f1e6b9a70fa5544adc11eb3116531458aa 100644 (file)
--- a/README.md
+++ b/README.md
@@ -361,6 +361,12 @@ ## Filesystem Options:
                                      filenames
     --no-restrict-filenames          Allow Unicode characters, "&" and spaces in
                                      filenames (default)
+    --windows-filenames              Force filenames to be windows compatible
+    --no-windows-filenames           Make filenames windows compatible only if
+                                     using windows (default)
+    --trim-filenames LENGTH          Limit the filename length (excluding
+                                     extension) to the specified number of
+                                     characters
     -w, --no-overwrites              Do not overwrite any files
     --force-overwrites               Overwrite all video and metadata files.
                                      This option includes --no-continue
@@ -411,8 +417,6 @@ ## Filesystem Options:
                                      may change
     --no-cache-dir                   Disable filesystem caching
     --rm-cache-dir                   Delete all filesystem cache files
-    --trim-file-name LENGTH          Limit the filename length (extension
-                                     excluded)
 
 ## Thumbnail Images:
     --write-thumbnail                Write thumbnail image to disk
index f88bc793ef689f00dce389911d98a5259ba84b15..125ce767cd8a0c50d6d8fcd4f7781155cb872b55 100644 (file)
@@ -868,13 +868,6 @@ def _prepare_filename(self, info_dict, tmpl_type='default'):
                     sub_ext = fn_groups[-2]
                 filename = '.'.join(filter(None, [fn_groups[0][:trim_file_name], sub_ext, ext]))
 
-            # Temporary fix for #4787
-            # 'Treat' all problem characters by passing filename through preferredencoding
-            # to workaround encoding issues with subprocess on python2 @ Windows
-            if sys.version_info < (3, 0) and sys.platform == 'win32':
-                filename = encodeFilename(filename, True).decode(preferredencoding())
-            filename = sanitize_path(filename)
-
             return filename
         except ValueError as err:
             self.report_error('Error in output template: ' + str(err) + ' (encoding: ' + repr(preferredencoding()) + ')')
@@ -901,7 +894,14 @@ def prepare_filename(self, info_dict, dir_type='', warn=False):
         assert isinstance(homepath, compat_str)
         subdir = expand_path(paths.get(dir_type, '').strip()) if dir_type else ''
         assert isinstance(subdir, compat_str)
-        return sanitize_path(os.path.join(homepath, subdir, filename))
+        path = os.path.join(homepath, subdir, filename)
+
+        # Temporary fix for #4787
+        # 'Treat' all problem characters by passing filename through preferredencoding
+        # to workaround encoding issues with subprocess on python2 @ Windows
+        if sys.version_info < (3, 0) and sys.platform == 'win32':
+            path = encodeFilename(path, True).decode(preferredencoding())
+        return sanitize_path(path, force=self.params.get('windowsfilenames'))
 
     def _match_entry(self, info_dict, incomplete):
         """ Returns None if the file should be downloaded """
index a14c8424ef0e27f89ffdd7c10c36923c569faca0..6451ed8c889378caa6e858d9cee4c5f48dea1231 100644 (file)
@@ -440,6 +440,7 @@ def report_args_compat(arg, name):
         'autonumber_size': opts.autonumber_size,
         'autonumber_start': opts.autonumber_start,
         'restrictfilenames': opts.restrictfilenames,
+        'windowsfilenames': opts.windowsfilenames,
         'ignoreerrors': opts.ignoreerrors,
         'force_generic_extractor': opts.force_generic_extractor,
         'ratelimit': opts.ratelimit,
index cb8e8236ae4b0d69938e02b740cdc1b1ebaae9bb..bb37554ec151f8a3a1d7cfdfa4adc0a80ce78849 100644 (file)
@@ -878,8 +878,20 @@ def _dict_from_multiple_values_options_callback(
         help='Restrict filenames to only ASCII characters, and avoid "&" and spaces in filenames')
     filesystem.add_option(
         '--no-restrict-filenames',
-        action='store_false', dest='restrictfilenames', default=False,
+        action='store_false', dest='restrictfilenames',
         help='Allow Unicode characters, "&" and spaces in filenames (default)')
+    filesystem.add_option(
+        '--windows-filenames',
+        action='store_true', dest='windowsfilenames', default=False,
+        help='Force filenames to be windows compatible')
+    filesystem.add_option(
+        '--no-windows-filenames',
+        action='store_false', dest='windowsfilenames',
+        help='Make filenames windows compatible only if using windows (default)')
+    filesystem.add_option(
+        '--trim-filenames', '--trim-file-names', metavar='LENGTH',
+        dest='trim_file_name', default=0, type=int,
+        help='Limit the filename length (excluding extension) to the specified number of characters')
     filesystem.add_option(
         '-A', '--auto-number',
         action='store_true', dest='autonumber', default=False,
@@ -992,10 +1004,6 @@ def _dict_from_multiple_values_options_callback(
         '--rm-cache-dir',
         action='store_true', dest='rm_cachedir',
         help='Delete all filesystem cache files')
-    filesystem.add_option(
-        '--trim-file-name', metavar='LENGTH',
-        dest='trim_file_name', default=0, type=int,
-        help='Limit the filename length (extension excluded)')
 
     thumbnail = optparse.OptionGroup(parser, 'Thumbnail Images')
     thumbnail.add_option(
index 5aaec4f171e6a028b09a71ed2408235762aa69c2..99cbb8a28c8b9ec0f786b45fef62ff8b508d0898 100644 (file)
@@ -2125,13 +2125,17 @@ def replace_insane(char):
     return result
 
 
-def sanitize_path(s):
+def sanitize_path(s, force=False):
     """Sanitizes and normalizes path on Windows"""
-    if sys.platform != 'win32':
+    if sys.platform == 'win32':
+        drive_or_unc, _ = os.path.splitdrive(s)
+        if sys.version_info < (2, 7) and not drive_or_unc:
+            drive_or_unc, _ = os.path.splitunc(s)
+    elif force:
+        drive_or_unc = ''
+    else:
         return s
-    drive_or_unc, _ = os.path.splitdrive(s)
-    if sys.version_info < (2, 7) and not drive_or_unc:
-        drive_or_unc, _ = os.path.splitunc(s)
+
     norm_path = os.path.normpath(remove_start(s, drive_or_unc)).split(os.path.sep)
     if drive_or_unc:
         norm_path.pop(0)