Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(cache): reject unusable material durations before conversion

Rudy Celeklie8cb38c4b2d0
diff --git a/app/services/material_cache.py b/app/services/material_cache.py
index 9da09386..18ea3e83 100644
--- a/app/services/material_cache.py
+++ b/app/services/material_cache.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import hashlib
import json
+import math
import os
import re
import tempfile
@@ -277,7 +278,8 @@ def load_material_search_cache(
or not item_url
or isinstance(item_duration, bool)
or not isinstance(item_duration, (int, float))
- or item_duration <= 0
+ or (isinstance(item_duration, float) and not math.isfinite(item_duration))
+ or item_duration < 1
or not isinstance(source_info, dict)
or not source_info
):
diff --git a/test/services/test_material_cache_duration.py b/test/services/test_material_cache_duration.py
new file mode 100644
index 00000000..dd5325da
--- /dev/null
+++ b/test/services/test_material_cache_duration.py
@@ -0,0 +1,31 @@
+from pathlib import Path
+from tempfile import TemporaryDirectory
+from unittest.mock import patch
+
+import pytest
+
+from app.models.schema import VideoAspect
+from app.services import material_cache
+
+
[email protected]('duration', ['1e309', 'Infinity', '-Infinity', 'NaN', '0.5'])
+def test_corrupted_duration_is_a_cache_miss(duration):
+ with TemporaryDirectory() as directory, patch.object(material_cache.utils, 'storage_dir', return_value=directory):
+ cache_path = material_cache._cache_path('pixabay', 'nature', 5, VideoAspect.portrait)
+ cache_path.write_text(
+ '{"version":2,"items":[{"provider":"pixabay","url":"https://example.com/clip.mp4",'
+ '"source_info":{"provider":"pixabay"},"duration":' + duration + '}]}'
+ )
+ assert material_cache.load_material_search_cache('pixabay', 'nature', 5, VideoAspect.portrait) is None
+ assert not Path(cache_path).exists()
+
+
+def test_legacy_finite_float_duration_remains_usable():
+ with TemporaryDirectory() as directory, patch.object(material_cache.utils, 'storage_dir', return_value=directory):
+ cache_path = material_cache._cache_path('pixabay', 'nature', 5, VideoAspect.portrait)
+ cache_path.write_text(
+ '{"version":2,"items":[{"provider":"pixabay","url":"https://example.com/clip.mp4",'
+ '"source_info":{"provider":"pixabay"},"duration":12.5}]}'
+ )
+ items = material_cache.load_material_search_cache('pixabay', 'nature', 5, VideoAspect.portrait)
+ assert items[0].duration == 12