使用双引号将命令传递给python中的subprocess.call

时间:2018-06-07 15:45:34

标签: python shell ffmpeg subprocess

我想使用包含双引号的python将命令传递给运行ffmpeg的Linux机器。那是我的剧本:

drawtext = "drawtext=\"fontfile=DejaVuSans: text='Random Name': fontcolor=white: fontsize=24: box=1: boxcolor=black@0.5: boxborderw=5: x=20: y=20\""
subprocess.call(["ffmpeg", "-v", "error", "-i", input.mp4, "-vf", drawtext, output.mp4])

如果我打印drawtext变量,这是输出:

drawtext="fontfile=DejaVuSans: text='Reference Image': fontcolor=white: fontsize=24: box=1: boxcolor=black@0.5: boxborderw=5: x=20: y=20"

这是我在ffmpeg中的错误:

[Parsed_drawtext_0 @ 0x564ad79f99c0] [Eval @ 0x7ffd41131810] Invalid chars '"' at the end of expression '20"'
[Parsed_drawtext_0 @ 0x564ad79f99c0] Failed to configure input pad on Parsed_drawtext_0
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0

如果我在Linux shell中执行以下命令,它可以正常工作:

$ ffmpeg -ss 10 -i input.mp4 -vf drawtext="fontfile=DejaVuSans: text='Random Name': fontcolor=white: fontsize=24: box=1: boxcolor=black@0.5: boxborderw=5: x=20: y=20" output.mp4

1 个答案:

答案 0 :(得分:1)

执行的一种方法是使用如下内容创建shell脚本:

#!/bin/bash
ffmpeg -ss 10 -i input.mp4 -vf drawtext="fontfile=DejaVuSans: text='Random Name': fontcolor=white: fontsize=24: box=1: boxcolor=black@0.5: boxborderw=5: x=20: y=20" output.mp4

然后您可以将其保存为script.sh,然后您可以按如下方式调用脚本:

import subprocess; subprocess.call(`./script.sh`; shell=True)