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:
@@ -119,12 +119,13 @@ class ProjectView:
|
||||
if size is None:
|
||||
size = self.THUMBNAIL_SIZE
|
||||
|
||||
# Use a cache key that includes the size
|
||||
cache_key = (video_path, size)
|
||||
if cache_key in self.thumbnails:
|
||||
return self.thumbnails[cache_key]
|
||||
# Cache the original thumbnail by video path only (not size)
|
||||
if video_path in self.thumbnails:
|
||||
original_thumbnail = self.thumbnails[video_path]
|
||||
# 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:
|
||||
cap = cv2.VideoCapture(str(video_path))
|
||||
if cap.isOpened():
|
||||
@@ -134,10 +135,12 @@ class ProjectView:
|
||||
cap.set(cv2.CAP_PROP_POS_FRAMES, middle_frame)
|
||||
ret, frame = cap.read()
|
||||
if ret:
|
||||
thumbnail = cv2.resize(frame, size)
|
||||
self.thumbnails[cache_key] = thumbnail
|
||||
# Store original thumbnail at original size
|
||||
original_thumbnail = cv2.resize(frame, self.THUMBNAIL_SIZE)
|
||||
self.thumbnails[video_path] = original_thumbnail
|
||||
cap.release()
|
||||
return thumbnail
|
||||
# Return resized version
|
||||
return cv2.resize(original_thumbnail, size)
|
||||
cap.release()
|
||||
except Exception as e:
|
||||
print(f"Error generating thumbnail for {video_path.name}: {e}")
|
||||
|
Reference in New Issue
Block a user