视频捕获后无法在CV2中读取视频文件

时间:2018-03-04 09:58:04

标签: python-3.x webcam video-capture video-processing cv2

我从网络摄像头捕获文件并保存:

fourcc = cv2.VideoWriter_fourcc('H','2','6','4')
out = cv2.VideoWriter(self.output_video_fn, fourcc, 20.0, (w, h), isColor = True)
flag = False
while not flag:
        success, buf = self.cam.read()
        print("******* Success is ", success)
        print(buf)
        if success == True:
            print("Wait")
            cv2.waitKey(1500) #todo delete after loop is good
            print("Here")
            flag = True
            # self.IAPS_TASK_IS_FINISHED = True
            # print(buf)
            out.write(buf)
    self.cam.release()
    print("Saved video to ", self.output_video_fn)

保存后我试图播放它。我无法在VLC中播放(大小为162KB,并且持续时间为00:00)。我试着玩cv2:

cap = cv2.VideoCapture(self.output_video_fn)

while(cap.isOpened()):
        ret, frame = cap.read()
        print("Ret is ", ret)
        print("frame is ", frame)

        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

cap.release()
cv2.destroyAllWindows()

但Ret为False且frame为None。 问题是什么?

1 个答案:

答案 0 :(得分:0)

我只使用OpenCV及其C和C ++ API,但是如果您尝试编写包含Alpha通道的图像数据,或者如果编解码器在系统上不可用,我之前就注意到了VideoWriter问题。如果编解码器不存在,某些环境似乎不会导致异常。你能确认你有H264吗?