diff --git a/utils/video_utils.py b/utils/video_utils.py index 392b749..536068f 100644 --- a/utils/video_utils.py +++ b/utils/video_utils.py @@ -1,5 +1,7 @@ import os import tempfile +import time +from pathlib import Path import cv2 from loguru import logger @@ -30,8 +32,23 @@ def get_first_frame(video_path, output_path): # 保存首帧为图片 try: - cv2.imwrite(output_path, frame) - logger.info(f"首帧已保存为: {output_path}") + # 获取项目根目录 + project_root = os.getcwd() + project_root_path = Path(project_root).resolve() + + # 创建临时目录 + temp_dir = project_root_path / "temp" + try: + temp_dir.mkdir(parents=True, exist_ok=True) + except Exception as e: + logger.error(f"Failed to create temp directory: {e}") + raise FileNotFoundError(f"Could not create temp directory: {temp_dir}") + + # 生成唯一的临时文件名 + timestamp = int(time.time()) + output_image_path = temp_dir / output_path + cv2.imwrite(str(output_image_path), frame) + logger.info(f"首帧已保存为: {str(output_image_path)}") except Exception as e: logger.error(f"保存首帧图片失败: {e}") cap.release() @@ -39,7 +56,7 @@ def get_first_frame(video_path, output_path): # 释放资源 cap.release() - return os.path.abspath(output_path) + return os.path.abspath(str(output_image_path)) except Exception as e: logger.error(f"提取视频首帧时出错: {e}")