python - 使用ffprobe在python中查找视频文件长度时如何解决文件路径错误?

标签 python video ffmpeg subprocess

所以,我试图通过这里讨论的方法找到视频文件的长度: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/

相关文章:

python - 使用 TensorFlow 层的 `kernel_constraint` 实现权重归一化

python - 如何使用 vault 在 Ansible 中运行 playbook api

java - 无法启动视频播放器 Activity

c# - 使用 ffmpeg/ffprobe 获取视频的长度不起作用

ffmpeg - 如何合并两个不同分辨率的.mp4视频

python - 如何读取 Python CGI 脚本中的所有 HTTP header ?

python - 将 Flask env vars 添加到 virtualenv 的激活脚本可以吗?

video - ffmpeg 输出的含义(tbc、tbn、tbr)

css - 删除图像和视频之间的空白

python - PyDub : ffmpeg header missing