9999def open_video (
100100 path : str ,
101101 framerate : Optional [float ] = None ,
102- backend : Optional [ str ] = None ,
102+ backend : str = 'opencv' ,
103103 ** kwargs ,
104104) -> VideoStream :
105105 """Open a video at the given path. If `backend` is specified but not available on the current
@@ -108,7 +108,7 @@ def open_video(
108108 Arguments:
109109 path: Path to video file to open.
110110 framerate: Overrides detected framerate if set.
111- backend: Name of specific to use if possible. See
111+ backend: Name of specific backend to use, if possible. See
112112 :py:data:`scenedetect.backends.AVAILABLE_BACKENDS` for backends available on the current
113113 system. If the backend fails to open the video, OpenCV will be used as a fallback.
114114 kwargs: Optional named arguments to pass to the specified `backend` constructor for
@@ -123,26 +123,27 @@ def open_video(
123123 """
124124 # Try to open the video with the specified backend.
125125 last_error = None
126- if backend is not None and backend != 'opencv' and backend in AVAILABLE_BACKENDS :
126+ if backend in AVAILABLE_BACKENDS :
127+ backend_type = AVAILABLE_BACKENDS [backend ]
127128 try :
128- logger .debug ('Opening video with %s...' , AVAILABLE_BACKENDS [ backend ]. __name__ )
129- return AVAILABLE_BACKENDS [ backend ] (path , framerate , ** kwargs )
129+ logger .debug ('Opening video with %s...' , backend_type . BACKEND_NAME )
130+ return backend_type (path , framerate , ** kwargs )
130131 except VideoOpenFailure as ex :
131- logger .debug ('Failed to open video: %s' , str (ex ))
132- logger .debug ('Falling back to OpenCV.' )
132+ logger .warning ('Failed to open video with %s: %s' , backend_type .BACKEND_NAME , str (ex ))
133+ if backend == VideoStreamCv2 .BACKEND_NAME :
134+ raise
133135 last_error = ex
134136 else :
135- logger .debug ('Backend %s not available, falling back to OpenCV .' , backend )
136-
137- # OpenCV backend must be available.
138- logger .debug ( 'Opening video with %s... ' , VideoStreamCv2 . __name__ )
137+ logger .warning ('Backend %s not available.' , backend )
138+ # Fallback to OpenCV if `backend` could not open the video or is unavailable.
139+ backend_type = VideoStreamCv2
140+ logger .warning ( 'Trying another backend: %s' , backend_type . BACKEND_NAME )
139141 try :
140- return VideoStreamCv2 (path , framerate , ** kwargs )
142+ return backend_type (path , framerate )
141143 except VideoOpenFailure as ex :
142144 logger .debug ('Failed to open video: %s' , str (ex ))
143145 if last_error is None :
144146 last_error = ex
145-
146147 # If we get here, either the specified backend or the OpenCV backend threw an exception, so
147148 # make sure we propagate it.
148149 assert last_error is not None
0 commit comments