]> jfr.im git - yt-dlp.git/commitdiff
Create `to_screen` and similar functions in postprocessor/common
authorpukkandan <redacted>
Sun, 10 Jan 2021 13:44:54 +0000 (19:14 +0530)
committerpukkandan <redacted>
Sun, 10 Jan 2021 16:52:24 +0000 (22:22 +0530)
`to_screen`, `report_warning`, `report_error`, `write_debug`, `get_param`

This is a first step in standardizing these function. This has to be done eventually for extractors and downloaders too

youtube_dlc/postprocessor/common.py
youtube_dlc/postprocessor/embedthumbnail.py
youtube_dlc/postprocessor/ffmpeg.py
youtube_dlc/postprocessor/sponskrub.py
youtube_dlc/postprocessor/xattrpp.py

index 6e84ff592a24c3cfe50586ba77d7d081e670980d..1a893d05f3670a61c317fbeb6f401697f99c9f13 100644 (file)
@@ -37,7 +37,25 @@ def __init__(self, downloader=None):
             self.PP_NAME = self.__class__.__name__[:-2]
 
     def to_screen(self, text, *args, **kwargs):
-        return self._downloader.to_screen('[%s] %s' % (self.PP_NAME, text), *args, **kwargs)
+        if self._downloader:
+            return self._downloader.to_screen('[%s] %s' % (self.PP_NAME, text), *args, **kwargs)
+
+    def report_warning(self, text, *args, **kwargs):
+        if self._downloader:
+            return self._downloader.report_warning(text, *args, **kwargs)
+
+    def report_error(self, text, *args, **kwargs):
+        if self._downloader:
+            return self._downloader.report_error(text, *args, **kwargs)
+
+    def write_debug(self, text, *args, **kwargs):
+        if self.get_param('verbose', False):
+            return self._downloader.to_screen('[debug] %s' % text, *args, **kwargs)
+
+    def get_param(self, name, default=None, *args, **kwargs):
+        if self._downloader:
+            return self._downloader.params.get(name, default, *args, **kwargs)
+        return default
 
     def set_downloader(self, downloader):
         """Sets the downloader for this PP."""
@@ -64,10 +82,10 @@ def try_utime(self, path, atime, mtime, errnote='Cannot update utime of file'):
         try:
             os.utime(encodeFilename(path), (atime, mtime))
         except Exception:
-            self._downloader.report_warning(errnote)
+            self.report_warning(errnote)
 
     def _configuration_args(self, default=[]):
-        args = self._downloader.params.get('postprocessor_args', {})
+        args = self.get_param('postprocessor_args', {})
         if isinstance(args, list):  # for backward compatibility
             args = {'default': args, 'sponskrub': []}
         return cli_configuration_args(args, self.PP_NAME.lower(), args.get('default', []))
index 3055a8c28942e36a8b1a3f2b70aacb14a9f63e5a..8e78ede00a395270438b852bec036eda62c2fb1b 100644 (file)
@@ -41,8 +41,7 @@ def run(self, info):
         thumbnail_filename = info['thumbnails'][-1]['filename']
 
         if not os.path.exists(encodeFilename(thumbnail_filename)):
-            self._downloader.report_warning(
-                'Skipping embedding the thumbnail because the file is missing.')
+            self.report_warning('Skipping embedding the thumbnail because the file is missing.')
             return [], info
 
         def is_webp(path):
@@ -125,8 +124,7 @@ def is_webp(path):
 
             self.to_screen('Adding thumbnail to "%s"' % filename)
 
-            if self._downloader.params.get('verbose', False):
-                self._downloader.to_screen('[debug] AtomicParsley command line: %s' % shell_quote(cmd))
+            self.verbose_message('AtomicParsley command line: %s' % shell_quote(cmd))
 
             p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
             stdout, stderr = process_communicate_or_kill(p)
@@ -140,7 +138,7 @@ def is_webp(path):
             # for formats that don't support thumbnails (like 3gp) AtomicParsley
             # won't create to the temporary file
             if b'No changes' in stdout:
-                self._downloader.report_warning('The file format doesn\'t support embedding a thumbnail')
+                self.report_warning('The file format doesn\'t support embedding a thumbnail')
             else:
                 os.remove(encodeFilename(filename))
                 os.rename(encodeFilename(temp_filename), encodeFilename(filename))
index c6ba1e221e925fccdf2440527f47e4a95e8fdcec..9c6065018c3052cbe03d235309e9588e3f3ae7b8 100644 (file)
@@ -68,8 +68,7 @@ def check_version(self):
                 self._versions[self.basename], required_version):
             warning = 'Your copy of %s is outdated, update %s to version %s or newer if you encounter any errors.' % (
                 self.basename, self.basename, required_version)
-            if self._downloader:
-                self._downloader.report_warning(warning)
+            self.report_warning(warning)
 
     @staticmethod
     def get_versions(downloader=None):
@@ -99,11 +98,11 @@ def get_ffmpeg_version(path):
         self._paths = None
         self._versions = None
         if self._downloader:
-            prefer_ffmpeg = self._downloader.params.get('prefer_ffmpeg', True)
-            location = self._downloader.params.get('ffmpeg_location')
+            prefer_ffmpeg = self.get_param('prefer_ffmpeg', True)
+            location = self.get_param('ffmpeg_location')
             if location is not None:
                 if not os.path.exists(location):
