所以,我试图通过这里讨论的方法找到视频文件的长度:How to get the duration of a video in Python? , Using ffmpeg to obtain video durations in python .但在这样做的过程中,我遇到了一个我无法解决的问题:我明白了
FileNotFoundError:[WinError 2] The system cannot find the file specified
在尝试了一堆故障排除步骤后,我开始在 IPython 和 cmd 中分别运行以查看可能出现故障的地方。在 IPython 中使用此代码的精简版本给了我
In [11]: subprocess.Popen([ffmpeg_path+'ffprobe'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Out[11]: <subprocess.Popen at (...)>
这似乎很好,此时 CMD 也是如此。所以要增加一点复杂性:
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
这会使 IPython 崩溃。运行相同的命令
ffprobe -i "F:/tst.mp4"
在 CMD 中的工作就像一个魅力。这是我尝试过的:将/更改为\和\,在文件路径周围添加和删除引号,将路径更改为 C:\tst.mp4。
运行命令
os.system(ffmpeg_path+'ffprobe -i "F:/tst.mp4")
确实有效。这里可能出了什么问题?
最佳答案
您使用列表作为 subprocess.Popen
的第一个参数。因为该列表作为参数传递给您执行的程序:
subprocess.Popen([ffmpeg_path + 'ffprobe', '-i', 'F:\\tst.mp4'], ...)
例如,
Popen(['foo bar'])
与运行名为 foo bar.exe
的可执行文件相同但是 Popen(['foo', 'bar'])
将执行 foo.exe
带有参数 bar
.您可能想使用
subprocess.check_output
如果您只对命令的输出感兴趣:output = subprocess.check_output([ffmpeg_path + 'ffprobe', '-i', 'F:\\tst.mp4'])
关于python - 使用ffprobe在python中查找视频文件长度时如何解决文件路径错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48603302/