【问题标题】:Bash process substitution in Python with Popen使用 Popen 在 Python 中替换 Bash 进程
【发布时间】:2014-12-02 14:18:27
【问题描述】:

我正在尝试通过从 python 子进程库调用 ffmpeg 来创建循环视频文件。这是给我带来问题的部分:

import subprocess as sp
sp.Popen(['ffmpeg', '-f', 'concat', '-i', "<(for f in ~/Desktop/*.mp4; do echo \"file \'$f\'\"; done)", "-c", "copy", "~/Desktop/sample3.mp4"])

使用上面的代码,我收到以下错误:

<(for f in /home/delta/Desktop/*.mp4; do echo "file '$f'"; done): No such file or directory

我确实找到了类似措辞的问题here。但我不确定该解决方案如何适用于解决我的问题。

编辑: 感谢大家的帮助!按照 cmets 中的建议并在其他地方查找,我最终将代码更改为:

sp.Popen("ffmpeg -f concat -i <(for f in ~/Desktop/*.mp4; do echo \"file \'$f\'\"; done) -c copy ~/Desktop/sample3.mp4", shell=True, executable="/bin/bash")

——效果很好。

【问题讨论】:

  • 您正在运行ffmpeg,但重定向是shell 的一项功能。使用shell=True 参数强制Popen 通过shell 传递它。还要考虑不传递命令和参数数组,而是传递单个字符串。阅读更多here
  • 如果使用shell=True,参数需要作为字符串传递,而不是列表。
  • +1 对 Amadan 和 dano 所说的话。您可能还需要明确指出您想要/bin/bash,而不是默认的/bin/sh Popen
  • 请参阅stackoverflow.com/a/24560058/258523 上的讨论,了解为什么 Popenshell=True 的单个字符串参数。

标签: python bash ffmpeg


【解决方案1】:

按照 cmets 中的建议并查看其他地方,我最终将代码更改为:

sp.Popen("ffmpeg -f concat -i <(for f in ~/Desktop/*.mp4; do echo \"file \'$f\'\"; done) -c copy ~/Desktop/sample3.mp4", shell=True, executable="/bin/bash")

——效果很好。 – 摩尔

【讨论】:

  • 一个简短的解释为什么bin/bash有助于处理替换&lt;()会很好
  • @Fibo Kowalsky - 只是因为process substitutionbash 的一个特殊功能,这是其他shell 所没有的。
猜你喜欢
  • 2013-02-26
  • 2012-11-20
  • 2018-12-05
  • 1970-01-01
  • 2012-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多