Update io_utils.py

Adjust the code to ensure Windows users can properly use the video saving feature.

复制
询问
解释

翻译(zh-CN)
This commit is contained in:
slmonker
2025-10-07 01:39:34 +08:00
committed by GitHub
parent 1550dbe16a
commit a7dfb53971
+16 -3
View File
@@ -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
return output_path