Skip to content

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

Fix Windows MoviePy temp audio handling

Mert Furkan Doğan6044b6a6e74e
diff --git a/app/services/video.py b/app/services/video.py
index 8259b0b9..e38e2f2c 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -6,6 +6,8 @@ import random
import gc
import shutil
import subprocess
+import sys
+import tempfile
from contextlib import redirect_stdout
from functools import lru_cache
from typing import List
@@ -225,6 +227,24 @@ def _disable_runtime_video_codec(codec: str, reason: str):
)
+def _get_temp_audio_dir(output_dir: str) -> str:
+ """
+ Return the directory to use for MoviePy's temporary audio file.
+
+ On Windows, Windows Defender can lock files written to the task output
+ directory while scanning them, causing MoviePy to fail with a
+ PermissionError (WinError 32) on the TEMP_MPY_wvf_snd temp file and
+ leaving the final MP4 at 0 bytes. Using the system temp directory
+ sidesteps the scan without changing behaviour on other platforms.
+
+ On Linux/macOS/Docker the output directory is returned unchanged so
+ existing behaviour is preserved.
+ """
+ if sys.platform == "win32":
+ return tempfile.gettempdir()
+ return output_dir
+
+
def _fallback_write_videofile(clip, output_file: str, failed_codec: str, reason: str, **kwargs):
"""
硬件编码失败后用 libx264 重试,只有重试成功才禁用该硬件编码器。
@@ -1079,7 +1099,7 @@ def generate_video(
audio_codec=audio_codec,
audio_fps=output_audio_fps,
audio_bitrate=audio_bitrate,
- temp_audiofile_path=output_dir,
+ temp_audiofile_path=_get_temp_audio_dir(output_dir),
threads=params.n_threads or 2,
logger=None,
fps=fps,
diff --git a/test/services/test_video.py b/test/services/test_video.py
index 53440a45..aacda263 100644
--- a/test/services/test_video.py
+++ b/test/services/test_video.py
@@ -562,5 +562,18 @@ class TestVideoService(unittest.TestCase):
finally:
clip.close()
+ def test_get_temp_audio_dir_returns_system_temp_on_windows(self):
+ with patch("sys.platform", "win32"):
+ result = vd._get_temp_audio_dir("/some/output/dir")
+ self.assertEqual(result, tempfile.gettempdir())
+
+ def test_get_temp_audio_dir_returns_output_dir_on_non_windows(self):
+ for platform in ("linux", "darwin"):
+ with self.subTest(platform=platform):
+ with patch("sys.platform", platform):
+ result = vd._get_temp_audio_dir("/some/output/dir")
+ self.assertEqual(result, "/some/output/dir")
+
+
if __name__ == "__main__":
- unittest.main()
+ unittest.main()