优化IO问题,使用异步方案进行视频下载等操作。

This commit is contained in:
liuwei
2025-06-16 10:11:43 +08:00
parent 34f9158697
commit 02a387628c
5 changed files with 68 additions and 58 deletions

View File

@@ -13,6 +13,7 @@ import pysilk
from loguru import logger
from pydub import AudioSegment
from pymediainfo import MediaInfo
import aiofiles
from utils.video_utils import get_first_frame, get_first_frame_bytes
from wechat_ipad import UserLoggedOut
@@ -178,8 +179,8 @@ class MessageMixin(WechatAPIClientBase):
elif isinstance(image, bytes):
image = base64.b64encode(image).decode()
elif isinstance(image, os.PathLike):
with open(image, 'rb') as f:
image = base64.b64encode(f.read()).decode()
async with aiofiles.open(image, 'rb') as f:
image = base64.b64encode(await f.read()).decode()
else:
raise ValueError("Argument 'image' can only be str, bytes, or os.PathLike")
@@ -239,8 +240,8 @@ class MessageMixin(WechatAPIClientBase):
video_path = Path(video)
if not video_path.exists():
raise ValueError(f"Video file does not exist: {video_path}")
with open(video_path, "rb") as f:
video_bytes = f.read()
async with aiofiles.open(video_path, "rb") as f:
video_bytes = await f.read()
file_len = len(video_bytes)
vid_base64 = base64.b64encode(video_bytes).decode()
media_info = MediaInfo.parse(video_path)
@@ -266,8 +267,8 @@ class MessageMixin(WechatAPIClientBase):
elif isinstance(image, bytes):
image_base64 = base64.b64encode(image).decode()
elif isinstance(image, os.PathLike):
with open(image, "rb") as f:
image_base64 = base64.b64encode(f.read()).decode()
async with aiofiles.open(image, "rb") as f:
image_base64 = base64.b64encode(await f.read()).decode()
else:
raise ValueError("image should be str, bytes, or path")
# self.logging.debug(f"vid_base64:{vid_base64}")
@@ -327,8 +328,8 @@ class MessageMixin(WechatAPIClientBase):
elif isinstance(voice, bytes):
voice_byte = voice
elif isinstance(voice, os.PathLike):
with open(voice, "rb") as f:
voice_byte = f.read()
async with aiofiles.open(voice, "rb") as f:
voice_byte = await f.read()
else:
raise ValueError("voice should be str, bytes, or path")
voice_type = 0

View File

@@ -1,6 +1,7 @@
import base64
import io
import os
import aiofiles
import aiohttp
import pysilk
@@ -234,7 +235,7 @@ class ToolMixin(WechatAPIClientBase):
return False
@staticmethod
def base64_to_file(base64_str: str, file_name: str, file_path: str) -> bool:
async def base64_to_file(base64_str: str, file_name: str, file_path: str) -> bool:
"""将base64字符串转换为文件并保存。
Args:
@@ -256,8 +257,8 @@ class ToolMixin(WechatAPIClientBase):
base64_str = base64_str.split(',')[1]
# 解码 base64 并写入文件
with open(full_path, 'wb') as f:
f.write(base64.b64decode(base64_str))
async with aiofiles.open(full_path, 'wb') as f:
await f.write(base64.b64decode(base64_str))
return True
@@ -265,7 +266,7 @@ class ToolMixin(WechatAPIClientBase):
return False
@staticmethod
def file_to_base64(file_path: str) -> str:
async def file_to_base64(file_path: str) -> str:
"""将文件转换为base64字符串。
Args:
@@ -274,8 +275,8 @@ class ToolMixin(WechatAPIClientBase):
Returns:
str: base64编码的字符串
"""
with open(file_path, 'rb') as f:
return base64.b64encode(f.read()).decode()
async with aiofiles.open(file_path, 'rb') as f:
return base64.b64encode(await f.read()).decode()
@staticmethod
def base64_to_byte(base64_str: str) -> bytes: