Fix up segmented mode to loop their video segment
This commit is contained in:
30
main.py
30
main.py
@@ -7,6 +7,8 @@ import argparse
|
|||||||
import shutil
|
import shutil
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
import subprocess
|
||||||
|
import json
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List
|
from typing import List
|
||||||
@@ -58,6 +60,7 @@ class MediaGrader:
|
|||||||
self.segment_caps = []
|
self.segment_caps = []
|
||||||
self.segment_frames = []
|
self.segment_frames = []
|
||||||
self.segment_positions = []
|
self.segment_positions = []
|
||||||
|
self.segment_end_positions = [] # Track where each segment should loop back to
|
||||||
|
|
||||||
self.timeline_visible = True
|
self.timeline_visible = True
|
||||||
|
|
||||||
@@ -527,6 +530,7 @@ class MediaGrader:
|
|||||||
self.segment_caps = [None] * self.segment_count # Keep for compatibility
|
self.segment_caps = [None] * self.segment_count # Keep for compatibility
|
||||||
self.segment_frames = [None] * self.segment_count
|
self.segment_frames = [None] * self.segment_count
|
||||||
self.segment_positions = []
|
self.segment_positions = []
|
||||||
|
self.segment_end_positions = []
|
||||||
self.segment_current_frames = [0] * self.segment_count # Track current frame for each segment
|
self.segment_current_frames = [0] * self.segment_count # Track current frame for each segment
|
||||||
|
|
||||||
# Calculate target positions
|
# Calculate target positions
|
||||||
@@ -538,13 +542,23 @@ class MediaGrader:
|
|||||||
for i in range(self.segment_count):
|
for i in range(self.segment_count):
|
||||||
if self.segment_count <= 1:
|
if self.segment_count <= 1:
|
||||||
position_ratio = 0
|
position_ratio = 0
|
||||||
|
end_ratio = 1.0
|
||||||
else:
|
else:
|
||||||
position_ratio = i / (self.segment_count - 1)
|
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))
|
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))
|
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_positions.append(start_frame)
|
||||||
|
self.segment_end_positions.append(end_frame)
|
||||||
self.segment_current_frames[i] = start_frame # Start each segment at its position
|
self.segment_current_frames[i] = start_frame # Start each segment at its position
|
||||||
|
|
||||||
print(f"Segment positions: {self.segment_positions}")
|
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
|
# Preload the entire video into memory - simple and fast
|
||||||
print("Preloading entire video into memory...")
|
print("Preloading entire video into memory...")
|
||||||
@@ -600,23 +614,33 @@ class MediaGrader:
|
|||||||
self.segment_caps = []
|
self.segment_caps = []
|
||||||
self.segment_frames = []
|
self.segment_frames = []
|
||||||
self.segment_positions = []
|
self.segment_positions = []
|
||||||
|
self.segment_end_positions = []
|
||||||
if hasattr(self, 'video_frame_cache'):
|
if hasattr(self, 'video_frame_cache'):
|
||||||
self.video_frame_cache = []
|
self.video_frame_cache = []
|
||||||
if hasattr(self, 'segment_current_frames'):
|
if hasattr(self, 'segment_current_frames'):
|
||||||
self.segment_current_frames = []
|
self.segment_current_frames = []
|
||||||
|
|
||||||
def update_segment_frames(self):
|
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'):
|
if not self.multi_segment_mode or not self.segment_frames or not hasattr(self, 'video_frame_cache'):
|
||||||
return
|
return
|
||||||
|
|
||||||
for i in range(len(self.segment_frames)):
|
for i in range(len(self.segment_frames)):
|
||||||
if self.segment_frames[i] is not None and self.video_frame_cache:
|
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
|
self.segment_current_frames[i] += 1
|
||||||
|
|
||||||
if self.segment_current_frames[i] >= len(self.video_frame_cache):
|
# Get the segment boundaries
|
||||||
self.segment_current_frames[i] = 0
|
start_frame = self.segment_positions[i]
|
||||||
|
end_frame = self.segment_end_positions[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
|
# Direct reference - no copy needed for display
|
||||||
self.segment_frames[i] = self.video_frame_cache[self.segment_current_frames[i]]
|
self.segment_frames[i] = self.video_frame_cache[self.segment_current_frames[i]]
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user