Skip to content

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

fix: add video duration safety buffer

Sushanth0128a6fcb7f6c05
diff --git a/app/services/video.py b/app/services/video.py
index 9102e32f..2937361b 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -314,7 +314,11 @@ def _format_ffmpeg_concat_path(file_path: str) -> str:
def concat_video_clips_with_ffmpeg(
- clip_files: List[str], output_file: str, threads: int, output_dir: str
+ clip_files: List[str],
+ output_file: str,
+ threads: int,
+ output_dir: str,
+ max_duration: float | None = None,
):
concat_list_file = os.path.join(output_dir, "ffmpeg-concat-list.txt")
with open(concat_list_file, "w", encoding="utf-8") as fp:
@@ -322,7 +326,7 @@ def concat_video_clips_with_ffmpeg(
fp.write(f"file '{_format_ffmpeg_concat_path(clip_file)}'\n")
def build_command(codec: str) -> list[str]:
- return [
+ command = [
utils.get_ffmpeg_binary(),
"-y",
"-f",
@@ -337,8 +341,11 @@ def concat_video_clips_with_ffmpeg(
str(threads or 2),
"-pix_fmt",
"yuv420p",
- output_file,
]
+ if max_duration is not None and max_duration > 0:
+ command.extend(["-t", f"{max_duration:.3f}"])
+ command.append(output_file)
+ return command
def run_concat(codec: str):
command = build_command(codec)
@@ -719,14 +726,6 @@ def combine_videos(
logger.warning("no clips available for merging")
return combined_video_path
- # if there is only one clip, use it directly
- if len(processed_clips) == 1:
- logger.info("using single clip directly")
- shutil.copy(processed_clips[0].file_path, combined_video_path)
- delete_files([processed_clips[0].file_path])
- logger.info("video combining completed")
- return combined_video_path
-
clip_files = [clip.file_path for clip in processed_clips]
logger.info(f"concatenating {len(clip_files)} clips with ffmpeg")
concat_video_clips_with_ffmpeg(
@@ -734,6 +733,7 @@ def combine_videos(
output_file=combined_video_path,
threads=threads,
output_dir=output_dir,
+ max_duration=audio_duration,
)
# clean temp files
diff --git a/test/services/test_video.py b/test/services/test_video.py
index c47ffbaf..c9346031 100644
--- a/test/services/test_video.py
+++ b/test/services/test_video.py
@@ -485,7 +485,7 @@ class TestVideoService(unittest.TestCase):
with patch.object(
vd, "_write_videofile_with_codec_fallback"
) as write_mock:
- with patch.object(vd, "concat_video_clips_with_ffmpeg"):
+ with patch.object(vd, "concat_video_clips_with_ffmpeg") as concat_mock:
with patch.object(vd, "delete_files"):
result = vd.combine_videos(
combined_video_path=combined_video_path,
@@ -499,6 +499,31 @@ class TestVideoService(unittest.TestCase):
self.assertEqual(result, combined_video_path)
self.assertEqual(write_mock.call_count, 4)
+ self.assertEqual(concat_mock.call_args.kwargs["max_duration"], 10.0)
+
+ def test_concat_video_clips_limits_output_to_audio_duration(self):
+ """最终拼接时应裁到音频时长,避免安全余量带来明显静音尾巴。"""
+
+ def fake_run(command, capture_output, text, check):
+ return types.SimpleNamespace(returncode=0, stdout="", stderr="")
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ clip_file = os.path.join(temp_dir, "clip.mp4")
+ output_file = os.path.join(temp_dir, "combined.mp4")
+ Path(clip_file).write_bytes(b"fake")
+
+ with patch.object(vd.subprocess, "run", side_effect=fake_run) as run:
+ vd.concat_video_clips_with_ffmpeg(
+ clip_files=[clip_file],
+ output_file=output_file,
+ threads=1,
+ output_dir=temp_dir,
+ max_duration=10.0,
+ )
+
+ command = run.call_args.args[0]
+ self.assertEqual(command[command.index("-t") + 1], "10.000")
+ self.assertLess(command.index("-t"), command.index(output_file))
def test_prioritize_unique_source_clips_uses_each_source_before_reuse(self):
"""