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

Python OpenCV打开视频文件与打开网络摄像头,python,opencv,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.

我不明白为什么这不起作用

以下代码在使用我的网络摄像头时非常有效:

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()
然而,当我用网络摄像头交换视频文件时,输出不会生成视频。只有名为output.avi的5.7kb文件:

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()

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

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

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

查看它是否在Windows上工作。

如果完全注释掉while循环,从而只打开和关闭output.avi,会发生什么?