File size: 1,125 Bytes
aabac69
3a3c94c
 
da56cf2
8c86000
aabac69
8c86000
 
aabac69
8c86000
3a3c94c
 
 
8c86000
3a3c94c
 
 
8c86000
 
 
3a3c94c
8c86000
 
3a3c94c
8c86000
 
3a3c94c
8c86000
 
 
3a3c94c
8c86000
3a3c94c
8c86000
 
 
 
 
 
 
3a3c94c
8c86000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import random
import cv2

VIDEO_DIR = "data"  # Corrected to 'data' folder

# Ensure the folder exists
os.makedirs(VIDEO_DIR, exist_ok=True)

# List available video files
video_files = [
    os.path.join(VIDEO_DIR, file)
    for file in os.listdir(VIDEO_DIR)
    if file.lower().endswith((".mp4", ".avi", ".mov"))
]

if not video_files:
    raise FileNotFoundError(
        f"No videos found in '{VIDEO_DIR}'. Please upload .mp4, .avi, or .mov videos."
    )

current_video_idx = 0
video_capture = None

def get_random_video_frame():
    global video_capture, current_video_idx

    if video_capture is None or not video_capture.isOpened():
        selected_video = video_files[current_video_idx]
        video_capture = cv2.VideoCapture(selected_video)

    ret, frame = video_capture.read()

    if not ret:
        # Move to next video
        current_video_idx = (current_video_idx + 1) % len(video_files)
        selected_video = video_files[current_video_idx]
        video_capture.release()
        video_capture = cv2.VideoCapture(selected_video)
        ret, frame = video_capture.read()

    return frame