优化首帧功能,如果没传入首帧,则提取首帧。

This commit is contained in:
liuwei
2025-05-29 16:36:51 +08:00
parent a01b9e444e
commit 31c062fa70
2 changed files with 56 additions and 3 deletions

48
utils/video_utils.py Normal file
View File

@@ -0,0 +1,48 @@
import os
import cv2
from loguru import logger
def _get_first_frame(video_path, output_path):
"""
提取视频的第一帧并保存为图片
:param video_path: 视频文件路径
:param output_path: 输出图片路径
:return: 输出图片的绝对路径如果失败则返回None
"""
cap = None
try:
logger.info(f"开始提取视频首帧: {video_path}")
# 打开视频文件
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
logger.error(f"无法打开视频: {video_path}")
return None
# 读取首帧
ret, frame = cap.read()
if not ret:
logger.error("无法读取视频帧")
cap.release()
return None
# 保存首帧为图片
try:
cv2.imwrite(output_path, frame)
logger.info(f"首帧已保存为: {output_path}")
except Exception as e:
logger.error(f"保存首帧图片失败: {e}")
cap.release()
return None
# 释放资源
cap.release()
return os.path.abspath(output_path)
except Exception as e:
logger.error(f"提取视频首帧时出错: {e}")
return None
finally:
cap.release()