How can I make video to pause/continue while recording in OpenCV Python?
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while (cap):
# Capture frame-by-frame
ret, frame = cap.read()
out.write(frame)
# Display the resulting frame
cv2.imshow('video recording', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
out.release()
cv2.destroyAllWindows()
You'll get continuous frames from the VideoCapture live feed. You can set a flag to decide whether a frame should be written or not:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
isRecording=true
while (cap):
# Capture frame-by-frame
ret, frame = cap.read()
if(isRecording):#read the boolean to decide whether to write frame or not
out.write(frame)
# Display the resulting frame
cv2.imshow('video recording', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) & 0xFF == ord('p'):#Pause
isRecording=false
if cv2.waitKey(1) & 0xFF == ord('c'):#Continue
isRecording=true
# When everything done, release the capture
cap.release()
out.release()
cv2.destroyAllWindows()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With