优化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