As part of a video analysis script I wanted to find the duration of a video file. For this I found the script offered in the first answer to this question: How to get the duration of a video in Python?
import subprocess
def get_length(filename):
result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
"format=duration", "-of",
"default=noprint_wrappers=1:nokey=1", filename],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return float(result.stdout)
This code works fine when my friend runs it in a Jupyter server environment, but when I try it on my laptop the trouble starts.
When I imput the following filename into the function:
filename = "C:\\Users\\benja\\OneDrive - De Haagse Hogeschool\\Onderzoeken 3\\8V.mp4"
I get the following error:
Traceback (most recent call last):
File "c:/Users/benja/OneDrive - De Haagse Hogeschool/Onderzoeken 3/python_script.py", line 9, in <module>
num_of_frames = math.floor((pf.get_length(filename) - 1)) * 30
File "c:\Users\benja\OneDrive - De Haagse Hogeschool\Onderzoeken 3\python_funcs.py", line 21, in get_length
stderr=subprocess.STDOUT)
File "C:\Users\benja\Anaconda3\lib\subprocess.py", line 466, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\benja\Anaconda3\lib\subprocess.py", line 769, in __init__
restore_signals, start_new_session)
File "C:\Users\benja\Anaconda3\lib\subprocess.py", line 1172, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Het systeem kan het opgegeven bestand niet vinden
I do realise that my problem is almost identical to that of several other questions on here. However, their solutions don't seem to work for me. I have tried to:
- Add the location of ffmpeg-win64-4.2.2.exe to the Path system variable.
- Add the location of python.exe to the ComSpec system variable.
- Put the videofile in the same directory as the script.
I would be most grateful if someone could point me in the right direction. Thank you in advance.