Open a video at the given path. If `backend` is specified but not available on the current system, OpenCV (`VideoStreamCv2`) will be used as a fallback. Arguments: path: Path to video file to open. frame_rate: Overrides detected frame rate if set. Takes precedence over `fram
(
path: StrPath,
frame_rate: FrameRate | None = None,
backend: str = "opencv",
framerate: float | None = None,
**kwargs,
)
| 82 | |
| 83 | |
| 84 | def open_video( |
| 85 | path: StrPath, |
| 86 | frame_rate: FrameRate | None = None, |
| 87 | backend: str = "opencv", |
| 88 | framerate: float | None = None, |
| 89 | **kwargs, |
| 90 | ) -> VideoStream: |
| 91 | """Open a video at the given path. If `backend` is specified but not available on the current |
| 92 | system, OpenCV (`VideoStreamCv2`) will be used as a fallback. |
| 93 | |
| 94 | Arguments: |
| 95 | path: Path to video file to open. |
| 96 | frame_rate: Overrides detected frame rate if set. Takes precedence over `framerate`. |
| 97 | backend: Name of specific backend to use, if possible. See |
| 98 | :data:`scenedetect.backends.AVAILABLE_BACKENDS` for backends available on the current |
| 99 | system. If the backend fails to open the video, OpenCV will be used as a fallback. |
| 100 | framerate: [DEPRECATED] Use `frame_rate` instead. Retained as a deprecated alias for |
| 101 | backwards compatibility; ignored when `frame_rate` is provided. |
| 102 | kwargs: Optional named arguments to pass to the specified `backend` constructor for |
| 103 | overriding backend-specific options. |
| 104 | |
| 105 | Returns: |
| 106 | Backend object created with the specified video path. |
| 107 | |
| 108 | Raises: |
| 109 | :class:`VideoOpenFailure`: Constructing the VideoStream fails. If multiple backends have |
| 110 | been attempted, the error from the first backend will be returned. |
| 111 | """ |
| 112 | # TODO(https://scenedetect.com/issue/548): emit DeprecationWarning when `framerate=` is |
| 113 | # used, once internal callers and downstream users have had a release to migrate. |
| 114 | if frame_rate is None: |
| 115 | frame_rate = framerate |
| 116 | last_error: Exception | None = None |
| 117 | # If `backend` is available, try to open the video at `path` using it. |
| 118 | if backend in AVAILABLE_BACKENDS: |
| 119 | backend_type = AVAILABLE_BACKENDS[backend] |
| 120 | try: |
| 121 | logger.debug("Opening video with %s...", backend_type.BACKEND_NAME) |
| 122 | return backend_type(path, frame_rate, **kwargs) |
| 123 | except VideoOpenFailure as ex: |
| 124 | logger.warning("Failed to open video with %s: %s", backend_type.BACKEND_NAME, str(ex)) |
| 125 | if backend == VideoStreamCv2.BACKEND_NAME: |
| 126 | raise |
| 127 | last_error = ex |
| 128 | else: |
| 129 | logger.warning("Backend %s not available.", backend) |
| 130 | # Fallback to OpenCV if `backend` is unavailable, or specified backend failed to open `path`. |
| 131 | backend_type = VideoStreamCv2 |
| 132 | logger.warning("Trying another backend: %s", backend_type.BACKEND_NAME) |
| 133 | try: |
| 134 | return backend_type(path, frame_rate) |
| 135 | except VideoOpenFailure as ex: |
| 136 | logger.debug("Failed to open video: %s", str(ex)) |
| 137 | if last_error is None: |
| 138 | last_error = ex |
| 139 | # Propagate any exceptions raised from specified backend, instead of errors from the fallback. |
| 140 | assert last_error is not None |
| 141 | raise last_error |
no outgoing calls