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

This commit is contained in:
liuwei
2025-05-29 16:47:39 +08:00
parent f5464f9e6c
commit 8da75ba329
2 changed files with 30 additions and 2 deletions

View File

@@ -1,4 +1,6 @@
import io
import os
import tempfile
import cv2
@@ -46,3 +48,24 @@ def _get_first_frame(video_path, output_path):
return None
finally:
cap.release()
def _get_first_frame_bytes(video_bytes, output_path):
"""
提取视频的第一帧并保存为图片
:param video_bytes: 视频文件流
:param output_path: 输出图片路径
:return: 输出图片的绝对路径如果失败则返回None
"""
try:
logger.info(f"开始提取视频流首帧")
# 创建临时文件
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file:
# 将字节流写入临时文件
temp_file.write(video_bytes)
temp_file_path = temp_file.name # 获取临时文件路径
# 打开视频文件
_get_first_frame(temp_file_path, output_path)
except Exception as e:
logger.error(f"提取视频首帧时出错: {e}")
return None