发送视频加入首帧

This commit is contained in:
liuwei
2025-05-09 14:28:18 +08:00
parent 2fa51533d5
commit a355047c5f
2 changed files with 29 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ import os
import requests
from typing import Dict, Any, List, Optional, Tuple
import cv2
from plugin_common.message_plugin_interface import MessagePluginInterface
from plugin_common.plugin_interface import PluginStatus
from utils.decorator.plugin_decorators import plugin_stats_decorator
@@ -105,7 +106,7 @@ class VideoPlugin(MessagePluginInterface):
try:
# 下载视频
save_path = os.path.join(self.download_dir, "video.mp4")
file_abspath = self._download_stream("https://api.guiguiya.com/api/hook/heisis", save_path)
file_abspath, first_frame = self._download_stream("https://api.guiguiya.com/api/hook/heisis", save_path)
if not file_abspath or not file_abspath.endswith("mp4"):
await self.bot.send_text_message((roomid if roomid else sender), f"\n❌视频下载失败,请稍后再试",
@@ -113,7 +114,7 @@ class VideoPlugin(MessagePluginInterface):
return False, "视频下载失败"
# 发送视频
result = await self.bot.send_video_message((roomid if roomid else sender), file_abspath)
result = await self.bot.send_video_message((roomid if roomid else sender), file_abspath, first_frame)
self.LOG.info(f"发送视频结果: {result}")
return True, "发送成功"
@@ -144,7 +145,7 @@ class VideoPlugin(MessagePluginInterface):
if "video" not in content_type and "application/octet-stream" not in content_type:
self.LOG.warning(f"警告: 返回的可能不是视频流Content-Type: {content_type}")
self.LOG.warning(f"响应内容预览: {response.text[:100]}") # 打印前100字符查看
return None
return None, None
# 以二进制写入模式保存流数据
with open(save_path, "wb") as file:
@@ -152,11 +153,31 @@ class VideoPlugin(MessagePluginInterface):
if chunk: # 过滤空块
file.write(chunk)
self.LOG.info(f"视频已下载到: {save_path}")
return os.path.abspath(save_path)
# 加入首帧下载
first_frame = self._get_first_frame(save_path)
return os.path.abspath(save_path), first_frame
except requests.RequestException as e:
self.LOG.error(f"请求失败: {e}")
except IOError as e:
self.LOG.error(f"文件写入失败: {e}")
except Exception as e:
self.LOG.error(f"发生未知错误: {e}")
return None
return None, None
def _get_first_frame(self, video_path, output_path="first_frame.jpg"):
# 打开视频文件
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("无法打开视频")
return
# 读取首帧
ret, frame = cap.read()
if ret:
# 保存首帧为图片
cv2.imwrite(output_path, frame)
print(f"首帧已保存为 {output_path}")
# 释放资源
cap.release()
return os.path.abspath(output_path)

View File

@@ -47,4 +47,6 @@ qrcode~=7.4.2
pysilk~=0.0.1
pydub~=0.25.1
pymediainfo~=7.0.1
loguru~=0.7.3
loguru~=0.7.3
opencv-python~=4.11.0.86
pathlib~=1.0.1