After learning to show your webcam, the next step is saving the video to a file. OpenCV does this with cv2.VideoWriter, which writes frames one by one into a video file.
What You Will Learn
- What a video codec (FourCC) is
- How to create a
VideoWriter - How to save webcam video to a file
- How to match frame size and frame rate
Prerequisites
Python 3, OpenCV, a webcam, and the basics from Access Camera Using OpenCV.
Record Webcam Video to out.avi
import cv2
capture = cv2.VideoCapture(0)
width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
cod = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('out.avi', cod, 20.0, (width, height))
while True:
ret, frame = capture.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray', gray)
cv2.imshow('Color', frame)
out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
out.release()
cv2.destroyAllWindows()
Explanation:
VideoWriter_fourcc(*'XVID')picks the XVID codec, which works well with.avifiles.VideoWriter('out.avi', cod, 20.0, (width, height))creates the file at 20 frames per second.- We read the real camera size with
capture.get()so the writer size matches the frames. out.write(frame)adds one frame to the video.out.release()finishes and closes the file. Without it the video may be empty or broken.
| Parameter | Meaning |
|---|---|
filename |
Output file, e.g. out.avi |
fourcc |
Codec code |
fps |
Frames per second |
frameSize |
(width, height), must match every frame you write |
Two windows show the color and grayscale video. When you press q, out.avi is saved in your folder.
Common Codecs
| FourCC | File type |
|---|---|
XVID |
.avi (works on most systems) |
MJPG |
.avi (large files, very compatible) |
mp4v |
.mp4 |
Saving the Grayscale Video
out = cv2.VideoWriter('gray.avi', cod, 20.0, (width, height), isColor=False)
# inside the loop:
out.write(gray)
Explanation: For single-channel frames you must pass isColor=False, otherwise nothing is saved.
Common Errors
| Problem | Cause and fix |
|---|---|
| The output file is 0 KB or will not play | The frame size does not match the size given to VideoWriter, or out.release() was not called. |
error: (-215:Assertion failed) !_src.empty() in imshow |
The camera did not return a frame (ret is False). Another app may be using the webcam, or the index is wrong. Try cv2.VideoCapture(1), and always check if not ret: break. |
| Window freezes or does not close | cv2.waitKey(1) must be inside the loop, and you must call capture.release() at the end. |
| Does not work in Google Colab | Colab runs on a remote server with no webcam. Run this on your own computer. |
Practice Exercises
- Save the video as .mp4 using the
mp4vcodec. - Record for exactly 10 seconds using a frame counter (fps × 10).
- Add the current time on each frame with
cv2.putTextbefore writing it.
Interview Questions
Q: What is FourCC?
A 4-character code that identifies the video codec, such as XVID or mp4v.
Q: Why might VideoWriter create an empty file?
Wrong frame size, wrong isColor setting, an unsupported codec, or a missing release().
Summary
Create a VideoWriter with a codec, fps and the exact frame size, call write() for each frame, and always release() both the camera and the writer.
Next: Face Detection from Webcam · Access Camera Using OpenCV
Machine Learning Tutorials, Courses and Certifications