debug 视频
This commit is contained in:
@@ -65,7 +65,7 @@ class VideoPlugin(MessagePluginInterface):
|
||||
if not os.path.exists(self.download_dir):
|
||||
self.LOG.warning(f"下载目录不存在,正在创建: {self.download_dir}")
|
||||
os.makedirs(self.download_dir, exist_ok=True)
|
||||
|
||||
|
||||
# 检查目录权限
|
||||
if not os.access(self.download_dir, os.W_OK):
|
||||
self.LOG.error(f"下载目录没有写入权限: {self.download_dir}")
|
||||
@@ -119,7 +119,7 @@ class VideoPlugin(MessagePluginInterface):
|
||||
video_filename = f"video_{int(time.time())}.mp4"
|
||||
save_path = os.path.join(self.download_dir, video_filename)
|
||||
self.LOG.info(f"开始下载视频到: {save_path}")
|
||||
|
||||
|
||||
file_abspath, first_frame = self._download_stream("https://api.guiguiya.com/api/hook/heisis", save_path)
|
||||
|
||||
if not file_abspath or not os.path.exists(file_abspath) or not file_abspath.endswith("mp4"):
|
||||
@@ -130,7 +130,8 @@ class VideoPlugin(MessagePluginInterface):
|
||||
|
||||
# 发送视频
|
||||
self.LOG.info(f"准备发送视频: {file_abspath}, 首帧: {first_frame}")
|
||||
result = await self.bot.send_video_message((roomid if roomid else sender), file_abspath, first_frame)
|
||||
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, "发送成功"
|
||||
|
||||
@@ -170,7 +171,7 @@ class VideoPlugin(MessagePluginInterface):
|
||||
# 检查是否是视频流(可选,根据Content-Type判断)
|
||||
content_type = response.headers.get("Content-Type", "").lower()
|
||||
self.LOG.info(f"响应Content-Type: {content_type}")
|
||||
|
||||
|
||||
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字符查看
|
||||
@@ -181,15 +182,15 @@ class VideoPlugin(MessagePluginInterface):
|
||||
total_size = int(response.headers.get('Content-Length', 0))
|
||||
self.LOG.info(f"预期下载大小: {total_size} 字节")
|
||||
downloaded_size = 0
|
||||
|
||||
|
||||
with open(save_path, "wb") as file:
|
||||
for chunk in response.iter_content(chunk_size=1024): # 分块读取,每块1KB
|
||||
if chunk: # 过滤空块
|
||||
file.write(chunk)
|
||||
downloaded_size += len(chunk)
|
||||
|
||||
|
||||
self.LOG.info(f"视频已下载到: {save_path}, 大小: {downloaded_size} 字节")
|
||||
|
||||
|
||||
# 验证下载是否完整
|
||||
if total_size > 0 and downloaded_size < total_size * 0.9: # 如果下载不到预期大小的90%
|
||||
self.LOG.error(f"下载不完整: 预期 {total_size} 字节, 实际 {downloaded_size} 字节")
|
||||
@@ -197,12 +198,12 @@ class VideoPlugin(MessagePluginInterface):
|
||||
except IOError as e:
|
||||
self.LOG.error(f"文件写入失败: {e}")
|
||||
return None, None
|
||||
|
||||
|
||||
# 检查文件是否存在且大小合理
|
||||
if not os.path.exists(save_path):
|
||||
self.LOG.error(f"下载的文件不存在: {save_path}")
|
||||
return None, None
|
||||
|
||||
|
||||
file_size = os.path.getsize(save_path)
|
||||
if file_size < 10000: # 小于10KB的文件可能不是有效视频
|
||||
self.LOG.error(f"下载的文件太小,可能不是有效视频: {file_size} 字节")
|
||||
@@ -214,17 +215,17 @@ class VideoPlugin(MessagePluginInterface):
|
||||
except Exception as e:
|
||||
self.LOG.error(f"读取文件内容失败: {e}")
|
||||
return None, None
|
||||
|
||||
|
||||
# 加入首帧下载
|
||||
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)
|
||||
|
||||
|
||||
if not first_frame or not os.path.exists(first_frame):
|
||||
self.LOG.warning(f"无法提取首帧,使用默认图片")
|
||||
# 可以在这里设置一个默认图片路径
|
||||
|
||||
|
||||
return os.path.abspath(save_path), first_frame
|
||||
|
||||
|
||||
except requests.RequestException as e:
|
||||
self.LOG.error(f"请求失败: {e}")
|
||||
except IOError as e:
|
||||
@@ -267,7 +268,7 @@ class VideoPlugin(MessagePluginInterface):
|
||||
# 释放资源
|
||||
cap.release()
|
||||
return os.path.abspath(output_path)
|
||||
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"提取视频首帧时出错: {e}")
|
||||
return None
|
||||
|
||||
@@ -245,7 +245,7 @@ class MessageMixin(WechatAPIClientBase):
|
||||
image_base64 = base64.b64encode(f.read()).decode()
|
||||
else:
|
||||
raise ValueError("image should be str, bytes, or path")
|
||||
|
||||
self.logging.debug(f"images_base64:{image_base64}")
|
||||
# 打印预估时间,300KB/s
|
||||
predict_time = int(file_len / 1024 / 300)
|
||||
self.logging.info("开始发送视频: 对方wxid:{} 视频base64略 图片base64略 预计耗时:{}秒", wxid, predict_time)
|
||||
|
||||
Reference in New Issue
Block a user