Skip to content

Commit a7627e8

Browse files
committed
refactor: extract video size extraction to controller
1 parent c1e7f7a commit a7627e8

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

src/tagstudio/qt/controller/widgets/preview/preview_thumb_controller.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
from pathlib import Path
66
from typing import TYPE_CHECKING
77

8+
import cv2
89
import rawpy
910
import structlog
1011
from PIL import Image, UnidentifiedImageError
1112
from PIL.Image import DecompressionBombError
13+
from PySide6.QtCore import QSize
1214

1315
from tagstudio.core.library.alchemy.library import Library
1416
from tagstudio.core.media_types import MediaCategories
@@ -21,6 +23,7 @@
2123
from tagstudio.qt.ts_qt import QtDriver
2224

2325
logger = structlog.get_logger(__name__)
26+
Image.MAX_IMAGE_PIXELS = None
2427

2528

2629
class PreviewThumb(PreviewThumbView):
@@ -93,6 +96,13 @@ def __get_gif_data(self, filepath: Path) -> tuple[bytes, tuple[int, int]] | None
9396
logger.error("[PreviewThumb] Could not load animated image", filepath=filepath, error=e)
9497
return None
9598

99+
def __get_video_res(self, filepath: str) -> tuple[bool, QSize]:
100+
video = cv2.VideoCapture(filepath, cv2.CAP_FFMPEG)
101+
success, frame = video.read()
102+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
103+
image = Image.fromarray(frame)
104+
return (success, QSize(image.width, image.height))
105+
96106
def display_file(self, filepath: Path) -> FileAttributeData:
97107
"""Render a single file preview."""
98108
self.__current_file = filepath
@@ -103,7 +113,15 @@ def display_file(self, filepath: Path) -> FileAttributeData:
103113
if MediaCategories.VIDEO_TYPES.contains(ext, mime_fallback=True) and is_readable_video(
104114
filepath
105115
):
106-
return self._display_video(filepath)
116+
size: QSize | None = None
117+
try:
118+
success, size = self.__get_video_res(str(filepath))
119+
if not success:
120+
size = None
121+
except cv2.error as e:
122+
logger.error("[PreviewThumb] Could not play video", filepath=filepath, error=e)
123+
124+
return self._display_video(filepath, size)
107125
# Audio
108126
elif MediaCategories.AUDIO_TYPES.contains(ext, mime_fallback=True):
109127
return self._display_audio(filepath)

src/tagstudio/qt/view/widgets/preview/preview_thumb_view.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
from pathlib import Path
66
from typing import TYPE_CHECKING, override
77

8-
import cv2
98
import structlog
10-
from PIL import Image
119
from PySide6.QtCore import QBuffer, QByteArray, QSize, Qt
1210
from PySide6.QtGui import QAction, QMovie, QPixmap, QResizeEvent
1311
from PySide6.QtWidgets import QHBoxLayout, QLabel, QPushButton, QStackedLayout, QWidget
@@ -25,7 +23,6 @@
2523
from tagstudio.qt.ts_qt import QtDriver
2624

2725
logger = structlog.get_logger(__name__)
28-
Image.MAX_IMAGE_PIXELS = None
2926

3027

3128
class PreviewThumbView(QWidget):
@@ -234,13 +231,6 @@ def __render_thumb(self, filepath: Path) -> None:
234231
update_on_ratio_change=True,
235232
)
236233

237-
def __get_video_res(self, filepath: str) -> tuple[bool, QSize]:
238-
video = cv2.VideoCapture(filepath, cv2.CAP_FFMPEG)
239-
success, frame = video.read()
240-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
241-
image = Image.fromarray(frame)
242-
return (success, QSize(image.width, image.height))
243-
244234
def __update_media_player(self, filepath: Path) -> int:
245235
"""Display either audio or video.
246236
@@ -249,25 +239,21 @@ def __update_media_player(self, filepath: Path) -> int:
249239
self.__media_player.play(filepath)
250240
return self.__media_player.player.duration() * 1000
251241

252-
def _display_video(self, filepath: Path) -> FileAttributeData:
242+
def _display_video(self, filepath: Path, size: QSize | None) -> FileAttributeData:
253243
self.__switch_preview(MediaType.VIDEO)
254244
stats = FileAttributeData(duration=self.__update_media_player(filepath))
255245

256-
try:
257-
success, size = self.__get_video_res(str(filepath))
258-
if success:
259-
stats.width = size.width()
260-
stats.height = size.height()
261-
262-
self.__image_ratio = stats.width / stats.height
263-
self.resizeEvent(
264-
QResizeEvent(
265-
QSize(stats.width, stats.height),
266-
QSize(stats.width, stats.height),
267-
)
246+
if size is not None:
247+
stats.width = size.width()
248+
stats.height = size.height()
249+
250+
self.__image_ratio = stats.width / stats.height
251+
self.resizeEvent(
252+
QResizeEvent(
253+
QSize(stats.width, stats.height),
254+
QSize(stats.width, stats.height),
268255
)
269-
except cv2.error as e:
270-
logger.error("[PreviewThumb] Could not play video", filepath=filepath, error=e)
256+
)
271257

272258
return stats
273259

0 commit comments

Comments
 (0)