Mitchell Faas
Mitchell Faas

Reputation: 440

How to solve file path error while using ffprobe to find length of video file in python?

So, I'm trying to find the length of a video file by the methods discussed here: How to get the duration of a video in Python?, Using ffmpeg to obtain video durations in python. But in doing so I run in to a problem which I haven't been able to solve: I get

FileNotFoundError:[WinError 2] The system cannot find the file specified

after trying a bunch of troubleshooting steps I've started running in IPython and in cmd seperately to see where things might break down. Using a stripped down version of this code in IPython gives me

In [11]: subprocess.Popen([ffmpeg_path+'ffprobe'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Out[11]: <subprocess.Popen at (...)>

which seems to be fine, as is CMD at this point. So to adding slight complexity:

In [17]: subprocess.Popen([ffmpeg_path+'ffprobe -i "F:/tst.mp4"'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-17-4e8b2cad7702> in <module>()
----> 1 subprocess.Popen([ffmpeg_path+'ffprobe -i "F:/tst.mp4"'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

C:\Python\Anaconda3\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
    705                                 c2pread, c2pwrite,
    706                                 errread, errwrite,
--> 707                                 restore_signals, start_new_session)
    708         except:
    709             # Cleanup if the child failed starting.

C:\Python\Anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
    988                                          env,
    989                                          cwd,
--> 990                                          startupinfo)
    991             finally:
    992                 # Child is launched. Close the parent's copy of those pipe

FileNotFoundError: [WinError 2] The system cannot find the file specified

This crashes IPython. Running the very same command ffprobe -i "F:/tst.mp4" in CMD works like a charm.

Here's what I tried: Changing / to \ and \ , adding and removing quotes around the file path, changing the path to C:\tst.mp4.

Running the command os.system(ffmpeg_path+'ffprobe -i "F:/tst.mp4") does work.

What could possibly be going wrong here?

Upvotes: 0

Views: 1683

Answers (1)

Blender
Blender

Reputation: 298176

You use a list as the first argument to subprocess.Popen because that list is passed as the arguments to your executed program:

subprocess.Popen([ffmpeg_path + 'ffprobe', '-i', 'F:\\tst.mp4'], ...)

For example, Popen(['foo bar']) is the same as running an executable named foo bar.exe but Popen(['foo', 'bar']) will execute foo.exe with the argument bar.

You may want to use subprocess.check_output if all you're interested in is the output of the command:

output = subprocess.check_output([ffmpeg_path + 'ffprobe', '-i', 'F:\\tst.mp4'])

Upvotes: 1

Related Questions