Capture and save webcam video in Python using OpenCV
Python is a great programming language to work with the webcam. OpenCV is a popular library available for Python that can make it easier to work with videos and webcam. Using this library, you can capture and record webcam video as well as working with videos too.
Many of you may think about how to take videos from webcam and save it in the directory using Python programming skill. So here, I am going to tell you how to capture and save webcam video in Python using OpenCV.
Below is the step by step guide and explanation of our program:
First import the OpenCV library:
import cv2
Off-course, you have to install the OpenCV library first. Visit this page to see how to install this library if you haven’t installed it yet.
After that create VideoCapture object to capture the video from our webcam and pass device index argument that will define the camera that is connected with our PC:
vid_capture = cv2.VideoCapture(0)
In my case, the device index is 0 which represent the webcam of my PC.
Next, put our output data in our file:
vid_cod = cv2.VideoWriter_fourcc(*'XVID') output = cv2.VideoWriter("videos/cam_video.mp4", vid_cod, 20.0, (640,480))
The above code will create a file “cam_video.mp4” in the directory “videos” and write in this file.
Now we are going to capture each webcam video data frame by frame and write it in our video file that we have mentioned above:
while(True): # Capture each frame of webcam video ret,frame = vid_capture.read() cv2.imshow("My cam video", frame) output.write(frame) # Close and break the loop after pressing "x" key if cv2.waitKey(1) &0XFF == ord('x'): break
In the above code, we have set the “x” key to close our webcam.
In the end, we just have to put these code you can see below:
# close the already opened camera vid_capture.release() # close the already opened file output.release() # close the window and de-allocate any associated memory usage cv2.destroyAllWindows()
Above in our program, we release our captured video data. Also, we call the destroyAllWindows() to close our window and de-allocate any associated memory usage.
Complete and final code to record webcam video in Python
Below is the complete and final Python code to save video from our webcam:
import cv2 #Capture video from webcam vid_capture = cv2.VideoCapture(0) vid_cod = cv2.VideoWriter_fourcc(*'XVID') output = cv2.VideoWriter("videos/cam_video.mp4", vid_cod, 20.0, (640,480)) while(True): # Capture each frame of webcam video ret,frame = vid_capture.read() cv2.imshow("My cam video", frame) output.write(frame) # Close and break the loop after pressing "x" key if cv2.waitKey(1) &0XFF == ord('x'): break # close the already opened camera vid_capture.release() # close the already opened file output.release() # close the window and de-allocate any associated memory usage cv2.destroyAllWindows()
Also, read:
- Convert RGB to Binary Image in Python (Black and White)
- Read an image in Python and open it in a Window
Now we are ready to run our program. If everything goes right, you will able to see the webcam window open. After you press “x”, the window will be closed and the video data will save inside “videos/cam_video.mp4”.
We can open our mp4 file with any of the mp4 video players to check our recorded video that was recorded by our Python program that we just have written.
So, we have successfully able to capture and record webcam video in Python using the OpenCV library.
Thanks Faruque. Just what I wanted.
Thanks !
Thank you very much for simple solution.
I had to change two things to have it work for me:
1. The codec only worked when I changed the output file to avi: “videos/cam_video.avi”
2. The webcam width-height should match, in my case
output = cv2.VideoWriter(“videos/cam_video.mp4”, vid_cod, 20.0, (1280,720))