1 from __future__
import unicode_literals
6 from ..compat
import compat_str
15 class PostProcessorMetaClass(type):
17 def run_wrapper(func
):
18 @functools.wraps(func
)
19 def run(self
, info
, *args
, **kwargs
):
20 info_copy
= self
._copy
_infodict
(info
)
21 self
._hook
_progress
({'status': 'started'}
, info_copy
)
22 ret
= func(self
, info
, *args
, **kwargs
)
25 self
._hook
_progress
({'status': 'finished'}
, info_copy
)
29 def __new__(cls
, name
, bases
, attrs
):
31 attrs
['run'] = cls
.run_wrapper(attrs
['run'])
32 return type.__new
__(cls
, name
, bases
, attrs
)
35 class PostProcessor(metaclass
=PostProcessorMetaClass
):
36 """Post Processor class.
38 PostProcessor objects can be added to downloaders with their
39 add_post_processor() method. When the downloader has finished a
40 successful download, it will take its internal chain of PostProcessors
41 and start calling the run() method on each one of them, first with
42 an initial argument and then with the returned value of the previous
45 The chain will be stopped if one of them ever returns None or the end
46 of the chain is reached.
48 PostProcessor objects follow a "mutual registration" process similar
49 to InfoExtractor objects.
51 Optionally PostProcessor can use a list of additional command-line arguments
52 with self._configuration_args.
57 def __init__(self
, downloader
=None):
58 self
._progress
_hooks
= []
59 self
.add_progress_hook(self
.report_progress
)
60 self
.set_downloader(downloader
)
61 self
.PP_NAME
= self
.pp_key()
65 name
= cls
.__name
__[:-2]
66 return compat_str(name
[6:]) if name
[:6].lower() == 'ffmpeg' else name
68 def to_screen(self
, text
, prefix
=True, *args
, **kwargs
):
69 tag
= '[%s] ' % self
.PP_NAME
if prefix
else ''
71 return self
._downloader
.to_screen('%s%s' % (tag
, text
), *args
, **kwargs
)
73 def report_warning(self
, text
, *args
, **kwargs
):
75 return self
._downloader
.report_warning(text
, *args
, **kwargs
)
77 def deprecation_warning(self
, text
):
79 return self
._downloader
.deprecation_warning(text
)
80 write_string(f
'DeprecationWarning: {text}')
82 def report_error(self
, text
, *args
, **kwargs
):
83 # Exists only for compatibility. Do not use
85 return self
._downloader
.report_error(text
, *args
, **kwargs
)
87 def write_debug(self
, text
, *args
, **kwargs
):
89 return self
._downloader
.write_debug(text
, *args
, **kwargs
)
91 def get_param(self
, name
, default
=None, *args
, **kwargs
):
93 return self
._downloader
.params
.get(name
, default
, *args
, **kwargs
)
96 def set_downloader(self
, downloader
):
97 """Sets the downloader for this PP."""
98 self
._downloader
= downloader
99 for ph
in getattr(downloader
, '_postprocessor_hooks', []):
100 self
.add_progress_hook(ph
)
102 def _copy_infodict(self
, info_dict
):
103 return getattr(self
._downloader
, '_copy_infodict', dict)(info_dict
)
106 def _restrict_to(*, video
=True, audio
=True, images
=True):
107 allowed
= {'video': video, 'audio': audio, 'images': images}
110 @functools.wraps(func
)
111 def wrapper(self
, info
):
113 'video' if info
.get('vcodec') != 'none'
114 else 'audio' if info
.get('acodec') != 'none'
116 if allowed
[format_type
]:
117 return func(self
, info
)
119 self
.to_screen('Skipping %s' % format_type
)
124 def run(self
, information
):
125 """Run the PostProcessor.
127 The "information" argument is a dictionary like the ones
128 composed by InfoExtractors. The only difference is that this
129 one has an extra field called "filepath" that points to the
132 This method returns a tuple, the first element is a list of the files
133 that can be deleted, and the second of which is the updated
136 In addition, this method may raise a PostProcessingError
137 exception if post processing fails.
139 return [], information
# by default, keep file and do nothing
141 def try_utime(self
, path
, atime
, mtime
, errnote
='Cannot update utime of file'):
143 os
.utime(encodeFilename(path
), (atime
, mtime
))
145 self
.report_warning(errnote
)
147 def _configuration_args(self
, exe
, *args
, **kwargs
):
148 return _configuration_args(
149 self
.pp_key(), self
.get_param('postprocessor_args'), exe
, *args
, **kwargs
)
151 def _hook_progress(self
, status
, info_dict
):
152 if not self
._progress
_hooks
:
155 'info_dict': info_dict
,
156 'postprocessor': self
.pp_key(),
158 for ph
in self
._progress
_hooks
:
161 def add_progress_hook(self
, ph
):
162 # See YoutubeDl.py (search for postprocessor_hooks) for a description of this interface
163 self
._progress
_hooks
.append(ph
)
165 def report_progress(self
, s
):
166 s
['_default_template'] = '%(postprocessor)s %(status)s' % s
168 progress_dict
= s
.copy()
169 progress_dict
.pop('info_dict')
170 progress_dict
= {'info': s['info_dict'], 'progress': progress_dict}
172 progress_template
= self
.get_param('progress_template', {})
173 tmpl
= progress_template
.get('postprocess')
175 self
._downloader
.to_stdout(self
._downloader
.evaluate_outtmpl(tmpl
, progress_dict
))
177 self
._downloader
.to_console_title(self
._downloader
.evaluate_outtmpl(
178 progress_template
.get('postprocess-title') or 'yt-dlp %(progress._default_template)s',
182 class AudioConversionError(PostProcessingError
):