Release 2024.12.23
[yt-dlp.git] / yt_dlp / postprocessor / exec.py
blob1f0a0015ecb1a0e56e5334b2d9f55f403a3585b6
1 from .common import PostProcessor
2 from ..utils import Popen, PostProcessingError, shell_quote, variadic
5 class ExecPP(PostProcessor):
7 def __init__(self, downloader, exec_cmd):
8 PostProcessor.__init__(self, downloader)
9 self.exec_cmd = variadic(exec_cmd)
11 def parse_cmd(self, cmd, info):
12 tmpl, tmpl_dict = self._downloader.prepare_outtmpl(cmd, info)
13 if tmpl_dict: # if there are no replacements, tmpl_dict = {}
14 return self._downloader.escape_outtmpl(tmpl) % tmpl_dict
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 += ' {}'
21 cmd = cmd.replace('{}', shell_quote(filepath))
22 return cmd
24 def run(self, info):
25 for tmpl in self.exec_cmd:
26 cmd = self.parse_cmd(tmpl, info)
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}')
31 return [], info
34 # Deprecated
35 class 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')