]> jfr.im git - yt-dlp.git/blob - yt_dlp/postprocessor/exec.py
[ExtractAudio] Handle outtmpl without ext (#6005)
[yt-dlp.git] / yt_dlp / postprocessor / exec.py
1 import subprocess
2
3 from .common import PostProcessor
4 from ..compat import compat_shlex_quote
5 from ..utils import PostProcessingError, encodeArgument, variadic
6
7
8 class ExecPP(PostProcessor):
9
10 def __init__(self, downloader, exec_cmd):
11 PostProcessor.__init__(self, downloader)
12 self.exec_cmd = variadic(exec_cmd)
13
14 def parse_cmd(self, cmd, info):
15 tmpl, tmpl_dict = self._downloader.prepare_outtmpl(cmd, info)
16 if tmpl_dict: # if there are no replacements, tmpl_dict = {}
17 return self._downloader.escape_outtmpl(tmpl) % tmpl_dict
18
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
26
27 def run(self, info):
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)
34 return [], info
35
36
37 # Deprecated
38 class 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')