From 007e371db6cf8690e8ab25e19a87159380ebacd1 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Thu, 4 Sep 2025 21:04:18 +0200 Subject: [PATCH] refactor(main.py): optimize frame advancement logic to handle playback speed and improve readability --- croppa/main.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/croppa/main.py b/croppa/main.py index baf1540..beaede8 100644 --- a/croppa/main.py +++ b/croppa/main.py @@ -311,22 +311,34 @@ class VideoEditor: self.load_current_frame() 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: return True - self.current_frame += 1 - if self.current_frame >= self.total_frames: - self.current_frame = 0 # Loop - this will require a seek + # Calculate how many frames to advance based on speed + # For speeds > 1.0, we skip frames. For speeds < 1.0, we delay in main loop + 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() - # For sequential playback, just read the next frame without seeking - ret, frame = self.cap.read() - if ret: - self.current_display_frame = frame - return True + # For sequential playback at normal speed, just read the next frame without seeking + if frames_to_advance == 1: + ret, frame = self.cap.read() + if ret: + self.current_frame = new_frame + self.current_display_frame = frame + return True + else: + # If sequential read failed, fall back to seeking + self.current_frame = new_frame + return self.load_current_frame() else: - # If sequential read failed, fall back to seeking (might be end of file or codec issue) + # For speed > 1.0, we need to seek to skip frames + self.current_frame = new_frame return self.load_current_frame() def apply_crop_zoom_and_rotation(self, frame):