From 2aec240128ea4148428c75bd4452d7a403683041 Mon Sep 17 00:00:00 2001 From: msramalho <19508417+msramalho@users.noreply.github.com> Date: Tue, 17 Jun 2025 20:28:20 +0100 Subject: [PATCH] thumbnail enricher always run probe by default --- .../thumbnail_enricher/thumbnail_enricher.py | 22 ++++++++++--------- tests/enrichers/test_thumbnail_enricher.py | 15 ++++++++----- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/auto_archiver/modules/thumbnail_enricher/thumbnail_enricher.py b/src/auto_archiver/modules/thumbnail_enricher/thumbnail_enricher.py index 1543cec..81b9ce3 100644 --- a/src/auto_archiver/modules/thumbnail_enricher/thumbnail_enricher.py +++ b/src/auto_archiver/modules/thumbnail_enricher/thumbnail_enricher.py @@ -35,16 +35,18 @@ class ThumbnailEnricher(Enricher): logger.debug(f"generating thumbnails for {m.filename}") duration = m.get("duration") - if duration is None: - try: - probe = ffmpeg.probe(m.filename) - duration = float( - next(stream for stream in probe["streams"] if stream["codec_type"] == "video")["duration"] - ) - to_enrich.media[m_id].set("duration", duration) - except Exception as e: - logger.error(f"error getting duration of video {m.filename}: {e}") - return + try: + probe = ffmpeg.probe(m.filename) + duration = float( + next(stream for stream in probe["streams"] if stream["codec_type"] == "video")["duration"] + ) + to_enrich.media[m_id].set("duration", duration) + except Exception as e: + logger.warning(f"failed to get duration with FFMPEG from {m.filename}: {e}") + + if not duration or type(duration) not in [float, int] or duration <= 0: + logger.warning(f"cannot generate thumbnails for {m.filename} without valid duration") + continue num_thumbs = int(min(max(1, (duration / 60) * self.thumbnails_per_minute), self.max_thumbnails)) timestamps = [duration / (num_thumbs + 1) * i for i in range(1, num_thumbs + 1)] diff --git a/tests/enrichers/test_thumbnail_enricher.py b/tests/enrichers/test_thumbnail_enricher.py index fdc28b7..41aac66 100644 --- a/tests/enrichers/test_thumbnail_enricher.py +++ b/tests/enrichers/test_thumbnail_enricher.py @@ -74,12 +74,12 @@ def test_enrich_thumbnail_limits( def test_enrich_handles_probe_failure(thumbnail_enricher, metadata_with_video, mocker): mocker.patch("ffmpeg.probe", side_effect=Exception("Probe error")) mocker.patch("os.makedirs") - mock_logger = mocker.patch("loguru.logger.error") + mock_logger = mocker.patch("loguru.logger.warning") mocker.patch.object(Media, "is_video", return_value=True) thumbnail_enricher.enrich(metadata_with_video) # Ensure error was logged - mock_logger.assert_called_with("error getting duration of video video.mp4: Probe error") + mock_logger.assert_called_with("cannot generate thumbnails for video.mp4 without valid duration") # Ensure no thumbnails were created thumbnails = metadata_with_video.media[0].get("thumbnails") assert thumbnails is None @@ -126,11 +126,14 @@ def test_enrich_handles_short_video( assert len(thumbnails) == expected_count -def test_uses_existing_duration(thumbnail_enricher, metadata_with_video, mock_ffmpeg_environment): - metadata_with_video.media[0].set("duration", 60) +def test_uses_existing_duration_on_exception(thumbnail_enricher, metadata_with_video, mock_ffmpeg_environment, mocker): + mock_logger = mocker.patch("loguru.logger.warning") + mock_probe = mocker.patch("ffmpeg.probe", side_effect=Exception("New probe error")) + metadata_with_video.media[0].set("duration", 3) thumbnail_enricher.enrich(metadata_with_video) - mock_ffmpeg_environment["mock_probe"].assert_not_called() - assert mock_ffmpeg_environment["mock_output"].run.call_count == 4 + mock_probe.assert_called_once() + mock_logger.assert_called_with("failed to get duration with FFMPEG from video.mp4: New probe error") + assert mock_ffmpeg_environment["mock_output"].run.call_count == 3 def test_enrich_metadata_structure(thumbnail_enricher, metadata_with_video, mock_ffmpeg_environment, mocker):