recorder Module
Video recording management for cat detection events.
This module provides a VideoRecorder class for managing video recording sessions. It handles file creation, frame writing, timing control, and automatic filename generation with timestamps.
The recorder uses OpenCV’s VideoWriter with the mp4v codec for compatibility on Raspberry Pi. Recordings are saved as MP4 files with timestamps in the filename.
Example
- Basic usage::
recorder = VideoRecorder(output_dir=Path(“recordings”), record_seconds=60) output_path = recorder.generate_filename() recorder.start_recording(output_path, (1920, 1080), fps=10) recorder.write_frame(frame) recorder.stop_recording()
- class recorder.VideoRecorder(output_dir: Path = PosixPath('recordings'), record_seconds: int = 60)[source]
Bases:
objectManages video recording with timing control.
- __init__(output_dir: Path = PosixPath('recordings'), record_seconds: int = 60)[source]
Initialize the video recorder.
- Parameters:
output_dir – Directory to save recordings
record_seconds – Duration to record in seconds
- is_recording() bool[source]
Check if currently recording.
- Returns:
True if recording, False otherwise
- should_continue_recording() bool[source]
Check if recording should continue based on elapsed time.
- Returns:
True if recording should continue, False if time limit reached
- start_recording(output_path: Path, frame_size: Tuple[int, int], fps: int) bool[source]
Start a new recording session.
- Parameters:
output_path – Full path to output video file
frame_size – Tuple of (width, height)
fps – Frames per second for the video
- Returns:
True if recording started successfully, False otherwise
- write_frame(frame) bool[source]
Write a frame to the current recording.
- Parameters:
frame – Frame to write (numpy array)
- Returns:
True if frame written successfully, False otherwise
- stop_recording() Path | None[source]
Stop the current recording.
- Returns:
Path to the recorded file, or None if no recording was active