Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to manually stop video recording?

Hey guys, I am new to the world of programming, I am not from the computer science background. Currently, I am working on a project in which I am using raspberry pi 3 and a USB webcam (I am not using the Pi camera). My objective is to record a video using the webcam, I have interfaced a button switch with one of the GPIO pins, so if I press the push button once it should start recording the video and on the next press, it must stop recording. I used the example code to check whether I am able to record the video or not? I run the code using terminal and yes it works, but whenever I want to stop recording I have to press Ctrl+C, which terminates the entire process, I also want to execute some different operation once the video stops, but I have no idea how to do that. Please I need your guidance.

import numpy as np
import cv2
cap = cv2.VideoCapture(0)   #defining the webcam
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter('webcamOut.avi',fourcc,30.0,(640,480))

while True:
    ret, frame = cap.read()
    out.write(frame)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF ==ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindow()

Also, one more thing. If I set the resolution higher than 480 (i.e. 720 or 1080), the output video saved with 5kb file size. what to do in that case? thanks