refactor(main.py): optimize frame advancement logic to handle playback speed and improve readability

This commit is contained in:
2025-09-04 21:04:18 +02:00
parent 6c8a5dad8e
commit 007e371db6

View File

@@ -311,22 +311,34 @@ class VideoEditor:
self.load_current_frame() self.load_current_frame()
def advance_frame(self) -> bool: def advance_frame(self) -> bool:
"""Advance to next frame - optimized to avoid seeking""" """Advance to next frame - optimized to avoid seeking, handles playback speed"""
if not self.is_playing: if not self.is_playing:
return True return True
self.current_frame += 1 # Calculate how many frames to advance based on speed
if self.current_frame >= self.total_frames: # For speeds > 1.0, we skip frames. For speeds < 1.0, we delay in main loop
self.current_frame = 0 # Loop - this will require a seek frames_to_advance = max(1, int(self.playback_speed))
new_frame = self.current_frame + frames_to_advance
if new_frame >= self.total_frames:
new_frame = 0 # Loop - this will require a seek
self.current_frame = new_frame
return self.load_current_frame() return self.load_current_frame()
# For sequential playback, just read the next frame without seeking # For sequential playback at normal speed, just read the next frame without seeking
if frames_to_advance == 1:
ret, frame = self.cap.read() ret, frame = self.cap.read()
if ret: if ret:
self.current_frame = new_frame
self.current_display_frame = frame self.current_display_frame = frame
return True return True
else: else:
# If sequential read failed, fall back to seeking (might be end of file or codec issue) # If sequential read failed, fall back to seeking
self.current_frame = new_frame
return self.load_current_frame()
else:
# For speed > 1.0, we need to seek to skip frames
self.current_frame = new_frame
return self.load_current_frame() return self.load_current_frame()
def apply_crop_zoom_and_rotation(self, frame): def apply_crop_zoom_and_rotation(self, frame):