Reading and Saving Videos with OpenCV-Python
Video content can be sourced from two primary places: pre-recorded local files, or real-time streams from capture devices like computer webcams or phone cameras. OpenCV-Python uses the same core API to read both types of video, with only the input parameter changing to switch sources.
Reading Video from a Webcam
Reading and displaying a live webcam feed requires two core steps: initializing the camera capture, then continuously reading and rendering frames.
import cv2
# Initialize capture with default webcam
capture = cv2.VideoCapture(0)
while capture.isOpened():
# Read a single frame from the stream
read_success, frame = capture.read()
if not read_success:
print("Failed to access camera")
break
cv2.imshow('Live Feed', frame)
# Exit loop if 'q' key is pressed
if cv2.waitKey(1) == ord('q'):
break
# Clean up resources
capture.release()
cv2.destroyAllWindows()
Key details for this code:
- Passing
0tocv2.VideoCapture()opens the device's default camera. For systems with multiple connected cameras, use1,2, or other integers to select alternate devices. capture.isOpened()validates that the camera was opened successfully before starting the frame loop.capture.read()returns two values:read_success(a boolean indicating if the frame was read without error) andframe(the raw image data of the captured frame).- A
whileloop continuously fetches new frames to create a continuous live stream. - A 1 millisecond delay set via
cv2.waitKey(1)enables near-real-time display of the feed. - The code checks for a 'q' key press to exit the loop, then releases the camera resource and closes all OpenCV display windows.
Reading Local Video Files
Reading a saved local video folllows almost the exact same workflow as reading a webcam stream, with only two small changes: the input to cv2.VideoCapture() and the delay value passed to cv2.waitKey().
import cv2
# Path to your local video file (include file extension)
local_video_path = "./input_video.mov"
capture = cv2.VideoCapture(local_video_path)
while capture.isOpened():
read_success, frame = capture.read()
if not read_success:
print("Could not load video file")
break
cv2.imshow('Playback', frame)
# Exit on 'q' press, 30ms delay per frame for standard playback
if cv2.waitKey(30) == ord('q'):
break
# Clean up
capture.release()
cv2.destroyAllWindows()
The key differences from webcam capture:
- Pass the full file path (including the file extension) of your local video to
cv2.VideoCapture()to load it. - A 30 millisecond delay per frame matches normal playback speed for most standard frame rates, matching what users expect for video playback.
Saving Video to File
Whether you are saving a capture from a webcam or saving a processed version of an existing local video, the saving workflow is consistent:
- Load the source video stream
- Configure output video parameters (file path, resolution, frame rate, codec)
- Process and write each frame to the output file sequentially
The example below converts a color input video to grayscale and saves the processed result to a new file:
import cv2
# Input and output file paths
input_path = "input_video.mov"
output_path = "grayscale_output.mp4"
# Open source video
source = cv2.VideoCapture(input_path)
# Exit if video failed to open
if not source.isOpened():
print("Error: Could not open input video")
exit()
# Extract native video properties from source
frame_width = int(source.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(source.get(cv2.CAP_PROP_FRAME_HEIGHT))
source_fps = int(source.get(cv2.CAP_PROP_FPS))
# Configure video output settings
codec_fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_writer = cv2.VideoWriter(
output_path,
codec_fourcc,
source_fps,
(frame_width, frame_height),
isColor=False
)
# Process and write each frame
while source.isOpened():
read_success, frame = source.read()
if not read_success:
break
# Convert frame to grayscale
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Write processed frame to output
output_writer.write(gray_frame)
# Optional: display the processed video during processing
cv2.imshow("Processed Grayscale", gray_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Clean up all resources
source.release()
output_writer.release()
cv2.destroyAllWindows()
Key configuration details for cv2.VideoWriter:
output_path: The file path where the finished video will be saved.fourcc: A 4-character code defining the video codec. Common options enclude:cv2.VideoWriter_fourcc(*'XVID'): For.avifiles, good cross-platform compatibilitycv2.VideoWriter_fourcc(*'MJPG'): Motion JPEG codec for.avifilescv2.VideoWriter_fourcc(*'mp4v'): Standard codec for.mp4filescv2.VideoWriter_fourcc(*'DIVX'): Another popular codec for.avifiles
fps: Frames per second, which controls playback speed and smoothness. Common values are 24, 30, and 60. The example above uses the same frame rate as the source video.frameSize: A tuple defining the width and height (in pixels) of the output video.isColor: A boolean indicating if the output video is color. Set toFalsefor grayscale video, as in the example.
After processing all frames, always release both the source capture and output writer resources to ensure the video file is saved correctly.