Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSError: no such file or directory on using subprocess.Popen [duplicate]

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)

This is the error I am getting:

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]
like image 561
Aman Agrawal Avatar asked Mar 31 '17 14:03

Aman Agrawal


1 Answers

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)

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)
like image 125
Artagel Avatar answered Oct 13 '22 12:10

Artagel