Skip to content

Commit 1a64027

Browse files
committed
Set ffmpeg as default backend for OpenCV
1 parent a6f95f0 commit 1a64027

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

benchmarks/decoders/benchmark_decoders.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
AbstractDecoder,
1919
DecordAccurate,
2020
DecordAccurateBatch,
21+
OpenCVDecoder,
2122
plot_data,
2223
run_benchmarks,
2324
TorchAudioDecoder,
@@ -28,7 +29,6 @@
2829
TorchCodecPublic,
2930
TorchCodecPublicNonBatch,
3031
TorchVision,
31-
OpenCVDecoder,
3232
)
3333

3434

@@ -62,7 +62,9 @@ class DecoderKind:
6262
{"backend": "video_reader"},
6363
),
6464
"torchaudio": DecoderKind("TorchAudio", TorchAudioDecoder),
65-
"opencv": DecoderKind("OpenCV", OpenCVDecoder),
65+
"opencv": DecoderKind(
66+
"OpenCV[backend=FFMPEG]", OpenCVDecoder, {"backend": "FFMPEG"}
67+
),
6668
}
6769

6870

benchmarks/decoders/benchmark_decoders_library.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,20 @@ def decode_and_resize(self, video_file, pts_list, height, width, device):
145145
]
146146
return frames
147147

148+
148149
class OpenCVDecoder(AbstractDecoder):
149-
def __init__(self):
150-
import cv2.videoio_registry as vr
150+
def __init__(self, backend):
151+
import cv2
152+
153+
self._available_backends = {"FFMPEG": cv2.CAP_FFMPEG}
154+
self._backend = self._available_backends.get(backend)
151155

152156
self._print_each_iteration_time = False
153-
api_pref = None
154-
# Check backend abi/api for compatibility
155-
for backend in vr.getStreamBufferedBackends():
156-
if not vr.hasBackend(backend):
157-
continue
158-
if not vr.isBackendBuiltIn(backend):
159-
_, abi, api = vr.getStreamBufferedBackendPluginVersion(backend)
160-
if (abi < 1 or (abi == 1 and api < 2)):
161-
continue
162-
api_pref = backend
163-
break
164-
self._backend = api_pref
165157

166158
def decode_frames(self, video_file, pts_list):
167159
import cv2
168160

169-
cap = cv2.VideoCapture(video_file, self._backend, [])
161+
cap = cv2.VideoCapture(video_file, self._backend)
170162
if not cap.isOpened():
171163
raise ValueError("Could not open video stream")
172164

@@ -194,7 +186,7 @@ def decode_frames(self, video_file, pts_list):
194186
def decode_first_n_frames(self, video_file, n):
195187
import cv2
196188

197-
cap = cv2.VideoCapture(video_file, self._backend, [])
189+
cap = cv2.VideoCapture(video_file, self._backend)
198190
if not cap.isOpened():
199191
raise ValueError("Could not open video stream")
200192

@@ -212,7 +204,11 @@ def decode_first_n_frames(self, video_file, n):
212204

213205
def decode_and_resize(self, video_file, pts_list, height, width, device):
214206
import cv2
215-
frames = [cv2.resize(frame, (width, height)) for frame in self.decode_frames(video_file, pts_list)]
207+
208+
frames = [
209+
cv2.resize(frame, (width, height))
210+
for frame in self.decode_frames(video_file, pts_list)
211+
]
216212
return frames
217213

218214

0 commit comments

Comments
 (0)