]> jfr.im git - yt-dlp.git/blob - yt_dlp/postprocessor/exec.py
[ie/patreon] Extract multiple embeds (#9850)
[yt-dlp.git] / yt_dlp / postprocessor / exec.py
1 from .common import PostProcessor
2 from ..compat import compat_shlex_quote
3 from ..utils import Popen, PostProcessingError, variadic
4
5
6 class ExecPP(PostProcessor):
7
8 def __init__(self, downloader, exec_cmd):
9 PostProcessor.__init__(self, downloader)
10 self.exec_cmd = variadic(exec_cmd)
11
12 def parse_cmd(self, cmd, info):
13 tmpl, tmpl_dict = self._downloader.prepare_outtmpl(cmd, info)
14 if tmpl_dict: # if there are no replacements, tmpl_dict = {}
15 return self._downloader.escape_outtmpl(tmpl) % tmpl_dict
16
17 filepath = info.get('filepath', info.get('_filename'))
18 # If video, and no replacements are found, replace {} for backard compatibility
19 if filepath:
20 if '{}' not in cmd:
21 cmd += ' {}'
22 cmd = cmd.replace('{}', compat_shlex_quote(filepath))
23 return cmd
24
25 def run(self, info):
26 for tmpl in self.exec_cmd:
27 cmd = self.parse_cmd(tmpl, info)
28 self.to_screen(f'Executing command: {cmd}')
29 _, _, return_code = Popen.run(cmd, shell=True)
30 if return_code != 0:
31 raise PostProcessingError(f'Command returned error code {return_code}')
32 return [], info
33
34
35 # Deprecated
36 class ExecAfterDownloadPP(ExecPP):
37 def __init__(self, *args, **kwargs):
38 super().__init__(*args, **kwargs)
39 self.deprecation_warning(
40 'yt_dlp.postprocessor.ExecAfterDownloadPP is deprecated '
41 'and may be removed in a future version. Use yt_dlp.postprocessor.ExecPP instead')