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