Adapter for existing VideoCapture objects. Unlike VideoStreamCv2, this class supports VideoCaptures which may not support seeking.
| 363 | |
| 364 | |
| 365 | class VideoCaptureAdapter(VideoStream): |
| 366 | """Adapter for existing VideoCapture objects. Unlike VideoStreamCv2, this class supports |
| 367 | VideoCaptures which may not support seeking. |
| 368 | """ |
| 369 | |
| 370 | def __init__( |
| 371 | self, |
| 372 | cap: cv2.VideoCapture, |
| 373 | frame_rate: FrameRate | None = None, |
| 374 | max_read_attempts: int = 5, |
| 375 | framerate: float | None = None, |
| 376 | ): |
| 377 | """Create from an existing OpenCV VideoCapture object. Used for webcams, live streams, |
| 378 | pipes, or other inputs which may not support seeking. |
| 379 | |
| 380 | Arguments: |
| 381 | cap: The `cv2.VideoCapture` object to wrap. Must already be opened and ready to |
| 382 | have `cap.read()` called on it. |
| 383 | frame_rate: If set, overrides the detected frame rate. Takes precedence over |
| 384 | `framerate`. |
| 385 | max_read_attempts: Number of attempts to continue decoding the video |
| 386 | after a frame fails to decode. This allows processing videos that |
| 387 | have a few corrupted frames or metadata (in which case accuracy |
| 388 | of detection algorithms may be lower). Once this limit is passed, |
| 389 | decoding will stop and emit an error. |
| 390 | framerate: [DEPRECATED] Use `frame_rate` instead. Retained as a deprecated |
| 391 | alias for backwards compatibility; ignored when `frame_rate` is provided. |
| 392 | |
| 393 | Raises: |
| 394 | ValueError: capture is not open, frame rate or max_read_attempts is invalid |
| 395 | """ |
| 396 | super().__init__() |
| 397 | |
| 398 | # TODO(https://scenedetect.com/issue/548): emit DeprecationWarning when `framerate=` is |
| 399 | # used, once internal callers and downstream users have had a release to migrate. |
| 400 | if frame_rate is None: |
| 401 | frame_rate = framerate |
| 402 | if frame_rate is not None and frame_rate < MAX_FPS_DELTA: |
| 403 | raise ValueError(f"Specified frame rate ({float(frame_rate):f}) is invalid!") |
| 404 | if max_read_attempts < 0: |
| 405 | raise ValueError("Maximum decode attempts must be >= 0!") |
| 406 | if not cap.isOpened(): |
| 407 | raise ValueError("Specified VideoCapture must already be opened!") |
| 408 | if frame_rate is None: |
| 409 | frame_rate = cap.get(cv2.CAP_PROP_FPS) |
| 410 | if frame_rate < MAX_FPS_DELTA: |
| 411 | raise FrameRateUnavailable() |
| 412 | |
| 413 | self._cap = cap |
| 414 | self._frame_rate: Fraction = framerate_to_fraction(frame_rate) |
| 415 | self._num_frames = 0 |
| 416 | self._max_read_attempts = max_read_attempts |
| 417 | self._decode_failures = 0 |
| 418 | self._warning_displayed = False |
| 419 | self._time_base: float = 0.0 |
| 420 | |
| 421 | # |
| 422 | # Backend-Specific Methods/Properties |
no outgoing calls