How to Write Video Using OpenCV in Computer Vision
To write video using
OpenCV, create a VideoWriter object specifying the output filename, codec, frame rate, and frame size. Then, write frames to the video file using the write() method inside a loop.Syntax
The main steps to write video in OpenCV are:
- VideoWriter(filename, fourcc, fps, frameSize): Creates a video writer object.
- filename: Output video file name with extension (e.g., 'output.avi').
- fourcc: Codec code to compress the video (e.g., 'XVID').
- fps: Frames per second (e.g., 20.0).
- frameSize: Size of each frame as (width, height).
- write(frame): Writes a frame to the video file.
python
import cv2 # Define codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) # Write frames using out.write(frame) # Release with out.release() when done
Example
This example captures video from your webcam and writes it to a file named 'output.avi'. It shows how to open the camera, create a VideoWriter, write frames, and release resources.
python
import cv2 cap = cv2.VideoCapture(0) # Open default camera fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) while cap.isOpened(): ret, frame = cap.read() if not ret: break frame = cv2.resize(frame, (640, 480)) out.write(frame) # Write frame to video file cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() out.release() cv2.destroyAllWindows()
Output
A window showing live webcam video appears; pressing 'q' stops recording and saves 'output.avi' file.
Common Pitfalls
- Not matching
frameSizeinVideoWriterwith actual frame size causes errors or black video. - Using wrong codec or unsupported file extension can fail to save video.
- Forgetting to call
release()onVideoWritercauses incomplete files. - Writing frames before checking if capture is successful leads to crashes.
python
import cv2 cap = cv2.VideoCapture(0) fourcc = cv2.VideoWriter_fourcc(*'XVID') # Wrong frame size example (should match actual frame size) out = cv2.VideoWriter('output.avi', fourcc, 20.0, (320, 240)) ret, frame = cap.read() if ret: # Wrong: writing frame without resizing to (320,240) causes issues out.write(frame) # Frame size mismatch # Correct way: frame_resized = cv2.resize(frame, (320, 240)) out.write(frame_resized) cap.release() out.release()
Quick Reference
Tips for writing video with OpenCV:
- Always match
frameSizeinVideoWriterwith your frame's actual size. - Use
cv2.VideoWriter_fourcc(*'XVID')or other supported codecs. - Call
release()on bothVideoCaptureandVideoWriterto save files properly. - Check if frames are read successfully before writing.
Key Takeaways
Create a VideoWriter with correct filename, codec, fps, and frame size before writing frames.
Ensure frame size matches between captured frames and VideoWriter to avoid errors.
Always release VideoWriter and VideoCapture objects to save video files properly.
Check frame capture success before writing frames to avoid crashes.
Use common codecs like 'XVID' for wide compatibility.