Reputation: 19
I want to save my webcam video using 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.
Upvotes: 1
Views: 683
Reputation: 244282
As I said in my comment, the solution is to change:
cv2.waitKey(0)
to:
cv2.waitKey(another_value)
by example 1.
According to the docs the parameter that receives cv2.waitKey() indicates the delay that is given:
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.
Upvotes: 1