]> jfr.im git - yt-dlp.git/blob - youtube_dlc/postprocessor/common.py
[postprocessor] fix write_debug when no _downloader
[yt-dlp.git] / youtube_dlc / postprocessor / common.py
1 from __future__ import unicode_literals
2
3 import os
4
5 from ..compat import compat_str
6 from ..utils import (
7 cli_configuration_args,
8 encodeFilename,
9 PostProcessingError,
10 )
11
12
13 class PostProcessor(object):
14 """Post Processor class.
15
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
21 PostProcessor.
22
23 The chain will be stopped if one of them ever returns None or the end
24 of the chain is reached.
25
26 PostProcessor objects follow a "mutual registration" process similar
27 to InfoExtractor objects.
28
29 Optionally PostProcessor can use a list of additional command-line arguments
30 with self._configuration_args.
31 """
32
33 _downloader = None
34
35 def __init__(self, downloader=None):
36 self._downloader = downloader
37 self.PP_NAME = self.pp_key()
38
39 @classmethod
40 def pp_key(cls):
41 name = cls.__name__[:-2]
42 return compat_str(name[6:]) if name[:6].lower() == 'ffmpeg' else name
43
44 def to_screen(self, text, prefix=True, *args, **kwargs):
45 tag = '[%s] ' % self.PP_NAME if prefix else ''
46 if self._downloader:
47 return self._downloader.to_screen('%s%s' % (tag, text), *args, **kwargs)
48
49 def report_warning(self, text, *args, **kwargs):
50 if self._downloader:
51 return self._downloader.report_warning(text, *args, **kwargs)
52
53 def report_error(self, text, *args, **kwargs):
54 if self._downloader:
55 return self._downloader.report_error(text, *args, **kwargs)
56
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)
61
62 def get_param(self, name, default=None, *args, **kwargs):
63 if self._downloader:
64 return self._downloader.params.get(name, default, *args, **kwargs)
65 return default
66
67 def set_downloader(self, downloader):
68 """Sets the downloader for this PP."""
69 self._downloader = downloader
70
71 def run(self, information):
72 """Run the PostProcessor.
73
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
77 downloaded file.
78
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
81 information.
82
83 In addition, this method may raise a PostProcessingError
84 exception if post processing fails.
85 """
86 return [], information # by default, keep file and do nothing
87
88 def try_utime(self, path, atime, mtime, errnote='Cannot update utime of file'):
89 try:
90 os.utime(encodeFilename(path), (atime, mtime))
91 except Exception:
92 self.report_warning(errnote)
93
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
99
100
101 class AudioConversionError(PostProcessingError):
102 pass