]> jfr.im git - yt-dlp.git/blob - youtube_dlc/postprocessor/common.py
5b777fad1d89f5fe53abdec0ec024d175a990b31
[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 PostProcessingError,
8 encodeFilename,
9 )
10
11
12 class PostProcessor(object):
13 """Post Processor class.
14
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
20 PostProcessor.
21
22 The chain will be stopped if one of them ever returns None or the end
23 of the chain is reached.
24
25 PostProcessor objects follow a "mutual registration" process similar
26 to InfoExtractor objects.
27
28 Optionally PostProcessor can use a list of additional command-line arguments
29 with self._configuration_args.
30 """
31
32 _downloader = None
33
34 def __init__(self, downloader=None):
35 self._downloader = downloader
36 self.PP_NAME = self.pp_key()
37
38 @classmethod
39 def pp_key(cls):
40 name = cls.__name__[:-2]
41 return compat_str(name[6:]) if name[:6].lower() == 'ffmpeg' else name
42
43 def to_screen(self, text, prefix=True, *args, **kwargs):
44 tag = '[%s] ' % self.PP_NAME if prefix else ''
45 if self._downloader:
46 return self._downloader.to_screen('%s%s' % (tag, text), *args, **kwargs)
47
48 def report_warning(self, text, *args, **kwargs):
49 if self._downloader:
50 return self._downloader.report_warning(text, *args, **kwargs)
51
52 def report_error(self, text, *args, **kwargs):
53 if self._downloader:
54 return self._downloader.report_error(text, *args, **kwargs)
55
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)
60
61 def get_param(self, name, default=None, *args, **kwargs):
62 if self._downloader:
63 return self._downloader.params.get(name, default, *args, **kwargs)
64 return default
65
66 def set_downloader(self, downloader):
67 """Sets the downloader for this PP."""
68 self._downloader = downloader
69
70 def run(self, information):
71 """Run the PostProcessor.
72
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
76 downloaded file.
77
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
80 information.
81
82 In addition, this method may raise a PostProcessingError
83 exception if post processing fails.
84 """
85 return [], information # by default, keep file and do nothing
86
87 def try_utime(self, path, atime, mtime, errnote='Cannot update utime of file'):
88 try:
89 os.utime(encodeFilename(path), (atime, mtime))
90 except Exception:
91 self.report_warning(errnote)
92
93 def _configuration_args(self, default=[], exe=None):
94 args = self.get_param('postprocessor_args', {})
95 pp_key = self.pp_key().lower()
96
97 if isinstance(args, (list, tuple)): # for backward compatibility
98 return default if pp_key == 'sponskrub' else args
99 if args is None:
100 return default
101 assert isinstance(args, dict)
102
103 exe_args = None
104 if exe is not None:
105 assert isinstance(exe, compat_str)
106 exe = exe.lower()
107 specific_args = args.get('%s+%s' % (pp_key, exe))
108 if specific_args is not None:
109 assert isinstance(specific_args, (list, tuple))
110 return specific_args
111 exe_args = args.get(exe)
112
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))
117 return default
118
119 if pp_args is None:
120 pp_args = []
121 elif exe_args is None:
122 exe_args = []
123
124 assert isinstance(pp_args, (list, tuple))
125 assert isinstance(exe_args, (list, tuple))
126 return pp_args + exe_args
127
128
129 class AudioConversionError(PostProcessingError):
130 pass