我尝试了不同的方法,但没有一个起作用。
视频正在运行,但是文件output.avi为空。
这是代码
import cv2
vid=cv2.VideoCapture(0)
if (not vid.isOpened()):
print 'Error'
size = (int(vid.get(3)), int(vid.get(4)))
fourcc = cv2.cv.CV_FOURCC('M','J','P','G')
newVideo=cv2.VideoWriter('output.avi',fourcc,20.0,(640,360))
while(vid.isOpened()):
ret,frame=vid.read()
if ret is True:
frame=cv2.flip(frame,0)
newVideo.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1)& 0xFF==ord('q'):
break
else:
break
vid.release()
newVideo.release()
cv2.destroyAllWindows()
最佳答案
问题很简单,您正在阅读框架,但尝试以不同的大小书写它们。
解决方案在您的代码内部。您定义了:
size = (int(vid.get(3)), int(vid.get(4)))
但是您没有像这样使用它:newVideo=cv2.VideoWriter('output.avi',fourcc,20.0,size)
您将大小设置为手动(640,360),这不是捕获的帧的大小。
关于python - Opencv写视频正在运行,但保存的output.avi文件为0KB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63783375/