python子流程命令行中不带反斜杠的引号

python子流程命令行中不带反斜杠的引号,python,ffmpeg,subprocess,Python,Ffmpeg,Subprocess,我正在尝试使用python中的ffmpeg。我需要执行的命令是: ffmpeg -i test_file-1kB.mp4 -i test_file.mp4 -filter_complex psnr="stats_file=test_file.mp4-1kB.psnr" -f null - 但是,传递给子流程的输出看起来像是用反斜杠转义双引号,如下所示: In[1]: print(subprocess.list2cmdline(psnr_args)) ffmpeg -i test_file-1k

我正在尝试使用python中的ffmpeg。我需要执行的命令是:

ffmpeg -i test_file-1kB.mp4 -i test_file.mp4 -filter_complex psnr="stats_file=test_file.mp4-1kB.psnr" -f null -
但是,传递给子流程的输出看起来像是用反斜杠转义双引号,如下所示:

In[1]: print(subprocess.list2cmdline(psnr_args))
ffmpeg -i test_file-1kB.mp4 -i test_file.mp4 -filter_complex psnr=\"stats_file=test_file.mp4-1kB.psnr\" -f null -
为了使用子流程,我将命令行参数一次生成一个列表,然后将该列表传递给子流程

    psnr_args = []
    psnr_args.append("ffmpeg")

    #add first input, the encoded video
    psnr_args.append("-i")
    psnr_args.append(full_output_file_name)

    #add second input, the original video
    psnr_args.append("-i")
    psnr_args.append(video_file)

    #Setup the psnr log file
    psnr_args.append("-filter_complex")
    psnr_args.append('psnr="stats_file=%s.psnr"' % vstats_abs_filename )

    #Output the video to null
    psnr_args.append("-f")
    psnr_args.append("null")
    psnr_args.append("-")
    print(subprocess.list2cmdline(psnr_args))
    run_info_psnr = subprocess.run(psnr_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

在做了更多的修改之后,我找到了一个在这种情况下有效的解决方案,但可能并非在所有情况下都有效。如果我使用双引号作为外引号,使用单引号作为内引号,则子流程的输出将在同一位置使用单引号,而不使用反斜杠。这对于ffmpeg是可以接受的。然而,对于双引号是唯一解决方案的其他人来说,这并不是一个解决方案

psnr_args.append("psnr='stats_file=%s.psnr'" % vstats_abs_filename )
子流程的输出如下所示:

In[1]: print(subprocess.list2cmdline(psnr_args))
ffmpeg -i test_file-1kB.mp4 -i test_file.mp4 -filter_complex psnr='stats_file=test_file.mp4-1kB.psnr' -f null -

在shell中,参数:

psnr="stats_file=test_file.mp4-1kB.psnr"
与完全相同:

psnr=stats_file=test_file.mp4-1kB.psnr

在shell自己的处理过程中删除引号。它们不是传递给
ffmpeg
的命令的一部分,该命令不期望也不理解它们。因为您直接告诉Python子流程模块调用文字参数向量,所以不涉及shell,所以shell语法不应该存在。

这也与ffmpeg AV过滤器链语法有关。您需要运行命令,如
xxxx-filter\u complex“psnr='stats.txt'”xxxx
。要做到这一点,您应该确保封装过滤器链的双引号到达内部。subproces需要一个平面列表作为第一个参数,其中命令是第一个条目。因此,
['ffmpeg'、'-i'、“t1.mp4”、“-filter\u compelx”、“-psnr=\'stats.txt\'”等等]

这些引号是对shell的指令,而不是对ffmpeg的指令。如果没有shell,就不应该使用文字引号。请不要使用它们。
list2cmdline
是一个Windows ism。如果您想以在Unix系列系统上有意义的方式将列表转换为命令行,请在Python 2中使用
'.join(psnr_args中的pipes.quote(x)代表x)
,或者在Python 3中使用
shlex.quote
替代
pipes.quote
。简单的引号在windows中没有特别的意义。@Jean Françoisfare,(1)对。(2) “那是针对我的吗?”查理·达菲:不是。我知道你知道:)