]>
jfr.im git - yt-dlp.git/blob - youtube_dlc/postprocessor/common.py
5b777fad1d89f5fe53abdec0ec024d175a990b31
1 from __future__
import unicode_literals
5 from ..compat
import compat_str
12 class PostProcessor(object):
13 """Post Processor class.
15 PostProcessor objects can be added to downloaders with their
16 add_post_processor() method. When the downloader has finished a
17 successful download, it will take its internal chain of PostProcessors
18 and start calling the run() method on each one of them, first with
19 an initial argument and then with the returned value of the previous
22 The chain will be stopped if one of them ever returns None or the end
23 of the chain is reached.
25 PostProcessor objects follow a "mutual registration" process similar
26 to InfoExtractor objects.
28 Optionally PostProcessor can use a list of additional command-line arguments
29 with self._configuration_args.
34 def __init__(self
, downloader
=None):
35 self
._downloader
= downloader
36 self
.PP_NAME
= self
.pp_key()
40 name
= cls
.__name
__[:-2]
41 return compat_str(name
[6:]) if name
[:6].lower() == 'ffmpeg' else name
43 def to_screen(self
, text
, prefix
=True, *args
, **kwargs
):
44 tag
= '[%s] ' % self
.PP_NAME
if prefix
else ''
46 return self
._downloader
.to_screen('%s%s' % (tag
, text
), *args
, **kwargs
)
48 def report_warning(self
, text
, *args
, **kwargs
):
50 return self
._downloader
.report_warning(text
, *args
, **kwargs
)
52 def report_error(self
, text
, *args
, **kwargs
):
54 return self
._downloader
.report_error(text
, *args
, **kwargs
)
56 def write_debug(self
, text
, prefix
=True, *args
, **kwargs
):
57 tag
= '[debug] ' if prefix
else ''
58 if self
.get_param('verbose', False):
59 return self
._downloader
.to_screen('%s%s' % (tag
, text
), *args
, **kwargs
)
61 def get_param(self
, name
, default
=None, *args
, **kwargs
):
63 return self
._downloader
.params
.get(name
, default
, *args
, **kwargs
)
66 def set_downloader(self
, downloader
):
67 """Sets the downloader for this PP."""
68 self
._downloader
= downloader
70 def run(self
, information
):
71 """Run the PostProcessor.
73 The "information" argument is a dictionary like the ones
74 composed by InfoExtractors. The only difference is that this
75 one has an extra field called "filepath" that points to the
78 This method returns a tuple, the first element is a list of the files
79 that can be deleted, and the second of which is the updated
82 In addition, this method may raise a PostProcessingError
83 exception if post processing fails.
85 return [], information
# by default, keep file and do nothing
87 def try_utime(self
, path
, atime
, mtime
, errnote
='Cannot update utime of file'):
89 os
.utime(encodeFilename(path
), (atime
, mtime
))
91 self
.report_warning(errnote
)
93 def _configuration_args(self
, default
=[], exe
=None):
94 args
= self
.get_param('postprocessor_args', {})
95 pp_key
= self
.pp_key().lower()
97 if isinstance(args
, (list, tuple)): # for backward compatibility
98 return default
if pp_key
== 'sponskrub' else args
101 assert isinstance(args
, dict)
105 assert isinstance(exe
, compat_str
)
107 specific_args
= args
.get('%s+%s' % (pp_key
, exe
))
108 if specific_args
is not None:
109 assert isinstance(specific_args
, (list, tuple))
111 exe_args
= args
.get(exe
)
113 pp_args
= args
.get(pp_key
) if pp_key
!= exe
else None
114 if pp_args
is None and exe_args
is None:
115 default
= args
.get('default', default
)
116 assert isinstance(default
, (list, tuple))
121 elif exe_args
is None:
124 assert isinstance(pp_args
, (list, tuple))
125 assert isinstance(exe_args
, (list, tuple))
126 return pp_args
+ exe_args
129 class AudioConversionError(PostProcessingError
):