Refactor thumbnail caching in ProjectView: store original thumbnails by video path instead of size, allowing for on-demand resizing. This change optimizes thumbnail generation and improves performance by reducing redundant processing.

This commit is contained in:
2025-09-16 10:05:09 +02:00
parent b9cf9f0125
commit c8dfcca954

View File

@@ -119,12 +119,13 @@ class ProjectView:
if size is None: if size is None:
size = self.THUMBNAIL_SIZE size = self.THUMBNAIL_SIZE
# Use a cache key that includes the size # Cache the original thumbnail by video path only (not size)
cache_key = (video_path, size) if video_path in self.thumbnails:
if cache_key in self.thumbnails: original_thumbnail = self.thumbnails[video_path]
return self.thumbnails[cache_key] # Resize the cached thumbnail to the requested size
return cv2.resize(original_thumbnail, size)
# Generate thumbnail on demand # Generate original thumbnail on demand (only once per video)
try: try:
cap = cv2.VideoCapture(str(video_path)) cap = cv2.VideoCapture(str(video_path))
if cap.isOpened(): if cap.isOpened():
@@ -134,10 +135,12 @@ class ProjectView:
cap.set(cv2.CAP_PROP_POS_FRAMES, middle_frame) cap.set(cv2.CAP_PROP_POS_FRAMES, middle_frame)
ret, frame = cap.read() ret, frame = cap.read()
if ret: if ret:
thumbnail = cv2.resize(frame, size) # Store original thumbnail at original size
self.thumbnails[cache_key] = thumbnail original_thumbnail = cv2.resize(frame, self.THUMBNAIL_SIZE)
self.thumbnails[video_path] = original_thumbnail
cap.release() cap.release()
return thumbnail # Return resized version
return cv2.resize(original_thumbnail, size)
cap.release() cap.release()
except Exception as e: except Exception as e:
print(f"Error generating thumbnail for {video_path.name}: {e}") print(f"Error generating thumbnail for {video_path.name}: {e}")