Skip to content

Commit 632a3ea

Browse files
committed
Take video screenshots after 50% of frames instead of 60secs of video by default, to support thumbnailing shorter videos
1 parent 33cf860 commit 632a3ea

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

src/bma_client_lib/bma_client.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,18 @@ def _handle_thumbnail_source_job(self, job: ThumbnailSourceJob, fileinfo: dict[s
202202
# unsupported filetype
203203
raise JobNotSupportedError(job=job)
204204

205-
def _get_video_screenshot(self, job: ThumbnailSourceJob, seconds: int = 60) -> Image.Image:
206-
"""Get a screenshot a certain number of seconds into the video."""
205+
def _get_video_screenshot(self, job: ThumbnailSourceJob, percent: int = 50) -> Image.Image:
206+
"""Get a screenshot a certain percent of frames into the video."""
207207
path = self.path / job.source_url[1:]
208-
cam = cv2.VideoCapture(path)
209-
fps = int(cam.get(cv2.CAP_PROP_FPS))
210-
currentframe = 0
211-
while True:
212-
ret, frame = cam.read()
213-
if ret:
214-
if currentframe > 0 and currentframe % (fps * seconds) == 0:
215-
# take screenshot
216-
break
217-
currentframe += 1
218-
else:
219-
raise JobNotSupportedError(job=job)
220-
cam.release()
208+
cap = cv2.VideoCapture(path)
209+
frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
210+
ss_frame = int((frames / 100) * percent)
211+
logger.debug(f"Getting video screenshot from frame {ss_frame} out of total {frames} frames in video {path} for job {job}")
212+
cap.set(cv2.CAP_PROP_POS_FRAMES, ss_frame-1)
213+
ret, frame = cap.read()
214+
if not ret:
215+
raise JobNotSupportedError(job=job)
216+
cap.release()
221217
cv2.destroyAllWindows()
222218
return frame # type: ignore[no-any-return]
223219

0 commit comments

Comments
 (0)