5

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]
Aman Agrawal
  • 193
  • 1
  • 2
  • 14

2 Answers2

8

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)
Community
  • 1
  • 1
Artagel
  • 390
  • 1
  • 12
  • thx very much @artagel – Aman Agrawal Apr 01 '17 at 08:19
  • i used this following code subprocess32.Popen(['python','m2p21_contacts.py'], close_fds=False) in my file to create subprocess to the file 'm2p21_contacts.py' but it is waiting until complete execution of 'm2p21_contacts.py' – user8119231 Feb 01 '18 at 15:26
  • I think you should ask a new question. But..if you are calling another python script, I can't imagine why you'd want to subprocess it instead of just importing it and calling your function. **Edit: unless its a different python version :) – Artagel Feb 02 '18 at 15:52
0

For me, real error is that /usr/local/bin/python is not defined, running sudo ln -s /usr/bin/python /usr/local/bin/python fix the problem.

Romain Norberg
  • 947
  • 6
  • 6