Reputation: 13
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:
I would be most grateful if someone could point me in the right direction. Thank you in advance.
Upvotes: 1
Views: 522
Reputation: 676
you weren't far off. After getting an ffmpeg windows build from here: https://github.com/BtbN/FFmpeg-Builds/releases I was able to get your code to work using absolute paths for both the ffprobe.exe as well as the mp4 file location as follows (actual paths altered in the code below):
import subprocess
def get_length(filename):
result = subprocess.run(["C:\\...your ffmpeg here...\\FFMPEG\\bin\\ffprobe.exe", "-v", "error", "-show_entries",
"format=duration", "-of",
"default=noprint_wrappers=1:nokey=1", filename],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return float(result.stdout)
print(get_length('C:\\...full file path...\\test.mp4'))
program correctly prints out the clip length
Upvotes: 2