From a7dfb53971bbcf2e9ecb83a4790accecc5759b79 Mon Sep 17 00:00:00 2001 From: slmonker <148075178@qq.com> Date: Tue, 7 Oct 2025 01:39:34 +0800 Subject: [PATCH] Update io_utils.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adjust the code to ensure Windows users can properly use the video saving feature. 复制 询问 解释 翻译(zh-CN) --- ovi/utils/io_utils.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ovi/utils/io_utils.py b/ovi/utils/io_utils.py index 88dbb4d..f6de7bb 100644 --- a/ovi/utils/io_utils.py +++ b/ovi/utils/io_utils.py @@ -1,10 +1,20 @@ +import os import tempfile -from typing import Optional + +# ====================== 修复权限问题:设置自定义临时目录 ====================== +# 创建项目目录下的temp文件夹 +custom_temp = os.path.join(os.getcwd(), "temp") +os.makedirs(custom_temp, exist_ok=True) +tempfile.tempdir = custom_temp +# ============================================================================== import numpy as np from moviepy.editor import ImageSequenceClip, AudioFileClip from scipy.io import wavfile +from typing import Optional +import logging +logger = logging.getLogger(__name__) def save_video( output_path: str, @@ -55,12 +65,15 @@ def save_video( # Add audio if provided if audio_numpy is not None: - with tempfile.NamedTemporaryFile(suffix=".wav") as temp_audio_file: + # 关键修复:使用二进制模式('wb')和delete=False + with tempfile.NamedTemporaryFile(suffix=".wav", mode='wb', delete=False) as temp_audio_file: wavfile.write( temp_audio_file.name, sample_rate, (audio_numpy * 32767).astype(np.int16), ) + # 添加调试日志(可选,用于验证) + logger.info(f"Audio written to: {temp_audio_file.name}, size: {os.path.getsize(temp_audio_file.name)} bytes") audio_clip = AudioFileClip(temp_audio_file.name) final_clip = clip.set_audio(audio_clip) else: @@ -72,4 +85,4 @@ def save_video( ) final_clip.close() - return output_path \ No newline at end of file + return output_path