-                    self._downloader.report_warning(
+                    self.report_warning(
                         'ffmpeg-location %s does not exist! '
                         'Continuing without avconv/ffmpeg.' % (location))
                     self._versions = {}
@@ -111,7 +110,7 @@ def get_ffmpeg_version(path):
                 elif not os.path.isdir(location):
                     basename = os.path.splitext(os.path.basename(location))[0]
                     if basename not in programs:
-                        self._downloader.report_warning(
+                        self.report_warning(
                             'Cannot identify executable %s, its basename should be one of %s. '
                             'Continuing without avconv/ffmpeg.' %
                             (location, ', '.join(programs)))
@@ -177,9 +176,7 @@ def get_audio_codec(self, path):
                     encodeFilename(self.executable, True),
                     encodeArgument('-i')]
             cmd.append(encodeFilename(self._ffmpeg_filename_argument(path), True))
-            if self._downloader.params.get('verbose', False):
-                self._downloader.to_screen(
-                    '[debug] %s command line: %s' % (self.basename, shell_quote(cmd)))
+            self.write_debug('%s command line: %s' % (self.basename, shell_quote(cmd)))
             handle = subprocess.Popen(
                 cmd, stderr=subprocess.PIPE,
                 stdout=subprocess.PIPE, stdin=subprocess.PIPE)
@@ -228,8 +225,7 @@ def run_ffmpeg_multiple_files(self, input_paths, out_path, opts):
                 + [encodeArgument(o) for o in opts]
                 + [encodeFilename(self._ffmpeg_filename_argument(out_path), True)])
 
-        if self._downloader.params.get('verbose', False):
-            self._downloader.to_screen('[debug] ffmpeg command line: %s' % shell_quote(cmd))
+        self.write_debug('ffmpeg command line: %s' % shell_quote(cmd))
         p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
         stdout, stderr = process_communicate_or_kill(p)
         if p.returncode != 0:
@@ -566,8 +562,7 @@ def can_merge(self):
                        'youtube-dlc will download single file media. '
                        'Update %s to version %s or newer to fix this.') % (
                            self.basename, self.basename, required_version)
-            if self._downloader:
-                self._downloader.report_warning(warning)
+            self.report_warning(warning)
             return False
         return True
 
@@ -656,7 +651,7 @@ def run(self, info):
             new_file = subtitles_filename(filename, lang, new_ext, info.get('ext'))
 
             if ext in ('dfxp', 'ttml', 'tt'):
-                self._downloader.report_warning(
+                self.report_warning(
                     'You have requested to convert dfxp (TTML) subtitles into another format, '
                     'which results in style information loss')
 
index 37f6c029089aef749610792fbef9782b7253db0f..9215913bcad803a36aa3a67f71a57c499fbde061 100644 (file)
@@ -46,16 +46,16 @@ def run(self, information):
             self.to_screen('Skipping sponskrub since it is not a YouTube video')
             return [], information
         if self.cutout and not self.force and not information.get('__real_download', False):
-            self._downloader.to_screen(
-                '[sponskrub] Skipping sponskrub since the video was already downloaded. '
+            self.report_warning(
+                'Skipping sponskrub since the video was already downloaded. '
                 'Use --sponskrub-force to run sponskrub anyway')
             return [], information
 
         self.to_screen('Trying to %s sponsor sections' % ('remove' if self.cutout else 'mark'))
         if self.cutout:
-            self._downloader.to_screen('WARNING: Cutting out sponsor segments will cause the subtitles to go out of sync.')
+            self.report_warning('Cutting out sponsor segments will cause the subtitles to go out of sync.')
             if not information.get('__real_download', False):
-                self._downloader.to_screen('WARNING: If sponskrub is run multiple times, unintended parts of the video could be cut out.')
+                self.report_warning('If sponskrub is run multiple times, unintended parts of the video could be cut out.')
 
         filename = information['filepath']
         temp_filename = filename + '.' + self._temp_ext + os.path.splitext(filename)[1]
@@ -68,8 +68,7 @@ def run(self, information):
         cmd += ['--', information['id'], filename, temp_filename]
         cmd = [encodeArgument(i) for i in cmd]
 
-        if self._downloader.params.get('verbose', False):
-            self._downloader.to_screen('[debug] sponskrub command line: %s' % shell_quote(cmd))
+        self.write_debug('sponskrub command line: %s' % shell_quote(cmd))
         p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
         stdout, stderr = p.communicate()
 
index 85834db45a24e4cf29324cee4525c39009d3e178..3d31f0ce5bf102e8feb96d57d6452cd605ee83a6 100644 (file)
@@ -57,16 +57,16 @@ def run(self, info):
             return [], info
 
         except XAttrUnavailableError as e:
-            self._downloader.report_error(str(e))
+            self.report_error(str(e))
             return [], info
 
         except XAttrMetadataError as e:
             if e.reason == 'NO_SPACE':
-                self._downloader.report_warning(
+                self.report_warning(
                     'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
                     + (('Some ' if num_written else '') + 'extended attributes are not written.').capitalize())
             elif e.reason == 'VALUE_TOO_LONG':
-                self._downloader.report_warning(
+                self.report_warning(
                     'Unable to write extended attributes due to too long values.')
             else:
                 msg = 'This filesystem doesn\'t support extended attributes. '
@@ -74,5 +74,5 @@ def run(self, info):
                     msg += 'You need to use NTFS.'
                 else:
                     msg += '(You may have to enable them in your /etc/fstab)'
-                self._downloader.report_error(msg)
+                self.report_error(msg)
             return [], info