利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(material): validate cached downloads (#1574)
Rudy Mizrahi Celeklieeab6cfe80cc
diff --git a/app/services/material.py b/app/services/material.pyindex da2492c3..eeaec3c5 100644--- a/app/services/material.py+++ b/app/services/material.py@@ -22,6 +22,7 @@ from app.config import configfrom app.models.schema import MaterialInfo, VideoAspect, VideoConcatModefrom app.services import (material_cache,+ material_upload,metaso_minimax,muapi,ofox,@@ -1221,6 +1222,10 @@ def save_video(video_url: str, save_dir: str = "") -> str:clip = VideoFileClip(temp_path)duration = clip.durationfps = clip.fps+ # Container duration and an initial frame do not prove the rest of+ # a faststart MP4 arrived. Validate every video packet before a+ # nonempty cache entry becomes reusable by later tasks.+ material_upload._validate_video(temp_path)if not (duration > 0 and fps > 0):logger.warning(f"invalid video file: {temp_path} => invalid duration or fps")return ""diff --git a/test/services/test_download_complete_decode.py b/test/services/test_download_complete_decode.pynew file mode 100644index 00000000..4cdbcfd5--- /dev/null+++ b/test/services/test_download_complete_decode.py@@ -0,0 +1,50 @@+import shutil+import subprocess+import threading+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer++import pytest++from app.services import material+++def test_partial_faststart_video_does_not_poison_download_cache(tmp_path):+ if not shutil.which("ffmpeg"):+ pytest.skip("native FFmpeg required")+ footage = tmp_path / "source.mp4"+ subprocess.run(["ffmpeg", "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=128x128:rate=20:duration=2", "-c:v", "libx264", "-preset", "ultrafast", "-movflags", "+faststart", str(footage)], check=True)+ complete = footage.read_bytes()+ payloads = [complete[:len(complete) // 2], complete]+ requests = []++ class Handler(BaseHTTPRequestHandler):+ def log_message(self, *args):+ pass++ def do_GET(self):+ requests.append(self.path)+ data = payloads[min(len(requests) - 1, 1)]+ self.send_response(200)+ # This is a completed transport response containing a truncated+ # media object, not a Content-Length/socket truncation.+ self.send_header("Content-Length", str(len(data)))+ self.end_headers()+ self.wfile.write(data)++ server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)+ thread = threading.Thread(target=server.serve_forever, daemon=True)+ thread.start()+ cache = tmp_path / "cache"+ try:+ url = f"http://127.0.0.1:{server.server_port}/clip.mp4"+ assert material.save_video(url, str(cache)) == ""+ assert list(cache.iterdir()) == []+ saved = material.save_video(url, str(cache))+ assert saved+ assert material.save_video(url, str(cache)) == saved+ assert len(requests) == 2+ assert len(list(cache.iterdir())) == 1+ finally:+ server.shutdown()+ server.server_close()+ thread.join(timeout=5)diff --git a/test/services/test_download_directory_race.py b/test/services/test_download_directory_race.pyindex a727ca6d..7b528ff2 100644--- a/test/services/test_download_directory_race.py+++ b/test/services/test_download_directory_race.py@@ -21,7 +21,8 @@ def test_parallel_first_downloads_share_new_directory(tmp_path):return MagicMock(duration=3.0, fps=30)with (patch.object(material.os, "makedirs", side_effect=makedirs),patch.object(material.requests, "get", return_value=response),- patch.object(material, "VideoFileClip", side_effect=decode)):+ patch.object(material, "VideoFileClip", side_effect=decode),+ patch.object(material.material_upload, "_validate_video")):with ThreadPoolExecutor(max_workers=2) as pool:results = list(pool.map(lambda n: material.save_video(f"https://example.test/{n}.mp4", str(destination)), [1, 2]))assert len(set(results)) == 2diff --git a/test/services/test_material.py b/test/services/test_material.pyindex 6d3fb4a8..cc8d2cbc 100644--- a/test/services/test_material.py+++ b/test/services/test_material.py@@ -751,7 +751,7 @@ class TestMaterialTlsVerification(unittest.TestCase):with tempfile.TemporaryDirectory() as temp_dir:with patch("app.services.material.requests.get", return_value=fake_response- ) as get, patch("app.services.material.VideoFileClip", FakeVideoFileClip):+ ) as get, patch("app.services.material.VideoFileClip", FakeVideoFileClip), patch("app.services.material.material_upload._validate_video"):video_path = material.save_video("https://example.com/video.mp4?token=abc", save_dir=temp_dir)@@ -797,7 +797,7 @@ class TestMaterialTlsVerification(unittest.TestCase):with tempfile.TemporaryDirectory() as temp_dir:with patch("app.services.material.requests.get", return_value=response- ) as get, patch("app.services.material.VideoFileClip", FakeVideoFileClip):+ ) as get, patch("app.services.material.VideoFileClip", FakeVideoFileClip), patch("app.services.material.material_upload._validate_video"):video_path = material.save_video("https://example.com/large.mp4", save_dir=temp_dir)@@ -834,7 +834,7 @@ class TestMaterialTlsVerification(unittest.TestCase):"app.services.material.requests.get",return_value=ChunkedResponse(),),- patch("app.services.material.VideoFileClip", FakeVideoFileClip),+ patch("app.services.material.VideoFileClip", FakeVideoFileClip), patch("app.services.material.material_upload._validate_video"),patch.object(material.logger, "info") as info,):video_path = material.save_video(@@ -904,7 +904,7 @@ class TestMaterialTlsVerification(unittest.TestCase):_FakeVideoDownloadResponse(b"first generated scene"),_FakeVideoDownloadResponse(b"second generated scene"),],- ) as get, patch("app.services.material.VideoFileClip", FakeVideoFileClip):+ ) as get, patch("app.services.material.VideoFileClip", FakeVideoFileClip), patch("app.services.material.material_upload._validate_video"):first_path = material.save_video(first_url, save_dir=temp_dir)second_path = material.save_video(second_url, save_dir=temp_dir)cached_path = material.save_video(first_url, save_dir=temp_dir)@@ -998,7 +998,7 @@ class TestMaterialTlsVerification(unittest.TestCase):_FakeVideoDownloadResponse(b"broken"),_FakeVideoDownloadResponse(b"valid"),],- ) as get, patch("app.services.material.VideoFileClip", FakeVideoFileClip):+ ) as get, patch("app.services.material.VideoFileClip", FakeVideoFileClip), patch("app.services.material.material_upload._validate_video"):self.assertEqual(material.save_video(url, save_dir=temp_dir), "")self.assertFalse((Path(temp_dir) / cached_name).exists())self.assertEqual(@@ -1024,7 +1024,7 @@ class TestMaterialTlsVerification(unittest.TestCase):with patch("app.services.material.requests.get",return_value=_FakeVideoDownloadResponse(b"bad metadata"),- ), patch("app.services.material.VideoFileClip", FakeVideoFileClip):+ ), patch("app.services.material.VideoFileClip", FakeVideoFileClip), patch("app.services.material.material_upload._validate_video"):self.assertEqual(material.save_video("https://example.com/nan.mp4", save_dir=temp_dir),"",