【问题标题】:subprocess.check_output fails with CalledProcessError but error is empty string. Command works in terminalsubprocess.check_output 因 CalledProcessError 而失败,但错误为空字符串。命令在终端中工作
【发布时间】:2022-08-12 00:54:49
【问题描述】:

我想运行命令ffprobe -i test.m4a -show_entries format=duration -v quiet -of csv=\"p=0\"。它在终端中工作并返回输出代码 0,但使用子进程运行它,即

subprocess.check_output([\'ffprobe\', \'-i\', \'test.m4a\', \'-show_entries\', \'format=duration\', \'-v\', \'quiet\', \'-of\', \'csv=\"p=0\"\'])

引发CalledProcessError - {Command} returned non-zero exit status 1.。我尝试在 try-except 循环中运行此命令并打印错误详细信息,但它只是输出为空字节字符串b\'\'

    标签: python ffmpeg subprocess ffprobe


    【解决方案1】:

    我建议使用 subprocess.run 而不是 check_output。

    subprocess.run(command, stdout=output, encoding="utf-8")
    

    Command = 是包含您想要的命令的变量,无需使用逗号分隔。

    stdout = Output = 表示输出应记录在您可以预先创建的名为 (Output) 的文件中。

    encoding = 只是意味着确保将其从字节编码为文本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-06
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      相关资源
      最近更新 更多