簡體   English   中英

如何在使用ffprobe查找python中視頻文件的長度時解決文件路徑錯誤?

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

因此,我正在嘗試通過此處討論的方法來查找視頻文件的長度: 如何在Python中獲取視頻的時長? 使用ffmpeg在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崩潰。 在CMD中運行相同的命令ffprobe -i "F:/tst.mp4"就像一個ffprobe -i "F:/tst.mp4"

這是我嘗試的方法:將/更改為\\和\\,在文件路徑周圍添加和刪除引號,將路徑更改為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的可執行文件相同,但foo bar.exe Popen(['foo', 'bar'])將使用參數bar執行foo.exe

如果您只對命令的輸出感興趣,則可以使用subprocess.check_output

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM