Skip to content

Commit 2f0179c

Browse files
committed
[api] Make default backend explicit in open_video signature.
1 parent 598182f commit 2f0179c

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

scenedetect/__init__.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
def 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

scenedetect/cli/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,15 +762,15 @@ def _open_video_stream(self, input_path: AnyStr, framerate: Optional[float],
762762
self.video_stream = open_video(
763763
path=input_path,
764764
framerate=framerate,
765-
backend='pyav',
765+
backend=backend,
766766
threading_mode=self.config.get_value('backend-pyav', 'threading-mode'),
767767
suppress_output=self.config.get_value('backend-pyav', 'suppress-output'),
768768
)
769769
elif backend == 'opencv':
770770
self.video_stream = open_video(
771771
path=input_path,
772772
framerate=framerate,
773-
backend='opencv',
773+
backend=backend,
774774
max_decode_attempts=self.config.get_value('backend-opencv',
775775
'max-decode-attempts'),
776776
)

0 commit comments

Comments
 (0)