exec.py 1.5 KB

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