]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/postprocessor/common.py
Improved progress reporting (See desc) (#1125)
[yt-dlp.git] / yt_dlp / postprocessor / common.py
index d8ec997d9d47d929710e97337ea35c344fa949f3..376a1c95eff1247110469b77dab4485673290541 100644 (file)
@@ -1,5 +1,6 @@
 from __future__ import unicode_literals
 
+import copy
 import functools
 import os
 
 )
 
 
-class PostProcessor(object):
+class PostProcessorMetaClass(type):
+    @staticmethod
+    def run_wrapper(func):
+        @functools.wraps(func)
+        def run(self, info, *args, **kwargs):
+            self._hook_progress({'status': 'started'}, info)
+            ret = func(self, info, *args, **kwargs)
+            if ret is not None:
+                _, info = ret
+            self._hook_progress({'status': 'finished'}, info)
+            return ret
+        return run
+
+    def __new__(cls, name, bases, attrs):
+        if 'run' in attrs:
+            attrs['run'] = cls.run_wrapper(attrs['run'])
+        return type.__new__(cls, name, bases, attrs)
+
+
+class PostProcessor(metaclass=PostProcessorMetaClass):
     """Post Processor class.
 
     PostProcessor objects can be added to downloaders with their
@@ -34,7 +54,9 @@ class PostProcessor(object):
     _downloader = None
 
     def __init__(self, downloader=None):
-        self._downloader = downloader
+        self._progress_hooks = []
+        self.add_progress_hook(self.report_progress)
+        self.set_downloader(downloader)
         self.PP_NAME = self.pp_key()
 
     @classmethod
@@ -68,6 +90,10 @@ def get_param(self, name, default=None, *args, **kwargs):
     def set_downloader(self, downloader):
         """Sets the downloader for this PP."""
         self._downloader = downloader
+        if not downloader:
+            return
+        for ph in downloader._postprocessor_hooks:
+            self.add_progress_hook(ph)
 
     @staticmethod
     def _restrict_to(*, video=True, audio=True, images=True):
@@ -115,6 +141,39 @@ def _configuration_args(self, exe, *args, **kwargs):
         return _configuration_args(
             self.pp_key(), self.get_param('postprocessor_args'), exe, *args, **kwargs)
 
+    def _hook_progress(self, status, info_dict):
+        if not self._progress_hooks:
+            return
+        info_dict = dict(info_dict)
+        for key in ('__original_infodict', '__postprocessors'):
+            info_dict.pop(key, None)
+        status.update({
+            'info_dict': copy.deepcopy(info_dict),
+            'postprocessor': self.pp_key(),
+        })
+        for ph in self._progress_hooks:
+            ph(status)
+
+    def add_progress_hook(self, ph):
+        # See YoutubeDl.py (search for postprocessor_hooks) for a description of this interface
+        self._progress_hooks.append(ph)
+
+    def report_progress(self, s):
+        s['_default_template'] = '%(postprocessor)s %(status)s' % s
+
+        progress_dict = s.copy()
+        progress_dict.pop('info_dict')
+        progress_dict = {'info': s['info_dict'], 'progress': progress_dict}
+
+        progress_template = self.get_param('progress_template', {})
+        tmpl = progress_template.get('postprocess')
+        if tmpl:
+            self._downloader.to_stdout(self._downloader.evaluate_outtmpl(tmpl, progress_dict))
+
+        self._downloader.to_console_title(self._downloader.evaluate_outtmpl(
+            progress_template.get('postprocess-title') or 'yt-dlp %(progress._default_template)s',
+            progress_dict))
+
 
 class AudioConversionError(PostProcessingError):
     pass