From 21aa58a2f178a80c751f487c1906e874ef69e90c Mon Sep 17 00:00:00 2001 From: maleficarium Date: Sun, 3 May 2015 07:11:05 +0300 Subject: [PATCH] Fix for unicode filenames Fix for the issue discussed here: https://github.com/rg3/youtube-dl/issues/4787 https://github.com/rg3/youtube-dl/issues/4787 by hashing the input and output filenames before processing them and then restoring them. --- youtube_dl/YoutubeDL.py | 4 ++-- youtube_dl/postprocessor/ffmpeg.py | 14 +++++++++++--- youtube_dl/postprocessor/hashNames.py | 9 +++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 youtube_dl/postprocessor/hashNames.py diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index eb7470f72..cb016776f 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -570,8 +570,8 @@ class YoutubeDL(object): # 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()) + #if sys.version_info < (3, 0) and sys.platform == 'win32': + # filename = encodeFilename(filename, True).decode(preferredencoding()) return filename except ValueError as err: self.report_error('Error in output template: ' + str(err) + ' (encoding: ' + repr(preferredencoding()) + ')') diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index 214de39f9..2435ad3b6 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -4,7 +4,7 @@ import io import os import subprocess import time - +import hashNames from .common import AudioConversionError, PostProcessor @@ -131,12 +131,17 @@ class FFmpegPostProcessor(PostProcessor): os.stat(encodeFilename(path)).st_mtime for path in input_paths) files_cmd = [] + original_names = [] for path in input_paths: - files_cmd.extend([encodeArgument('-i'), encodeFilename(path, True)]) + hashed_path = hashNames.hashRename(path) + os.rename(path, hashed_path) + original_names.append([hashed_path, path]) + files_cmd.extend([encodeArgument('-i'), encodeFilename(hashed_path, True)]) + hashed_out_path = hashNames.hashRename(out_path) cmd = ([encodeFilename(self.executable, True), encodeArgument('-y')] + files_cmd + [encodeArgument(o) for o in opts] + - [encodeFilename(self._ffmpeg_filename_argument(out_path), True)]) + [encodeFilename(self._ffmpeg_filename_argument(hashed_out_path), True)]) if self._downloader.params.get('verbose', False): self._downloader.to_screen('[debug] ffmpeg command line: %s' % shell_quote(cmd)) @@ -146,6 +151,9 @@ class FFmpegPostProcessor(PostProcessor): stderr = stderr.decode('utf-8', 'replace') msg = stderr.strip().split('\n')[-1] raise FFmpegPostProcessorError(msg) + for o_name in original_names: + os.rename(o_name[0], o_name[1]) + os.rename(hashed_out_path, out_path) self.try_utime(out_path, oldest_mtime, oldest_mtime) def run_ffmpeg(self, path, out_path, opts): diff --git a/youtube_dl/postprocessor/hashNames.py b/youtube_dl/postprocessor/hashNames.py new file mode 100644 index 000000000..f174f55ec --- /dev/null +++ b/youtube_dl/postprocessor/hashNames.py @@ -0,0 +1,9 @@ +import os +import hashlib + +#Hash the file names to drop unicode characters for FFMPEG +def hashRename(fileN): + ext = os.path.splitext(fileN) + fileHash = hashlib.sha224(fileN.encode('utf-8')).hexdigest() + hash_name = fileHash+ext[1] + return hash_name \ No newline at end of file