1. Capture video
cap = cv2.VideoCapture(xx)
arg1: the index of the device or the video file name
After that, you can capture frame by frame, and finally don't forget to release the captured video cap
2. Capture a frame
ret, frame = cap.read ()
ret is the returned boolean value, whether the capture is successful, frame is a captured frame
Sometimes an error occurs because the capture is not initialized, use cap.isOpened() to judge. If it returns False, use cap.open() to open
3. Capture the properties of the video
View the properties of the video through cap.get(propId). The propId value is 0~18, representing different properties.
Set the property value through cap.set(propId, value)
4. Save the video
cv2.VideoWriter()
arg1: saved file name, arg2: encoder code, arg3: frames per second, arg4: size of each frame, arg5: color Flg (to be confirmed)
----- sample coding -----
import cv2
cap = cv2.VideoCapture(0)
#define codec
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#Create VideoWriter object
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while True:
# Capture frame-by-frame
ret, frame = cap.read ()
#operations on the frame
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# cv2.imshow('Capture Camera', gray)
# write the flipped frame
out.write(frame)
cv2.imshow('Capture Camera', frame)
# Press q to exit
if cv2.waitKey(1) == ord('q'):
break
#release the capture
cap.release()
out.release()
cv2.destroyAllWindows()
-- End --