The task mentioned by our team to me is to capture video from the webcam and saving it using python.
Myself by getting knowledge through the multiple resources I feel too share something important steps to complete this task .
Capturing and saving video using OpenCV Python
Here the procedure I have done to complete is shown below by me in some points that are given :
- First open the webcam and check whether its working or not working .
- Now the next one is to getting frame width and height .
- setting up a video writer to save the frames .
- Now to capture and display video frames until you press ‘q’.
- Writing down the output of the task in form of files per data .
- putting down the resource and stop .
- exit.
import cv2 cap = cv2.VideoCapture(0) if not cap.isOpened(): print("Unable to access the webcam.") exit() frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) codec = cv2.VideoWriter_fourcc(*'MJPG') output = cv2.VideoWriter('my_video.avi', codec, 20.0, (frame_width, frame_height)) if not output.isOpened(): print("Error: Unable to open video writer.") cap.release() exit() print("Recording... Press 'q' to stop.") while True: ret, frame = cap.read() if not ret: print("Error capturing frame.") break output.write(frame) cv2.imshow('Recording...', frame) if cv2.waitKey(1) & 0xFF == ord('q'): print("Recording stopped.") break cap.release() output.release() cv2.destroyAllWindows() print("Video saved as 'my_video.avi'")
OUTPUT:
-
Opens the webcam — if it doesn’t work, it says
"Unable to access the webcam."
-
Reads the video size (width and height).
-
Creates a video file called
"my_video.avi"
to save the recording. -
Shows
"Recording... Press 'q' to stop."
-
Displays the recording on the screen as it’s happening.
-
Press 'q' to stop recording and save the file.
-
Finally says
"Video saved as 'my_video.avi'"
.Here we can also learn more from these links about open cv are :
Capture Webcam Video in C++ using OpenCV
https://www.geeksforgeeks.org/opencv-python-tutorial
Myself onkar being intern on python a opencv being learned to good level through this task thanks to code speedy and team .