1

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.

wasneeplus
  • 13
  • 3

1 Answers1

2

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

Dmytro Bugayev
  • 606
  • 9
  • 13
  • Thank you for your help, that seems to have resolved the issue! It is bit of a shame that the ffprobe location has to be hardcoded in. It doesn't help the code's portability. But I'll worry about that later. Have a nice day! – wasneeplus Apr 07 '21 at 21:46
  • adding ffmpeg's bin directory to the system path will allow you to just specify `ffprobe` in the script (just tested it), but again, that path will be different from one computer to another. – Dmytro Bugayev Apr 07 '21 at 22:30