Skip to content

Commit 1688804

Browse files
committed
vdh now can fastforward 1k frames when sampled frame gap is larger (speed up video seeking)
1 parent a12a17e commit 1688804

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Diff for: mmif/utils/video_document_helper.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import importlib
2+
import sys
3+
24
import math
35
import warnings
46
from io import StringIO
@@ -81,21 +83,30 @@ def extract_frames_as_images(video_document: Document, framenums: Iterable[int],
8183
:param as_PIL: return :py:class:`PIL.Image.Image` instead of :py:class:`~numpy.ndarray`
8284
:return: frames as a list of :py:class:`~numpy.ndarray` or :py:class:`~PIL.Image.Image`
8385
"""
86+
import cv2
8487
if as_PIL:
8588
from PIL import Image
8689
frames = []
8790
video = capture(video_document)
8891
cur_f = 0
8992
tot_fcount = video_document.get_property(FRAMECOUNT_DOCPROP_KEY)
93+
# when the target frame is more than this frames away, fast-forward instead of reading frame by frame
94+
# this is sanity-checked with a small number of video samples
95+
# (frame-by-frame ndarrays are compared with fast-forwarded ndarrays)
96+
skip_threadhold = 1000
9097
framenumi = iter(framenums) # make sure that it's actually an iterator, in case a list is passed
9198
next_target_f = next(framenumi, None)
9299
from wurlitzer import pipes as cpipes
93-
ffmpeg_outs = StringIO()
94100
ffmpeg_errs = StringIO()
95-
with cpipes(stderr=ffmpeg_errs, stdout=ffmpeg_outs):
101+
with cpipes(stderr=ffmpeg_errs, stdout=sys.stdout):
96102
while True:
97-
if next_target_f is None or cur_f > tot_fcount:
103+
if next_target_f is None or cur_f > tot_fcount or next_target_f > tot_fcount:
98104
break
105+
if next_target_f - cur_f > skip_threadhold:
106+
while next_target_f - cur_f > skip_threadhold:
107+
cur_f += skip_threadhold
108+
else:
109+
video.set(cv2.CAP_PROP_POS_FRAMES, cur_f)
99110
ret, frame = video.read()
100111
if cur_f == next_target_f:
101112
if not ret:

0 commit comments

Comments
 (0)