]> jfr.im git - yt-dlp.git/blame - yt_dlp/postprocessor/exec.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / postprocessor / exec.py
CommitLineData
8d31fa3c 1from .common import PostProcessor
add96eb9 2from ..utils import Popen, PostProcessingError, shell_quote, variadic
a7cacbca 3
a7cacbca 4
ad3dc496 5class ExecPP(PostProcessor):
1b77b347 6
69b46b3d 7 def __init__(self, downloader, exec_cmd):
ad3dc496 8 PostProcessor.__init__(self, downloader)
c681cb5d 9 self.exec_cmd = variadic(exec_cmd)
a7cacbca 10
c6ce8154 11 def parse_cmd(self, cmd, info):
752cda38 12 tmpl, tmpl_dict = self._downloader.prepare_outtmpl(cmd, info)
13 if tmpl_dict: # if there are no replacements, tmpl_dict = {}
901130bb 14 return self._downloader.escape_outtmpl(tmpl) % tmpl_dict
752cda38 15
1e43a6f7 16 filepath = info.get('filepath', info.get('_filename'))
17 # If video, and no replacements are found, replace {} for backard compatibility
18 if filepath:
19 if '{}' not in cmd:
20 cmd += ' {}'
add96eb9 21 cmd = cmd.replace('{}', shell_quote(filepath))
1e43a6f7 22 return cmd
8d31fa3c 23
c6ce8154 24 def run(self, info):
c681cb5d 25 for tmpl in self.exec_cmd:
26 cmd = self.parse_cmd(tmpl, info)
de015e93
SS
27 self.to_screen(f'Executing command: {cmd}')
28 _, _, return_code = Popen.run(cmd, shell=True)
29 if return_code != 0:
30 raise PostProcessingError(f'Command returned error code {return_code}')
9de3ea31 31 return [], info
ad3dc496 32
33
ee8dd27a 34# Deprecated
35class ExecAfterDownloadPP(ExecPP):
36 def __init__(self, *args, **kwargs):
37 super().__init__(*args, **kwargs)
38 self.deprecation_warning(
39 'yt_dlp.postprocessor.ExecAfterDownloadPP is deprecated '
40 'and may be removed in a future version. Use yt_dlp.postprocessor.ExecPP instead')