我正在使用 OpenCV 从视频中提取帧,运行分割 AI 模型,并将帧和蒙版保存到文件夹中。当我运行代码来提取帧时,在处理一定数量的帧后遇到错误“超出抓取帧数据包读取最大尝试次数”。对于跨多个环境的相同视频,此问题始终会出现。
错误信息:
[ WARN:[email protected]] global cap_ffmpeg_impl.hpp:1541 grabFrame packet read max attempts exceeded, if your video have multiple streams (video, audio) try to increase attempt limit by setting environment variable OPENCV_FFMPEG_READ_ATTEMPTS (current value is 10000)
最小可重现示例
import os
import cv2
videofilename = "test.mp4"
capture = cv2.VideoCapture(videofilename)
frameNum = 0
createfolder = os.getcwd() + '/' + videofilename.split(".")[0] + '/'
if not os.path.exists(createfolder):
os.makedirs(createfolder)
os.makedirs(createfolder + "/frames/")
while True:
success, frame = capture.read()
if success is False:
break
frameNum += 1
framedownloadname = videofilename.split(".")[0] + '-fr' + str(frameNum) + '.jpg'
framedownloadloc = createfolder + '/frames/' + framedownloadname
print(framedownloadloc)
cv2.imwrite(framedownloadloc, frame)
img = cv2.imread(framedownloadloc)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
capture.release()
按照错误中的建议,我将 OPENCV_FFMPEG_READ_ATTEMPTS 环境变量增加到 10000。但是,这似乎对出现错误之前的帧数几乎没有影响。
答案已经找到了吗?