Skip to content

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

feat(video): report clip processing and final render progress

amitk2108f420c092ee56
diff --git a/app/services/task.py b/app/services/task.py
index a30f7f52..6334b721 100644
--- a/app/services/task.py
+++ b/app/services/task.py
@@ -983,6 +983,9 @@ def generate_final_videos(
}
if allocate_batch_materials else {}
)
+ # 每条视频占 50% 进度中的一份,合成与最终编码各占一半。片段处理是
+ # 合成里最耗时的部分,逐段推进这一半的进度。
+ combine_share = 50 / params.video_count / 2
video.combine_videos(
combined_video_path=combined_video_path,
video_paths=downloaded_videos,
@@ -994,6 +997,9 @@ def generate_final_videos(
max_clip_duration=params.video_clip_duration,
threads=params.n_threads,
clip_speed=params.video_clip_speed,
+ progress_callback=_stage_progress_reporter(
+ task_id, _progress, _progress + combine_share
+ ),
**batch_options,
)
if allocate_batch_materials:
diff --git a/app/services/video.py b/app/services/video.py
index c9a53652..3df63dd3 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -11,9 +11,9 @@ import tempfile
import threading
import time
import unicodedata
-from contextlib import ExitStack, redirect_stdout
+from contextlib import ExitStack, contextmanager, redirect_stdout
from functools import lru_cache
-from typing import List
+from typing import Callable, List
from loguru import logger
import numpy as np
from moviepy import (
@@ -99,6 +99,9 @@ _DEFAULT_VIDEO_CODEC = "libx264"
# ffmpeg 串联片段期间没有阶段日志,`subprocess.run` 又阻塞到进程退出,耗时拼接在
# 日志上表现为“无输出”。这里按间隔记录存活信息,便于区分编码中与已经卡死。
_FFMPEG_CONCAT_HEARTBEAT_SECONDS = 30.0
+# MoviePy 写出最终成片时同样没有任何日志,一分钟的竖屏视频在 CPU 上要编码数
+# 分钟。沿用拼接阶段的做法,按间隔记录存活信息。
+_STAGE_HEARTBEAT_SECONDS = 30.0
_DEFAULT_FFMPEG_CONCAT_TIMEOUT_SECONDS = 3600
_SUBTITLE_SPRING_DURATION_SECONDS = 0.18
_MIN_SUBTITLE_SPRING_SCALE = 0.05
@@ -496,6 +499,58 @@ def _describe_concat_output_progress(output_file: str) -> str:
return f"output size: {size / (1024 * 1024):.2f} MB"
+@contextmanager
+def _stage_heartbeat(description: str):
+ """
+ 在一个没有自身日志的耗时阶段期间,按间隔记录存活日志。
+
+ 心跳线程绑定到进入该阶段的线程,日志才会出现在所属任务的 WebUI 面板。
+ 阶段结束或抛出异常时停止并等待心跳线程退出,避免阶段已经结束后仍然
+ 写出“still running”。
+ """
+ started_at = time.monotonic()
+ stop_event = threading.Event()
+
+ def log_heartbeat() -> None:
+ while not stop_event.wait(_STAGE_HEARTBEAT_SECONDS):
+ logger.info(
+ f"{description} still running: "
+ f"elapsed={time.monotonic() - started_at:.0f}s"
+ )
+
+ reporter = threading.Thread(
+ target=logging_utils.bind_log_scope(log_heartbeat), daemon=True
+ )
+ reporter.start()
+ try:
+ yield
+ finally:
+ stop_event.set()
+ reporter.join(timeout=1)
+
+
+def _report_clip_progress(
+ progress_callback: Callable[[float], None] | None,
+ covered_duration: float,
+ required_duration: float,
+) -> None:
+ """把已覆盖的成片时长换算成 0~1 的比例并通知调用方。"""
+ if progress_callback is None:
+ return
+ fraction = 1.0
+ if required_duration > 0:
+ fraction = min(1.0, covered_duration / required_duration)
+ try:
+ progress_callback(fraction)
+ except Exception as exc:
+ # 进度只是展示信息。回调失败(例如状态后端暂时不可用)不能让已经
+ # 处理好的片段作废。
+ logger.warning(
+ "failed to report clip processing progress: "
+ f"error={type(exc).__name__}, detail={exc}"
+ )
+
+
def _run_concat_with_heartbeat(command: list[str], output_file: str):
"""
阻塞等待 ffmpeg 完成,期间按间隔记录存活日志。
@@ -865,6 +920,7 @@ def combine_videos(
source_usage: dict[str, int] | None = None,
source_groups: dict[str, str] | None = None,
used_video_paths: List[str] | None = None,
+ progress_callback: Callable[[float], None] | None = None,
) -> str:
audio_clip = AudioFileClip(audio_file)
try:
@@ -1098,6 +1154,15 @@ def combine_videos(
continue
processed_clips.append(processed_clip)
video_duration += processed_clip.duration
+ # 每段 4K 素材要处理十几秒,逐段报告覆盖时长,日志和进度条
+ # 才能反映这一阶段仍在推进。
+ logger.info(
+ f"processed clip {len(processed_clips)}: "
+ f"{video_duration:.1f} of {required_video_duration:.1f}s covered"
+ )
+ _report_clip_progress(
+ progress_callback, video_duration, required_video_duration
+ )
next_candidate_index = candidate_index
# loop processed clips until the video duration covers the audio duration and the small safety margin.
@@ -1688,19 +1753,20 @@ def generate_video(
# 显式沿用输入音频的采样率;如果取不到,再回退 MoviePy 默认的 44100Hz。
# 这样可以减少不同环境,尤其 Docker 中再次重采样带来的音质波动。
output_audio_fps = int(getattr(audio_clip, "fps", 0) or 44100)
- _write_videofile_with_codec_fallback(
- final_video_clip,
- output_file=output_file,
- codec=_get_configured_video_codec(),
- atomic_output=True,
- audio_codec=audio_codec,
- audio_fps=output_audio_fps,
- audio_bitrate=audio_bitrate,
- temp_audiofile_path=_get_temp_audio_dir(output_dir),
- threads=params.n_threads or 2,
- logger=None,
- fps=fps,
- )
+ with _stage_heartbeat("final video render"):
+ _write_videofile_with_codec_fallback(
+ final_video_clip,
+ output_file=output_file,
+ codec=_get_configured_video_codec(),
+ atomic_output=True,
+ audio_codec=audio_codec,
+ audio_fps=output_audio_fps,
+ audio_bitrate=audio_bitrate,
+ temp_audiofile_path=_get_temp_audio_dir(output_dir),
+ threads=params.n_threads or 2,
+ logger=None,
+ fps=fps,
+ )
return bgm_mix_succeeded
diff --git a/test/services/test_task.py b/test/services/test_task.py
index c120ccd2..10ab3cd4 100644
--- a/test/services/test_task.py
+++ b/test/services/test_task.py
@@ -192,6 +192,52 @@ class TestTaskService(unittest.TestCase):
self.assertEqual(result, ["a.mp4"])
self.assertEqual(observed, [42, 45, 50])
+ def test_clip_processing_moves_progress_through_the_combine_half(self):
+ """
+ 片段处理期间进度此前固定在 50%。合成占每条视频进度份额的前一半:
+ 单条视频时为 50%~75%,两条视频时第二条从 75% 开始。
+ """
+ for video_count, expected in (
+ (1, [[62, 75]]),
+ (2, [[56, 62], [81, 87]]),
+ ):
+ with self.subTest(video_count=video_count):
+ params = VideoParams(video_subject="test", video_count=video_count)
+ state = MemoryState()
+ state.update_task("combine-progress", progress=50)
+ observed = []
+
+ def fake_combine_videos(**kwargs):
+ seen = []
+ for fraction in (0.5, 1.0):
+ kwargs["progress_callback"](fraction)
+ seen.append(
+ state.get_task("combine-progress")["progress"]
+ )
+ observed.append(seen)
+
+ with (
+ patch.object(tm.sm, "state", state),
+ patch.object(
+ tm.video, "combine_videos", side_effect=fake_combine_videos
+ ),
+ patch.object(tm.video, "generate_video"),
+ patch.object(tm.task_artifacts, "patch_script_data"),
+ ):
+ tm.generate_final_videos(
+ task_id="combine-progress",
+ params=params,
+ downloaded_videos=["material.mp4"],
+ audio_file="audio.mp3",
+ subtitle_path="",
+ audio_duration=5,
+ )
+
+ self.assertEqual(observed, expected)
+ self.assertEqual(
+ state.get_task("combine-progress")["progress"], 100
+ )
+
def test_generate_final_videos_uses_generated_sonilo_music(self):
"""Sonilo 必须针对每条拼接后的视频生成配乐,并传给最终混音。"""
params = VideoParams(
diff --git a/test/services/test_video.py b/test/services/test_video.py
index fe2f1c85..3da05d8f 100644
--- a/test/services/test_video.py
+++ b/test/services/test_video.py
@@ -1511,6 +1511,201 @@ class TestVideoService(unittest.TestCase):
"逐片段处理日志必须归属发起合成的任务线程",
)
+ def test_combine_videos_reports_covered_duration_as_progress(self):
+ """
+ 片段处理是合成阶段最耗时的部分(4K 素材每段约 20 秒),此前整个阶段
+ 进度固定在 50%。每处理完一段都要报告已覆盖的成片时长比例,并写一条
+ 带覆盖时长的日志;全部覆盖后比例封顶为 1.0。
+ """
+
+ class _FakeAudioClip:
+ duration = 4.0
+
+ def close(self):
+ pass
+
+ class _FakeVideoClip:
+ def __init__(self, duration):
+ self.duration = duration
+ self.size = (1080, 1920)
+ self.w = 1080
+ self.h = 1920
+
+ def subclipped(self, start_time, end_time):
+ return _FakeVideoClip(end_time - start_time)
+
+ def close(self):
+ pass
+
+ fractions = []
+ with tempfile.TemporaryDirectory() as temp_dir:
+ with (
+ patch.object(vd, "AudioFileClip", return_value=_FakeAudioClip()),
+ patch.object(
+ vd,
+ "_open_video_clip_quietly",
+ side_effect=lambda _path: _FakeVideoClip(10.0),
+ ),
+ patch.object(vd, "_write_videofile_with_codec_fallback"),
+ patch.object(vd, "concat_video_clips_with_ffmpeg"),
+ patch.object(vd, "delete_files"),
+ patch.object(vd.logger, "info") as info,
+ ):
+ vd.combine_videos(
+ combined_video_path=os.path.join(temp_dir, "combined.mp4"),
+ # 顺序模式下每个源文件只取一段,三个源文件对应三段。
+ video_paths=["a.mp4", "b.mp4", "c.mp4"],
+ audio_file="audio.mp3",
+ video_concat_mode=vd.VideoConcatMode.sequential,
+ max_clip_duration=2,
+ progress_callback=fractions.append,
+ )
+
+ # 配音 4.0 秒加 0.1 秒安全余量,每段 2 秒:需要 3 段才覆盖 4.1 秒。
+ self.assertEqual(len(fractions), 3)
+ self.assertEqual(fractions, sorted(fractions))
+ self.assertAlmostEqual(fractions[0], 2.0 / 4.1, places=3)
+ self.assertEqual(fractions[-1], 1.0)
+ processed = [
+ str(call.args[0])
+ for call in info.call_args_list
+ if str(call.args[0]).startswith("processed clip")
+ ]
+ self.assertEqual(
+ processed,
+ [
+ "processed clip 1: 2.0 of 4.1s covered",
+ "processed clip 2: 4.0 of 4.1s covered",
+ "processed clip 3: 6.0 of 4.1s covered",
+ ],
+ )
+
+ def test_failing_clip_progress_callback_does_not_break_combine(self):
+ """进度只是展示信息,回调出错不能让已经处理好的片段作废。"""
+
+ class _FakeAudioClip:
+ duration = 1.0
+
+ def close(self):
+ pass
+
+ class _FakeVideoClip:
+ def __init__(self, duration):
+ self.duration = duration
+ self.size = (1080, 1920)
+ self.w = 1080
+ self.h = 1920
+
+ def subclipped(self, start_time, end_time):
+ return _FakeVideoClip(end_time - start_time)
+
+ def close(self):
+ pass
+
+ def broken_callback(_fraction):
+ raise RuntimeError("state backend unavailable")
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ combined_video_path = os.path.join(temp_dir, "combined.mp4")
+ with (
+ patch.object(vd, "AudioFileClip", return_value=_FakeAudioClip()),
+ patch.object(
+ vd,
+ "_open_video_clip_quietly",
+ side_effect=lambda _path: _FakeVideoClip(10.0),
+ ),
+ patch.object(vd, "_write_videofile_with_codec_fallback"),
+ patch.object(vd, "concat_video_clips_with_ffmpeg") as concat,
+ patch.object(vd, "delete_files"),
+ patch.object(vd.logger, "warning") as warning,
+ ):
+ result = vd.combine_videos(
+ combined_video_path=combined_video_path,
+ video_paths=["clip.mp4"],
+ audio_file="audio.mp3",
+ video_concat_mode=vd.VideoConcatMode.sequential,
+ max_clip_duration=2,
+ progress_callback=broken_callback,
+ )
+
+ self.assertEqual(result, combined_video_path)
+ concat.assert_called_once()
+ self.assertTrue(
+ [
+ call
+ for call in warning.call_args_list
+ if "progress" in str(call.args[0])
+ ]
+ )
+
+ def test_stage_heartbeat_logs_while_running_and_stops_afterwards(self):
+ """
+ 最终成片编码期间 MoviePy 不输出任何日志。心跳要在阶段运行时按间隔
+ 写出、归属任务线程,并在阶段结束后停止,不能留下继续刷日志的线程。
+ """
+ with (
+ patch.object(vd, "_STAGE_HEARTBEAT_SECONDS", 0.02),
+ _capture_task_scoped_logs() as messages,
+ ):
+ with vd._stage_heartbeat("final video render"):
+ time.sleep(0.2)
+ heartbeats_at_exit = len(
+ [m for m in messages if "still running" in m]
+ )
+ time.sleep(0.1)
+ heartbeats_later = len([m for m in messages if "still running" in m])
+
+ self.assertGreater(heartbeats_at_exit, 0)
+ self.assertEqual(heartbeats_later, heartbeats_at_exit)
+ self.assertRegex(
+ next(m for m in messages if "still running" in m),
+ r"^final video render still running: elapsed=\d+s$",
+ )
+
+ def test_stage_heartbeat_stops_when_the_stage_fails(self):
+ """阶段抛出异常时心跳线程同样要停止,异常原样向外传播。"""
+ with patch.object(vd, "_STAGE_HEARTBEAT_SECONDS", 0.02):
+ with _capture_task_scoped_logs() as messages:
+ with self.assertRaisesRegex(RuntimeError, "encode failed"):
+ with vd._stage_heartbeat("final video render"):
+ raise RuntimeError("encode failed")
+ time.sleep(0.1)
+
+ self.assertEqual([m for m in messages if "still running" in m], [])
+
+ def test_generate_video_reports_heartbeat_during_final_render(self):
+ """最终编码耗时数分钟,期间必须有存活日志。"""
+ params = vd.VideoParams(
+ video_subject="test", subtitle_enabled=False, bgm_type=""
+ )
+
+ def slow_write(*_args, **_kwargs):
+ time.sleep(0.2)
+
+ with (
+ patch.object(vd, "_STAGE_HEARTBEAT_SECONDS", 0.02),
+ patch.object(
+ vd, "_open_video_clip_quietly", return_value=_FakeMoviePyClip()
+ ),
+ patch.object(vd, "AudioFileClip", return_value=_FakeMoviePyClip()),
+ patch.object(
+ vd, "_write_videofile_with_codec_fallback", side_effect=slow_write
+ ),
+ patch.object(vd, "_get_configured_video_codec", return_value="libx264"),
+ _capture_task_scoped_logs() as messages,
+ ):
+ vd.generate_video(
+ video_path="combined.mp4",
+ audio_path="voice.mp3",
+ subtitle_path="",
+ output_file="final.mp4",
+ params=params,
+ )
+
+ self.assertTrue(
+ [m for m in messages if m.startswith("final video render still running")]
+ )
+
def test_concat_timeout_fails_without_retrying_another_codec(self):
"""A stalled FFmpeg must fail the task and release the concat list file."""
config.app["ffmpeg_concat_timeout_seconds"] = 12