利用 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
feat(video): log concat progress while ffmpeg is running
c0206274197cebed171
diff --git a/app/services/video.py b/app/services/video.pyindex 53830384..7689c1f3 100644--- a/app/services/video.py+++ b/app/services/video.py@@ -7,6 +7,8 @@ import gcimport subprocessimport sysimport tempfile+import threading+import timeimport unicodedatafrom contextlib import ExitStack, redirect_stdoutfrom functools import lru_cache@@ -82,6 +84,9 @@ _MIN_MATERIAL_DIMENSION = 480# 既能放行仅仅因为取整而略低于阈值的素材,也仍然能挡住真正的低清素材。_MIN_DIMENSION_TOLERANCE = 10_DEFAULT_VIDEO_CODEC = "libx264"+# ffmpeg 串联片段期间没有阶段日志,`subprocess.run` 又阻塞到进程退出,耗时拼接在+# 日志上表现为“无输出”。这里按间隔记录存活信息,便于区分编码中与已经卡死。+_FFMPEG_CONCAT_HEARTBEAT_SECONDS = 30.0_SUBTITLE_SPRING_DURATION_SECONDS = 0.18_MIN_SUBTITLE_SPRING_SCALE = 0.05_MAX_SUBTITLE_SPRING_SCALE = 1.35@@ -436,6 +441,42 @@ def _format_ffmpeg_concat_path(file_path: str) -> str:return _escape_ffmpeg_concat_path(absolute_path.replace("\\", "/"))+def _describe_concat_output_progress(output_file: str) -> str:+ """返回输出文件当前大小的可读描述,用于拼接心跳日志。"""+ try:+ size = os.path.getsize(output_file)+ except OSError:+ # 输出文件尚未创建时同样要安全降级,不能影响拼接本身。+ return "output size not available"+ return f"output size: {size / (1024 * 1024):.2f} MB"+++def _run_concat_with_heartbeat(command: list[str], output_file: str):+ """+ 阻塞等待 ffmpeg 完成,期间按间隔记录存活日志。++ ffmpeg 串联片段时没有阶段日志,`subprocess.run` 又把输出缓冲到进程退出,耗时拼接+ 在日志上表现为“无输出”。记录已等待时长与输出文件大小,便于区分仍在编码与已经卡死。+ """+ started_at = time.monotonic()+ stop_event = threading.Event()++ def log_heartbeat() -> None:+ while not stop_event.wait(_FFMPEG_CONCAT_HEARTBEAT_SECONDS):+ logger.info(+ "ffmpeg concat still running: "+ f"elapsed={time.monotonic() - started_at:.0f}s, "+ f"{_describe_concat_output_progress(output_file)}"+ )++ reporter = threading.Thread(target=log_heartbeat, daemon=True)+ reporter.start()+ try:+ return subprocess.run(command, capture_output=True, text=True, check=False)+ finally:+ stop_event.set()++def concat_video_clips_with_ffmpeg(clip_files: List[str],output_file: str,@@ -473,13 +514,8 @@ def concat_video_clips_with_ffmpeg(def run_concat(codec: str):command = build_command(codec)# 使用 ffmpeg 只做一次串联与编码,避免 MoviePy 逐段合并时反复重编码,- # 从而降低画质劣化与颜色偏移风险。- result = subprocess.run(- command,- capture_output=True,- text=True,- check=False,- )+ # 从而降低画质劣化与颜色偏移风险。阻塞等待期间由心跳日志体现任务仍在运行。+ result = _run_concat_with_heartbeat(command, output_file)if result.returncode != 0:error_message = (result.stderr or result.stdout or "").strip()raise RuntimeError(error_message or "ffmpeg concat failed")diff --git a/test/services/test_video.py b/test/services/test_video.pyindex 20f8d634..195e2177 100644--- a/test/services/test_video.py+++ b/test/services/test_video.py@@ -2,6 +2,7 @@ import osimport shutilimport sysimport tempfile+import timeimport typesimport unittestfrom contextlib import redirect_stdout@@ -984,6 +985,59 @@ class TestVideoService(unittest.TestCase):self.assertEqual(command[command.index("-t") + 1], "10.000")self.assertLess(command.index("-t"), command.index(output_file))+ def test_concat_video_clips_logs_heartbeat_while_ffmpeg_runs(self):+ """+ 拼接时 subprocess.run 会阻塞到 ffmpeg 退出,期间项目不再产生任何日志,用户+ 无法区分仍在编码与已经卡死(issue #1342)。等待期间必须记录存活信息。+ """++ def slow_run(command, capture_output, text, check):+ # 模拟一次耗时拼接:这段窗口内心跳线程应至少记录一次存活日志。+ time.sleep(0.2)+ 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")+ Path(output_file).write_bytes(b"x" * 2048)++ with patch.object(vd, "_FFMPEG_CONCAT_HEARTBEAT_SECONDS", 0.02):+ with patch.object(vd.subprocess, "run", side_effect=slow_run):+ with patch.object(vd.logger, "info") as info_mock:+ vd.concat_video_clips_with_ffmpeg(+ clip_files=[clip_file],+ output_file=output_file,+ threads=1,+ output_dir=temp_dir,+ )++ heartbeats = [+ str(call.args[0])+ for call in info_mock.call_args_list+ if "still running" in str(call.args[0])+ ]+ self.assertTrue(heartbeats, "耗时拼接期间必须记录存活日志")+ self.assertRegex(heartbeats[0], r"elapsed=\d+s, output size: 0\.00 MB")++ def test_concat_video_clips_heartbeat_tolerates_missing_output_file(self):+ """+ 拼接刚开始时输出文件尚未创建,心跳描述必须安全降级;若探测文件大小的异常+ 穿透到拼接调用,本可正常完成的任务会变成失败。+ """+ with tempfile.TemporaryDirectory() as temp_dir:+ self.assertIn(+ "not available",+ vd._describe_concat_output_progress(+ os.path.join(temp_dir, "absent.mp4")+ ),+ )+ existing = os.path.join(temp_dir, "present.mp4")+ Path(existing).write_bytes(b"x" * 2048)+ self.assertIn(+ "output size: 0.00 MB", vd._describe_concat_output_progress(existing)+ )+def test_prioritize_unique_source_clips_uses_each_source_before_reuse(self):"""随机模式下,一个长素材会被拆成多个片段。调度层应先让每个源素材