common.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import functools
  2. import json
  3. import os
  4. from ..networking import Request
  5. from ..networking.exceptions import HTTPError, network_exceptions
  6. from ..utils import (
  7. PostProcessingError,
  8. RetryManager,
  9. _configuration_args,
  10. deprecation_warning,
  11. encodeFilename,
  12. )
  13. class PostProcessorMetaClass(type):
  14. @staticmethod
  15. def run_wrapper(func):
  16. @functools.wraps(func)
  17. def run(self, info, *args, **kwargs):
  18. info_copy = self._copy_infodict(info)
  19. self._hook_progress({'status': 'started'}, info_copy)
  20. ret = func(self, info, *args, **kwargs)
  21. if ret is not None:
  22. _, info = ret
  23. self._hook_progress({'status': 'finished'}, info_copy)
  24. return ret
  25. return run
  26. def __new__(cls, name, bases, attrs):
  27. if 'run' in attrs:
  28. attrs['run'] = cls.run_wrapper(attrs['run'])
  29. return type.__new__(cls, name, bases, attrs)
  30. class PostProcessor(metaclass=PostProcessorMetaClass):
  31. """Post Processor class.
  32. PostProcessor objects can be added to downloaders with their
  33. add_post_processor() method. When the downloader has finished a
  34. successful download, it will take its internal chain of PostProcessors
  35. and start calling the run() method on each one of them, first with
  36. an initial argument and then with the returned value of the previous
  37. PostProcessor.
  38. PostProcessor objects follow a "mutual registration" process similar
  39. to InfoExtractor objects.
  40. Optionally PostProcessor can use a list of additional command-line arguments
  41. with self._configuration_args.
  42. """
  43. _downloader = None
  44. def __init__(self, downloader=None):
  45. self._progress_hooks = []
  46. self.add_progress_hook(self.report_progress)
  47. self.set_downloader(downloader)
  48. self.PP_NAME = self.pp_key()
  49. @classmethod
  50. def pp_key(cls):
  51. name = cls.__name__[:-2]
  52. return name[6:] if name[:6].lower() == 'ffmpeg' else name
  53. def to_screen(self, text, prefix=True, *args, **kwargs):
  54. if self._downloader:
  55. tag = f'[{self.PP_NAME}] ' if prefix else ''
  56. return self._downloader.to_screen(f'{tag}{text}', *args, **kwargs)
  57. def report_warning(self, text, *args, **kwargs):
  58. if self._downloader:
  59. return self._downloader.report_warning(text, *args, **kwargs)
  60. def deprecation_warning(self, msg):
  61. warn = getattr(self._downloader, 'deprecation_warning', deprecation_warning)
  62. return warn(msg, stacklevel=1)
  63. def deprecated_feature(self, msg):
  64. if self._downloader:
  65. return self._downloader.deprecated_feature(msg)
  66. return deprecation_warning(msg, stacklevel=1)
  67. def report_error(self, text, *args, **kwargs):
  68. self.deprecation_warning('"yt_dlp.postprocessor.PostProcessor.report_error" is deprecated. '
  69. 'raise "yt_dlp.utils.PostProcessingError" instead')
  70. if self._downloader:
  71. return self._downloader.report_error(text, *args, **kwargs)
  72. def write_debug(self, text, *args, **kwargs):
  73. if self._downloader:
  74. return self._downloader.write_debug(text, *args, **kwargs)
  75. def _delete_downloaded_files(self, *files_to_delete, **kwargs):
  76. if self._downloader:
  77. return self._downloader._delete_downloaded_files(*files_to_delete, **kwargs)
  78. for filename in set(filter(None, files_to_delete)):
  79. os.remove(filename)
  80. def get_param(self, name, default=None, *args, **kwargs):
  81. if self._downloader:
  82. return self._downloader.params.get(name, default, *args, **kwargs)
  83. return default
  84. def set_downloader(self, downloader):
  85. """Sets the downloader for this PP."""
  86. self._downloader = downloader
  87. for ph in getattr(downloader, '_postprocessor_hooks', []):
  88. self.add_progress_hook(ph)
  89. def _copy_infodict(self, info_dict):
  90. return getattr(self._downloader, '_copy_infodict', dict)(info_dict)
  91. @staticmethod
  92. def _restrict_to(*, video=True, audio=True, images=True, simulated=True):
  93. allowed = {'video': video, 'audio': audio, 'images': images}
  94. def decorator(func):
  95. @functools.wraps(func)
  96. def wrapper(self, info):
  97. if not simulated and (self.get_param('simulate') or self.get_param('skip_download')):
  98. return [], info
  99. format_type = (
  100. 'video' if info.get('vcodec') != 'none'
  101. else 'audio' if info.get('acodec') != 'none'
  102. else 'images')
  103. if allowed[format_type]:
  104. return func(self, info)
  105. else:
  106. self.to_screen(f'Skipping {format_type}')
  107. return [], info
  108. return wrapper
  109. return decorator
  110. def run(self, information):
  111. """Run the PostProcessor.
  112. The "information" argument is a dictionary like the ones
  113. composed by InfoExtractors. The only difference is that this
  114. one has an extra field called "filepath" that points to the
  115. downloaded file.
  116. This method returns a tuple, the first element is a list of the files
  117. that can be deleted, and the second of which is the updated
  118. information.
  119. In addition, this method may raise a PostProcessingError
  120. exception if post processing fails.
  121. """
  122. return [], information # by default, keep file and do nothing
  123. def try_utime(self, path, atime, mtime, errnote='Cannot update utime of file'):
  124. try:
  125. os.utime(encodeFilename(path), (atime, mtime))
  126. except Exception:
  127. self.report_warning(errnote)
  128. def _configuration_args(self, exe, *args, **kwargs):
  129. return _configuration_args(
  130. self.pp_key(), self.get_param('postprocessor_args'), exe, *args, **kwargs)
  131. def _hook_progress(self, status, info_dict):
  132. if not self._progress_hooks:
  133. return
  134. status.update({
  135. 'info_dict': info_dict,
  136. 'postprocessor': self.pp_key(),
  137. })
  138. for ph in self._progress_hooks:
  139. ph(status)
  140. def add_progress_hook(self, ph):
  141. # See YoutubeDl.py (search for postprocessor_hooks) for a description of this interface
  142. self._progress_hooks.append(ph)
  143. def report_progress(self, s):
  144. s['_default_template'] = '%(postprocessor)s %(status)s' % s # noqa: UP031
  145. if not self._downloader:
  146. return
  147. progress_dict = s.copy()
  148. progress_dict.pop('info_dict')
  149. progress_dict = {'info': s['info_dict'], 'progress': progress_dict}
  150. progress_template = self.get_param('progress_template', {})
  151. tmpl = progress_template.get('postprocess')
  152. if tmpl:
  153. self._downloader.to_screen(
  154. self._downloader.evaluate_outtmpl(tmpl, progress_dict), quiet=False)
  155. self._downloader.to_console_title(self._downloader.evaluate_outtmpl(
  156. progress_template.get('postprocess-title') or 'yt-dlp %(progress._default_template)s',
  157. progress_dict))
  158. def _retry_download(self, err, count, retries):
  159. # While this is not an extractor, it behaves similar to one and
  160. # so obey extractor_retries and "--retry-sleep extractor"
  161. RetryManager.report_retry(err, count, retries, info=self.to_screen, warn=self.report_warning,
  162. sleep_func=self.get_param('retry_sleep_functions', {}).get('extractor'))
  163. def _download_json(self, url, *, expected_http_errors=(404,)):
  164. self.write_debug(f'{self.PP_NAME} query: {url}')
  165. for retry in RetryManager(self.get_param('extractor_retries', 3), self._retry_download):
  166. try:
  167. rsp = self._downloader.urlopen(Request(url))
  168. except network_exceptions as e:
  169. if isinstance(e, HTTPError) and e.status in expected_http_errors:
  170. return None
  171. retry.error = PostProcessingError(f'Unable to communicate with {self.PP_NAME} API: {e}')
  172. continue
  173. return json.loads(rsp.read().decode(rsp.headers.get_param('charset') or 'utf-8'))
  174. class AudioConversionError(PostProcessingError): # Deprecated
  175. pass