OSError:使用subprocess.Popen时没有这样的文件或目录

时间:2017-03-31 14:56:16

标签: python subprocess

我正在尝试获取视频片段的持续时间。但它无法获取文件。 这是我的代码:

import subprocess
import os
def getLength(input_video):
    result = subprocess.Popen('ffprobe -i input_video -show_entries format=duration -v quiet -of csv="p=0"', stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    output = result.communicate()
    return output[0]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
x = (os.path.join(BASE_DIR,'uploads/video.mkv'))
getLength(x)

这是我得到的错误:

Traceback (most recent call last):
  File "/home/aman/Desktop/stream/src/stream/uploads/sadf.py", line 9, in <module>
    getLength(x)
  File "/home/aman/Desktop/stream/src/stream/uploads/sadf.py", line 4, in getLength
    result = subprocess.Popen('ffprobe -i input_video -show_entries format=duration -v quiet -of csv="p=0"', stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
  File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
[Finished in 0.1s with exit code 1]
[shell_cmd: "python" -u "/home/aman/Desktop/stream/src/stream/uploads/sadf.py"]
[dir: /home/aman/Desktop/stream/src/stream/uploads]
[path: /home/aman/bin:/home/aman/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]

2 个答案:

答案 0 :(得分:7)

如果没有添加shell = True,则无法将subprocess.Popen作为字符串运行。

result = subprocess.Popen('ffprobe -i input_video -show_entries format=duration -v quiet -of csv="p=0"', stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True)

如果将命令拆分为args列表,则可以使用不使用shell = True的方法。通常建议使用非shell方法:When to use Shell=True for Python subprocess module

result = subprocess.Popen(['ffprobe', '-i', 'input_video', '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv="p=0"'], stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

答案 1 :(得分:0)

对我来说,真正的错误是未定义/usr/local/bin/python,运行sudo ln -s /usr/bin/python /usr/local/bin/python可以解决问题。