简体   繁体   English

使用用户在 Python 中插入的文件运行 Linux shell 脚本

[英]Run a Linux shell script using a file inserted by the user in Python

I am trying to run a ffprobe command using a pre-defined by the user input file in Python.我正在尝试使用 Python 中用户输入文件的预定义来运行 ffprobe 命令。 And then I will use the generated file by this command to report some parameters in a more organized view.然后我将使用此命令生成的文件以更有条理的视图报告一些参数。 My code is:我的代码是:

import subprocess
import json

cmd = "ffprobe -v quiet -print_format -show_format -show_streams /path/to/input/file.mp4 > output.json" 
subprocess.call(cmd.split())

with open('output.json') as json_data:
        data = json.load(json_data)
        json_data.close()
        d = float((data["streams"][0]["duration"]))
        t = (data["streams"][0]["time_base"])
        fps = [float(x) for x in t.split('/')]
        print "==========================General=========================="
        print "Name of the File: %s" %(data["format"]["filename"])
        print "Duration: %.2f minutes" %(d/60)
        print "Frame Rate: %d fps" %fps[1]
        print "Bitrate: %.2f Mbps" %(float(data["format"]["bit_rate"])/1000000)

I was thinking to use: input_file = ("Please enter the path to your input file: ") and then use the input_file in the ffprobe command on the second line of my code.我想使用: input_file = ("Please enter the path to your input file: ")然后在我的代码第二行的 ffprobe 命令中使用 input_file。 But I am not sure how I can do it within the quotes.但我不确定如何在引号内做到这一点。 Please also note that the file name should also include an extension like input.mp4.另请注意,文件名还应包含类似 input.mp4 的扩展名。

Shell redirection ( > ) will only work when shell=True is passed to subprocess.call() . Shell 重定向 ( > ) 仅在shell=True传递给subprocess.call()时才起作用。 Generally you should avoid doing that, specially if you take user input as part of the executed command, in which a case you need to make sure that the user input is properly escaped, eg by using shlex.qutoe() .通常你应该避免这样做,特别是如果你将用户输入作为执行命令的一部分,在这种情况下你需要确保用户输入被正确转义,例如使用shlex.qutoe()

Instead of using redirection in the shell you can open the file to write in python and pass it as stdout , or if you don't need the file you can use subprocess.check_output() instead of subprocess.call() :您可以打开文件以在 python 中写入并将其作为stdout传递,而不是在 shell 中使用重定向,或者如果您不需要该文件,您可以使用subprocess.check_output()而不是subprocess.call()

input_filename = raw_input("Please enter the path to your input file: ")
cmd = ['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams',
       input_filename]

returned_data = subprocess.check_output(cmd)
data = json.loads(returned_data.decode('utf-8'))    # assuming the returned data is utf-8 encoded

...

Or the same with using a file to write to:或者与使用文件写入相同:

input_filename = raw_input("Please enter the path to your input file: ")
cmd = ['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams',
       input_filename]

with open('output.json', 'wb') as outfile:
    subprocess.call(cmd, stdout=outfile)

with open('output.json', 'r') as infile:
    data = json.load(infile)

...

In this case the input filename doesn't need to be quoted because it is not interpreted by a shell.在这种情况下,输入文件名不需要被引用,因为它不被 shell 解释。

This should work:这应该有效:

#!/usr/bin/env python
import subprocess
import json

input_file = raw_input("Please enter the input file path: ")

returned_data = subprocess.check_output(['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', input_file])
data = json.loads(returned_data.decode('utf-8'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM