(
source_path: str, start: int, end: int | None, iterative_seek: bool = False
)
| 205 | |
| 206 | |
| 207 | def _validate_and_setup_video( |
| 208 | source_path: str, start: int, end: int | None, iterative_seek: bool = False |
| 209 | ) -> tuple[cv2.VideoCapture, int, int]: |
| 210 | video = cv2.VideoCapture(source_path) |
| 211 | if not video.isOpened(): |
| 212 | raise Exception(f"Could not open video at {source_path}") |
| 213 | total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) |
| 214 | if end is not None and end > total_frames: |
| 215 | raise Exception("Requested frames are outbound") |
| 216 | start = max(start, 0) |
| 217 | end = min(end, total_frames) if end is not None else total_frames |
| 218 | |
| 219 | if iterative_seek: |
| 220 | while start > 0: |
| 221 | success = video.grab() |
| 222 | if not success: |
| 223 | break |
| 224 | start -= 1 |
| 225 | elif start > 0: |
| 226 | video.set(cv2.CAP_PROP_POS_FRAMES, start) |
| 227 | |
| 228 | return video, start, end |
| 229 | |
| 230 | |
| 231 | def get_video_frames_generator( |
no test coverage detected