File size: 888 Bytes
b23251f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import os
import tempfile
import logging
from typing import List

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def extract_frames(video_path: str, skip: int = 1) -> List:
    """

    Extract frames from a video.



    Args:

        video_path (str): Path to the video file.

        skip (int): Number of frames to skip between extractions.



    Returns:

        List of BGR frames as numpy arrays.

    """
    logger.info(f"Extracting frames from video: {video_path}")
    frames = []
    cap = cv2.VideoCapture(video_path)
    frame_count = 0
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        if frame_count % skip == 0:
            frames.append(frame)
        frame_count += 1
    cap.release()
    logger.info(f"Extracted {len(frames)} frames")
    return frames