兼容linux
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import os
|
||||
import time
|
||||
from typing import Dict, Any, List, Optional, Tuple
|
||||
|
||||
import cv2
|
||||
import requests
|
||||
from loguru import logger
|
||||
from pathlib import Path
|
||||
@@ -102,7 +104,7 @@ class VideoManPlugin(MessagePluginInterface):
|
||||
|
||||
try:
|
||||
# 下载视频
|
||||
file_abspath = self._download_video("https://api.guiguiya.com/api/video/fuji?type=json")
|
||||
file_abspath,first_frame = self._download_video("https://api.guiguiya.com/api/video/fuji?type=json")
|
||||
# FIXME 需要换成web容器地址。否则无法获取。
|
||||
if not file_abspath:
|
||||
await self.bot.send_text_message((roomid if roomid else sender), f"\n❌视频下载失败,请稍后再试",
|
||||
@@ -110,7 +112,7 @@ class VideoManPlugin(MessagePluginInterface):
|
||||
return False, "视频下载失败"
|
||||
|
||||
# 发送视频
|
||||
result = await self.bot.send_video_message((roomid if roomid else sender), Path(file_abspath))
|
||||
result = await self.bot.send_video_message((roomid if roomid else sender), Path(file_abspath),Path(first_frame))
|
||||
self.LOG.info(f"发送视频结果: {result}")
|
||||
return True, "发送成功"
|
||||
|
||||
@@ -157,7 +159,11 @@ class VideoManPlugin(MessagePluginInterface):
|
||||
|
||||
abs_path = os.path.abspath(save_path)
|
||||
self.LOG.info(f"视频已下载至: {abs_path}")
|
||||
return abs_path
|
||||
|
||||
first_frame_path = os.path.join(self.download_dir, f"frame_{int(time.time())}.jpg")
|
||||
first_frame = self._get_first_frame(save_path, first_frame_path)
|
||||
|
||||
return abs_path,first_frame
|
||||
|
||||
except requests.RequestException as e:
|
||||
self.LOG.error(f"请求失败: {e}")
|
||||
@@ -167,3 +173,42 @@ class VideoManPlugin(MessagePluginInterface):
|
||||
self.LOG.error(f"发生未知错误: {e}")
|
||||
|
||||
return None
|
||||
|
||||
def _get_first_frame(self, video_path, output_path):
|
||||
"""
|
||||
提取视频的第一帧并保存为图片
|
||||
:param video_path: 视频文件路径
|
||||
:param output_path: 输出图片路径
|
||||
:return: 输出图片的绝对路径,如果失败则返回None
|
||||
"""
|
||||
try:
|
||||
self.LOG.info(f"开始提取视频首帧: {video_path}")
|
||||
# 打开视频文件
|
||||
cap = cv2.VideoCapture(video_path)
|
||||
if not cap.isOpened():
|
||||
self.LOG.error(f"无法打开视频: {video_path}")
|
||||
return None
|
||||
|
||||
# 读取首帧
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
self.LOG.error("无法读取视频帧")
|
||||
cap.release()
|
||||
return None
|
||||
|
||||
# 保存首帧为图片
|
||||
try:
|
||||
cv2.imwrite(output_path, frame)
|
||||
self.LOG.info(f"首帧已保存为: {output_path}")
|
||||
except Exception as e:
|
||||
self.LOG.error(f"保存首帧图片失败: {e}")
|
||||
cap.release()
|
||||
return None
|
||||
|
||||
# 释放资源
|
||||
cap.release()
|
||||
return os.path.abspath(output_path)
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"提取视频首帧时出错: {e}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user