优化IO问题,使用异步方案进行视频下载等操作。
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
import re # 添加re模块导入
|
import re # 添加re模块导入
|
||||||
|
import asyncio
|
||||||
from typing import Dict, Any, List, Optional, Tuple
|
from typing import Dict, Any, List, Optional, Tuple
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -209,7 +209,7 @@ class DifyPlugin(MessagePluginInterface):
|
|||||||
if file_type == 1:
|
if file_type == 1:
|
||||||
await bot.send_image_message(target, Path(response))
|
await bot.send_image_message(target, Path(response))
|
||||||
elif file_type == 2:
|
elif file_type == 2:
|
||||||
first_frame = self._get_first_frame(response, f"dify_frame_{int(time.time())}.jpg")
|
first_frame = await self._get_first_frame(response, f"dify_frame_{int(time.time())}.jpg")
|
||||||
await bot.send_video_message(target, Path(response), Path(first_frame))
|
await bot.send_video_message(target, Path(response), Path(first_frame))
|
||||||
else:
|
else:
|
||||||
return False, "获取媒资失败"
|
return False, "获取媒资失败"
|
||||||
@@ -321,12 +321,12 @@ class DifyPlugin(MessagePluginInterface):
|
|||||||
if outputs["type"] == "image":
|
if outputs["type"] == "image":
|
||||||
downloader = MediaDownloader()
|
downloader = MediaDownloader()
|
||||||
image_url = outputs["result"]
|
image_url = outputs["result"]
|
||||||
image_path = downloader.download_media(image_url)
|
image_path = await downloader.download_media(image_url)
|
||||||
answer = image_path
|
answer = image_path
|
||||||
if outputs["type"] == "video":
|
if outputs["type"] == "video":
|
||||||
downloader = MediaDownloader()
|
downloader = MediaDownloader()
|
||||||
image_url = outputs["result"]
|
image_url = outputs["result"]
|
||||||
image_path = downloader.download_media(image_url)
|
image_path = await downloader.download_media(image_url)
|
||||||
answer = image_path
|
answer = image_path
|
||||||
# 处理文本类型返回
|
# 处理文本类型返回
|
||||||
elif "text" in outputs and isinstance(outputs["text"], str):
|
elif "text" in outputs and isinstance(outputs["text"], str):
|
||||||
@@ -446,29 +446,29 @@ class DifyPlugin(MessagePluginInterface):
|
|||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def _get_first_frame(self, video_path, output_path):
|
async def _get_first_frame(self, video_path: str, output_path: str) -> Optional[str]:
|
||||||
"""
|
"""
|
||||||
提取视频的第一帧并保存为图片
|
异步提取视频的第一帧并保存为图片
|
||||||
:param video_path: 视频文件路径
|
:param video_path: 视频文件路径
|
||||||
:param output_path: 输出图片路径
|
:param output_path: 输出图片路径
|
||||||
:return: 输出图片的绝对路径,如果失败则返回None
|
:return: 输出图片的绝对路径,如果失败则返回None
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.LOG.info(f"开始提取视频首帧: {video_path}")
|
self.LOG.info(f"开始提取视频首帧: {video_path}")
|
||||||
# 打开视频文件
|
|
||||||
|
# 使用线程池执行OpenCV操作
|
||||||
|
def extract_frame():
|
||||||
cap = cv2.VideoCapture(video_path)
|
cap = cv2.VideoCapture(video_path)
|
||||||
if not cap.isOpened():
|
if not cap.isOpened():
|
||||||
self.LOG.error(f"无法打开视频: {video_path}")
|
self.LOG.error(f"无法打开视频: {video_path}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# 读取首帧
|
|
||||||
ret, frame = cap.read()
|
ret, frame = cap.read()
|
||||||
if not ret:
|
if not ret:
|
||||||
self.LOG.error("无法读取视频帧")
|
self.LOG.error("无法读取视频帧")
|
||||||
cap.release()
|
cap.release()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# 保存首帧为图片
|
|
||||||
try:
|
try:
|
||||||
cv2.imwrite(output_path, frame)
|
cv2.imwrite(output_path, frame)
|
||||||
self.LOG.info(f"首帧已保存为: {output_path}")
|
self.LOG.info(f"首帧已保存为: {output_path}")
|
||||||
@@ -477,10 +477,13 @@ class DifyPlugin(MessagePluginInterface):
|
|||||||
cap.release()
|
cap.release()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# 释放资源
|
|
||||||
cap.release()
|
cap.release()
|
||||||
return os.path.abspath(output_path)
|
return os.path.abspath(output_path)
|
||||||
|
|
||||||
|
# 在线程池中执行OpenCV操作
|
||||||
|
result = await asyncio.to_thread(extract_frame)
|
||||||
|
return result
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.LOG.error(f"提取视频首帧时出错: {e}")
|
self.LOG.error(f"提取视频首帧时出错: {e}")
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user