Python OpenCV打开Vid文件与打开网络摄像头

时间:2016-05-09 11:10:49

标签: python opencv

我无法弄清楚为什么这不起作用。

以下代码使用我的网络摄像头完美运行:

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)
        # write the flipped frame
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

然而,当我将网络摄像头换成视频文件时,输出不会生成视频。只有5.7kb文件名为output.avi:

import numpy as np
import cv2
cap = cv2.VideoCapture('Input.avi')
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)
        # write the flipped frame
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

我可以在我的窗口中看到视频正在处理但未保存。我也尝试更改分辨率以匹配初始视频文件。

1 个答案:

答案 0 :(得分:0)

我在Ubuntu上使用OpenCV,这对我有用:

out = cv2.VideoWriter("output.avi", cv.CV_FOURCC(*'DIVX'), fps, (640, 480))

查看它是否适用于Windows。