]>
jfr.im git - yt-dlp.git/blob - youtube_dlc/postprocessor/common.py
1 from __future__
import unicode_literals
5 from ..compat
import compat_str
7 cli_configuration_args
,
13 class PostProcessor(object):
14 """Post Processor class.
16 PostProcessor objects can be added to downloaders with their
17 add_post_processor() method. When the downloader has finished a
18 successful download, it will take its internal chain of PostProcessors
19 and start calling the run() method on each one of them, first with
20 an initial argument and then with the returned value of the previous
23 The chain will be stopped if one of them ever returns None or the end
24 of the chain is reached.
26 PostProcessor objects follow a "mutual registration" process similar
27 to InfoExtractor objects.
29 Optionally PostProcessor can use a list of additional command-line arguments
30 with self._configuration_args.
35 def __init__(self
, downloader
=None):
36 self
._downloader
= downloader
37 self
.PP_NAME
= self
.pp_key()
41 name
= cls
.__name
__[:-2]
42 return compat_str(name
[6:]) if name
[:6].lower() == 'ffmpeg' else name
44 def to_screen(self
, text
, prefix
=True, *args
, **kwargs
):
45 tag
= '[%s] ' % self
.PP_NAME
if prefix
else ''
47 return self
._downloader
.to_screen('%s%s' % (tag
, text
), *args
, **kwargs
)
49 def report_warning(self
, text
, *args
, **kwargs
):
51 return self
._downloader
.report_warning(text
, *args
, **kwargs
)
53 def report_error(self
, text
, *args
, **kwargs
):
55 return self
._downloader
.report_error(text
, *args
, **kwargs
)
57 def write_debug(self
, text
, prefix
=True, *args
, **kwargs
):
58 tag
= '[debug] ' if prefix
else ''
59 if self
.get_param('verbose', False) and self
._downloader
:
60 return self
._downloader
.to_screen('%s%s' % (tag
, text
), *args
, **kwargs
)
62 def get_param(self
, name
, default
=None, *args
, **kwargs
):
64 return self
._downloader
.params
.get(name
, default
, *args
, **kwargs
)
67 def set_downloader(self
, downloader
):
68 """Sets the downloader for this PP."""
69 self
._downloader
= downloader
71 def run(self
, information
):
72 """Run the PostProcessor.
74 The "information" argument is a dictionary like the ones
75 composed by InfoExtractors. The only difference is that this
76 one has an extra field called "filepath" that points to the
79 This method returns a tuple, the first element is a list of the files
80 that can be deleted, and the second of which is the updated
83 In addition, this method may raise a PostProcessingError
84 exception if post processing fails.
86 return [], information
# by default, keep file and do nothing
88 def try_utime(self
, path
, atime
, mtime
, errnote
='Cannot update utime of file'):
90 os
.utime(encodeFilename(path
), (atime
, mtime
))
92 self
.report_warning(errnote
)
94 def _configuration_args(self
, default
=[], exe
=None):
95 key
= self
.pp_key().lower()
96 args
, is_compat
= cli_configuration_args(
97 self
._downloader
.params
, 'postprocessor_args', key
, default
, exe
)
98 return args
if not is_compat
or key
!= 'sponskrub' else default
101 class AudioConversionError(PostProcessingError
):