本文介绍了OSError: 在使用 subprocess.Popen 时没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取视频剪辑的持续时间.但是无法获取文件.这是我的代码:

I am trying to get the duration of a video clip. But it is unable to get the file.Here is my code:

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]

推荐答案

你不能在不添加 shell=True 的情况下将 subprocess.Popen 作为字符串运行.

You can't run subprocess.Popen as a string like that without add shell=True.

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)

如果将命令拆分为参数列表,则可以使用不带 shell=True 的方法.一般推荐非shell方式:何时使用Shell=适用于 Python 子进程模块

If you split the command into a list of args, then you can use the method you used without shell=True. The non-shell method is generally recommended: 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)

这篇关于OSError: 在使用 subprocess.Popen 时没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:49