Fix up segmented mode to loop their video segment

This commit is contained in:
2025-09-08 17:45:43 +02:00
parent a6886a8ab8
commit fc0aa1317b

34
main.py
View File

@@ -7,6 +7,8 @@ import argparse
import shutil
import time
import threading
import subprocess
import json
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import List
@@ -58,6 +60,7 @@ class MediaGrader:
self.segment_caps = []
self.segment_frames = []
self.segment_positions = []
self.segment_end_positions = [] # Track where each segment should loop back to
self.timeline_visible = True
@@ -527,6 +530,7 @@ class MediaGrader:
self.segment_caps = [None] * self.segment_count # Keep for compatibility
self.segment_frames = [None] * self.segment_count
self.segment_positions = []
self.segment_end_positions = []
self.segment_current_frames = [0] * self.segment_count # Track current frame for each segment
# Calculate target positions
@@ -538,13 +542,23 @@ class MediaGrader:
for i in range(self.segment_count):
if self.segment_count <= 1:
position_ratio = 0
end_ratio = 1.0
else:
position_ratio = i / (self.segment_count - 1)
end_ratio = (i + 1) / (self.segment_count - 1) if i < self.segment_count - 1 else 1.0
start_frame = int(position_ratio * (safe_frame_count - 1))
end_frame = int(end_ratio * (safe_frame_count - 1))
start_frame = max(0, min(start_frame, safe_frame_count - 1))
end_frame = max(start_frame + 1, min(end_frame, safe_frame_count - 1)) # Ensure at least 1 frame per segment
self.segment_positions.append(start_frame)
self.segment_end_positions.append(end_frame)
self.segment_current_frames[i] = start_frame # Start each segment at its position
print(f"Segment positions: {self.segment_positions}")
print(f"Segment end positions: {self.segment_end_positions}")
# Preload the entire video into memory - simple and fast
print("Preloading entire video into memory...")
@@ -600,25 +614,35 @@ class MediaGrader:
self.segment_caps = []
self.segment_frames = []
self.segment_positions = []
self.segment_end_positions = []
if hasattr(self, 'video_frame_cache'):
self.video_frame_cache = []
if hasattr(self, 'segment_current_frames'):
self.segment_current_frames = []
def update_segment_frames(self):
"""Update frames for segments using the preloaded video array - smooth playback!"""
"""Update frames for segments - each segment loops within its own range"""
if not self.multi_segment_mode or not self.segment_frames or not hasattr(self, 'video_frame_cache'):
return
for i in range(len(self.segment_frames)):
if self.segment_frames[i] is not None and self.video_frame_cache:
# Advance to next frame in this segment
self.segment_current_frames[i] += 1
if self.segment_current_frames[i] >= len(self.video_frame_cache):
self.segment_current_frames[i] = 0
# Get the segment boundaries
start_frame = self.segment_positions[i]
end_frame = self.segment_end_positions[i]
# Direct reference - no copy needed for display
self.segment_frames[i] = self.video_frame_cache[self.segment_current_frames[i]]
# Loop within the segment bounds
if self.segment_current_frames[i] > end_frame:
# Loop back to start of segment
self.segment_current_frames[i] = start_frame
# Ensure we don't go beyond the video cache
if self.segment_current_frames[i] < len(self.video_frame_cache):
# Direct reference - no copy needed for display
self.segment_frames[i] = self.video_frame_cache[self.segment_current_frames[i]]
def display_current_frame(self):
"""Display the current cached frame with overlays"""