利用 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(video): serialize MoviePy reader stdout capture
Rudy Celeklic2ed18d27f5c
diff --git a/app/services/video.py b/app/services/video.pyindex bba1533b..38b6ca91 100644--- a/app/services/video.py+++ b/app/services/video.py@@ -653,6 +653,9 @@ def _open_image_clip_with_fallback(image_path: str):return ImageClip(sanitized_path), sanitized_path+_moviepy_reader_open_lock = threading.Lock()++def _open_video_clip_quietly(video_path: str, audio: bool = False) -> VideoFileClip:"""安静地打开视频文件,避免 MoviePy 2.1.x 把 ffmpeg 探测信息直接打印到 stdout。@@ -670,8 +673,12 @@ def _open_video_clip_quietly(video_path: str, audio: bool = False) -> VideoFileC3. 如果依赖库确实输出了内容,降级为 debug 日志,便于必要时排查。"""captured_stdout = io.StringIO()- with redirect_stdout(captured_stdout):- clip = VideoFileClip(video_path, audio=audio)+ # redirect_stdout changes process-wide state. Overlapping reader opens can+ # restore each other's capture buffers instead of the original stdout.+ # Serialize this short construction window; clip processing stays parallel.+ with _moviepy_reader_open_lock:+ with redirect_stdout(captured_stdout):+ clip = VideoFileClip(video_path, audio=audio)moviepy_stdout = captured_stdout.getvalue().strip()if moviepy_stdout:diff --git a/test/services/test_video_reader_stdout.py b/test/services/test_video_reader_stdout.pynew file mode 100644index 00000000..16fcc466--- /dev/null+++ b/test/services/test_video_reader_stdout.py@@ -0,0 +1,61 @@+import io+import sys+import threading+import unittest+from concurrent.futures import ThreadPoolExecutor+from unittest.mock import patch++from app.services import video+++class TestVideoReaderStdout(unittest.TestCase):+ def test_overlapping_reader_opens_restore_the_original_stdout(self):+ first_started = threading.Event()+ second_started = threading.Event()+ first_returned = threading.Event()+ release_first = threading.Event()+ captured = io.StringIO()+ readers = {"first.mp4": object(), "second.mp4": object()}++ def open_reader(path, audio=False):+ if path == "first.mp4":+ first_started.set()+ if not release_first.wait(timeout=2):+ raise TimeoutError("first reader was not released")+ else:+ second_started.set()+ if not first_returned.wait(timeout=2):+ raise TimeoutError("first caller did not finish")+ print(f"metadata: {path}")+ return readers[path]++ def first_call():+ try:+ return video._open_video_clip_quietly("first.mp4")+ finally:+ first_returned.set()++ with patch.object(video, "VideoFileClip", side_effect=open_reader), patch.object(sys, "stdout", captured):+ with ThreadPoolExecutor(max_workers=2) as executor:+ first = executor.submit(first_call)+ self.assertTrue(first_started.wait(timeout=1))+ second = executor.submit(video._open_video_clip_quietly, "second.mp4")+ # Old code permits both redirections to overlap; corrected code+ # serializes just reader construction, not later encoding.+ second_started.wait(timeout=0.1)+ release_first.set()+ self.assertIs(first.result(timeout=2), readers["first.mp4"])+ self.assertIs(second.result(timeout=2), readers["second.mp4"])+ self.assertIs(sys.stdout, captured)+ print("CLI result after both readers")+ self.assertEqual(captured.getvalue(), "CLI result after both readers\n")++ def test_failed_reader_restores_stdout_and_releases_open_lock(self):+ captured = io.StringIO()+ reader = object()+ with patch.object(video, "VideoFileClip", side_effect=[RuntimeError("bad media"), reader]), patch.object(sys, "stdout", captured):+ with self.assertRaisesRegex(RuntimeError, "bad media"):+ video._open_video_clip_quietly("bad.mp4")+ self.assertIs(sys.stdout, captured)+ self.assertIs(video._open_video_clip_quietly("good.mp4"), reader)+ self.assertIs(sys.stdout, captured)