From a355047c5f8348065ed9f69a557aa8bf8a97d5c4 Mon Sep 17 00:00:00 2001 From: liuwei Date: Fri, 9 May 2025 14:28:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E8=A7=86=E9=A2=91=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E9=A6=96=E5=B8=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/video/main.py | 31 ++++++++++++++++++++++++++----- requirements.txt | 4 +++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/plugins/video/main.py b/plugins/video/main.py index 36b100e..047417f 100644 --- a/plugins/video/main.py +++ b/plugins/video/main.py @@ -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) diff --git a/requirements.txt b/requirements.txt index 47ca5d1..c9555da 100644 --- a/requirements.txt +++ b/requirements.txt @@ -47,4 +47,6 @@ qrcode~=7.4.2 pysilk~=0.0.1 pydub~=0.25.1 pymediainfo~=7.0.1 -loguru~=0.7.3 \ No newline at end of file +loguru~=0.7.3 +opencv-python~=4.11.0.86 +pathlib~=1.0.1 \ No newline at end of file