File size: 1,183 Bytes
58ac08a 6557d31 58ac08a 6557d31 58ac08a 6557d31 58ac08a 6557d31 58ac08a 6557d31 58ac08a 6557d31 58ac08a 6557d31 |
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 |
import cv2
def detect_event_segments(video_path):
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
segments = []
segment_duration = 5 # seconds per segment
frames_per_segment = int(segment_duration * fps)
frame_number = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_number += 1
if frame_number % frames_per_segment == 1:
segment_start_sec = (frame_number - 1) / fps
segment_end_sec = (frame_number + frames_per_segment - 2) / fps
segments.append({
"start_sec": segment_start_sec,
"end_sec": segment_end_sec,
"frames": [] # This can hold keyframes later if needed
})
if segments:
segments[-1]["frames"].append(frame)
cap.release()
# Final cleanup to make sure segment end matches actual video length if needed
if segments:
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
total_duration = total_frames / fps
segments[-1]["end_sec"] = min(segments[-1]["end_sec"], total_duration)
return segments
|