无法在opencv中保存视频

问题描述 投票:1回答:1

我想用opencv保存我的网络摄像头视频。

我写了这段代码。

import numpy as np
import cv2

cap=cv2.VideoCapture(0)
#Define the codec
#FourCC code is passed as cv2.VideoWriter_fourcc('M','J','P','G')
#or cv2.VideoWriter_fourcc(*'MJPG') for MJPG.
fourcc = cv2.VideoWriter_fourcc(*'XVID')

#Define VideWrite object
#cv2.VideoWrite('arg1',arg2,arg3,(width,heigh))
#arg1:output file name
#arg2:Specify Fourcc code
#arg3: frames per seconds
#FourCC is a 4-byte code used to specify video codec
out=cv2.VideoWriter('SaveAVideo.avi',fourcc,20.0, (640,480))


while(cap.isOpened()):      

   ret,frame = cap.read()
   print('frame =',frame)
   print('ret = ',ret)
   if ret==True:
          frame =cv2.flip(frame,0)
          #Write the flipped frame
          out.write(frame)
          cv2.imshow('frame',frame)
          if cv2.waitKey(0) & 0xFF== ord('q'):
               break
   else:
          break
   print('after while loop')
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

我面临的问题是它只录制黑屏,虽然我已经检查了我的网络摄像头。

python opencv
1个回答
1
投票

正如我在评论中所说,解决方案是改变:

cv2.waitKey(0) 

至:

cv2.waitKey(another_value)

通过例子1。

根据文档,接收cv2.waitKey()的参数表示给出的延迟:

enter image description here

在您的情况下,由于保存图像在视频中是必要的,因此图像以60Hz给出也是非常方便的,因此不需要克服该频率。

© www.soinside.com 2019 - 2024. All rights reserved.