简体   繁体   English

无法在opencv中保存视频

[英]Trouble saving video in opencv

I want to save my webcam video using opencv.我想使用 opencv 保存我的网络摄像头视频。

I wrote this code.我写了这段代码。

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

The problem I am facing is that it is recording only black screen, although I have checked my webcam.我面临的问题是它只录制黑屏,尽管我已经检查了我的网络摄像头。

As I said in my comment, the solution is to change:正如我在评论中所说,解决方案是改变:

cv2.waitKey(0) 

to:到:

cv2.waitKey(another_value)

by example 1.通过示例 1。

According to the docs the parameter that receives cv2.waitKey() indicates the delay that is given:根据文档,接收cv2.waitKey()的参数表示给出的延迟:

在此处输入图片说明

In your case it is necessary since saving the image in the video, also is always convenient since an image is given at 60Hz so Overcoming that frequency is unnecessary.在您的情况下,这是必要的,因为将图像保存在视频中,也总是很方便,因为图像以 60Hz 给出,因此不需要克服该频率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM