Spaces:
Sleeping
Sleeping
File size: 1,175 Bytes
2ab6e0a |
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 |
from typing import List, Tuple
import cv2
import numpy as np
def extract_frames(video_path: str) -> Tuple[List[np.ndarray], float, int, int]:
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
raise ValueError("Unable to open video.")
fps = cap.get(cv2.CAP_PROP_FPS) or 0.0
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
frames: List[np.ndarray] = []
success, frame = cap.read()
while success:
frames.append(frame)
success, frame = cap.read()
cap.release()
if not frames:
raise ValueError("Video decode produced zero frames.")
return frames, fps, width, height
def write_video(frames: List[np.ndarray], output_path: str, fps: float, width: int, height: int) -> None:
if not frames:
raise ValueError("No frames available for writing.")
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
writer = cv2.VideoWriter(output_path, fourcc, fps or 1.0, (width, height))
if not writer.isOpened():
raise ValueError("Failed to open VideoWriter.")
for frame in frames:
writer.write(frame)
writer.release()
|