利用 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 WaveSpeed paid generation durations
Rudy Celeklia04f6920802f
diff --git a/app/services/material.py b/app/services/material.pyindex 807d55b0..c6f80d01 100644--- a/app/services/material.py+++ b/app/services/material.py@@ -2045,12 +2045,34 @@ def _download_videos_wavespeed_on_demand("""video_paths: List[str] = []material_sources: list[dict[str, Any]] = []++ try:+ required_duration = float(audio_duration)+ except (TypeError, ValueError, OverflowError) as exc:+ raise ValueError("WaveSpeed audio duration must be finite") from exc+ if not math.isfinite(required_duration):+ raise ValueError("WaveSpeed audio duration must be finite")+ if required_duration <= 0:+ logger.warning(+ "skip WaveSpeed paid generation because required audio duration "+ f"is not positive: duration={required_duration}"+ )+ _persist_material_sources(task_id, material_sources)+ return video_paths++ try:+ clip_duration = int(max_clip_duration)+ except (TypeError, ValueError, OverflowError) as exc:+ raise ValueError("WaveSpeed clip duration must be a positive integer") from exc+ if clip_duration <= 0:+ raise ValueError("WaveSpeed clip duration must be a positive integer")+total_duration = 0.0for search_term in search_terms:try:video_items = generate_videos_wavespeed(search_term=search_term,- minimum_duration=max_clip_duration,+ minimum_duration=clip_duration,video_aspect=video_aspect,)except WaveSpeedUnconfirmedTaskError as e:@@ -2091,16 +2113,16 @@ def _download_videos_wavespeed_on_demand(f"provider={item.provider}, "f"error={type(source_error).__name__}, detail={source_error}")- total_duration += min(max_clip_duration, item.duration)+ total_duration += min(clip_duration, item.duration)# 用 >= 判断:累计时长恰好等于所需时长时已经够用,再生成会# 多付一次费用。内外两处判断必须保持同一语义。- if total_duration >= audio_duration:+ if total_duration >= required_duration:break- if total_duration >= audio_duration:+ if total_duration >= required_duration:logger.info("generated materials cover the required duration, stop "f"generating more clips: generated={total_duration:.1f}s, "- f"required={audio_duration:.1f}s"+ f"required={required_duration:.1f}s")breaklogger.success(f"generated and downloaded {len(video_paths)} videos")diff --git a/test/services/test_material.py b/test/services/test_material.pyindex 4fe0aca0..f35d9e07 100644--- a/test/services/test_material.py+++ b/test/services/test_material.py@@ -2003,6 +2003,50 @@ class TestWaveSpeedProvider(unittest.TestCase):self.assertEqual(generate.call_count, 2)self.assertEqual(result, ["/tmp/1.mp4", "/tmp/2.mp4"])+ def test_download_videos_wavespeed_rejects_nonfinite_duration_before_submission(self):+ """NaN/Infinity must not buy a video for every script keyword."""+ for duration in (float("nan"), float("inf"), -float("inf")):+ with self.subTest(duration=duration):+ with patch("app.services.material.generate_videos_wavespeed") as generate:+ with self.assertRaisesRegex(ValueError, "finite"):+ material.download_videos(+ task_id="test-wavespeed-invalid-duration",+ search_terms=["term-1", "term-2"],+ source="wavespeed",+ audio_duration=duration,+ max_clip_duration=5,+ )+ generate.assert_not_called()++ def test_download_videos_wavespeed_rejects_nonpositive_clip_duration(self):+ """A zero clip duration cannot advance paid coverage."""+ with patch("app.services.material.generate_videos_wavespeed") as generate:+ with self.assertRaisesRegex(ValueError, "clip duration"):+ material.download_videos(+ task_id="test-wavespeed-invalid-clip",+ search_terms=["term-1", "term-2"],+ source="wavespeed",+ audio_duration=10,+ max_clip_duration=0,+ )+ generate.assert_not_called()++ def test_download_videos_wavespeed_skips_nonpositive_audio_duration(self):+ """An empty narration must not start a paid generation request."""+ with (+ patch("app.services.material.generate_videos_wavespeed") as generate,+ patch("app.services.material._persist_material_sources"),+ ):+ result = material.download_videos(+ task_id="test-wavespeed-empty-audio",+ search_terms=["term-1", "term-2"],+ source="wavespeed",+ audio_duration=0,+ max_clip_duration=5,+ )+ self.assertEqual(result, [])+ generate.assert_not_called()+def test_download_videos_wavespeed_skips_failed_segment_and_continues(self):"""单个片段生成失败(空结果)时跳过该关键词,继续为后续片段生成。"""generated = {