在python中的子进程命令行中没有反斜杠的引号

时间:2017-10-20 17:43:46

标签: 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-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)

3 个答案:

答案 0 :(得分:1)

在shell中,参数:

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

完全相同

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

在shell自己处理过程中会删除引号。它们是传递给ffmpeg的命令的一部分,而infile = open("myFile.txt" , "r") outfile = open("x_myFile.txt" , "w") userBase = int(inputBase()) for line in infile: for ch in line: if ch.isalpha() or ch.isdigit(): value = int(ord(ch)) remainders = list() while value>0: remainders.append(value % userBase) value//=userBase remainders = remainders[::-1] outfile.write(*remainders, sep='') else: outfile.write("..") infile.close() outfile.close() 并不期望或理解它们。因为您直接告诉Python子进程模块调用文字参数向量,所以没有涉及shell,所以shell语法不应该存在。

答案 1 :(得分:0)

经过更多的摆弄,我发现了一个适用于这种情况的解决方案,但在所有情况下都可能无效。如果我使用双引号作为外引号并使用单引号作为内引号,则子进程的输出在同一位置使用单引号而不使用反斜杠。这对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 -

答案 2 :(得分:0)

这也与ffmpeg AV过滤器链语法有关。您需要运行xxxx -filter_complex "psnr='stats.txt'" xxxx之类的命令。为此,您应确保封装过滤器链的双引号到达内部。子进程需要一个平面列表作为第一个参数,其中命令是第一个参数。所以['ffmpeg', '-i', "t1.mp4", "-filter_compelx", '"psnr=\'stats.txt\'"', .... and so on ]

相关问题