视频功能优化
This commit is contained in:
@@ -151,7 +151,14 @@ class VideoPlugin(MessagePluginInterface):
|
|||||||
try:
|
try:
|
||||||
# 发送GET请求,启用流式传输
|
# 发送GET请求,启用流式传输
|
||||||
self.LOG.info(f"开始从 {url} 下载视频")
|
self.LOG.info(f"开始从 {url} 下载视频")
|
||||||
response = requests.get(url, stream=True, timeout=30)
|
headers = {
|
||||||
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
||||||
|
"Accept": "*/*",
|
||||||
|
"Accept-Encoding": "gzip, deflate, br",
|
||||||
|
"Connection": "keep-alive",
|
||||||
|
"Referer": "https://api.guiguiya.com/"
|
||||||
|
}
|
||||||
|
response = requests.get(url, stream=True, timeout=30, headers=headers, allow_redirects=True)
|
||||||
|
|
||||||
# 检查请求是否成功
|
# 检查请求是否成功
|
||||||
response.raise_for_status() # 如果状态码不是200,将抛出异常
|
response.raise_for_status() # 如果状态码不是200,将抛出异常
|
||||||
@@ -173,18 +180,41 @@ class VideoPlugin(MessagePluginInterface):
|
|||||||
|
|
||||||
# 以二进制写入模式保存流数据
|
# 以二进制写入模式保存流数据
|
||||||
try:
|
try:
|
||||||
|
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:
|
with open(save_path, "wb") as file:
|
||||||
for chunk in response.iter_content(chunk_size=1024): # 分块读取,每块1KB
|
for chunk in response.iter_content(chunk_size=1024): # 分块读取,每块1KB
|
||||||
if chunk: # 过滤空块
|
if chunk: # 过滤空块
|
||||||
file.write(chunk)
|
file.write(chunk)
|
||||||
self.LOG.info(f"视频已下载到: {save_path}")
|
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} 字节")
|
||||||
|
return None, None
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
self.LOG.error(f"文件写入失败: {e}")
|
self.LOG.error(f"文件写入失败: {e}")
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
# 检查文件是否存在且大小大于0
|
# 检查文件是否存在且大小合理
|
||||||
if not os.path.exists(save_path) or os.path.getsize(save_path) == 0:
|
if not os.path.exists(save_path):
|
||||||
self.LOG.error(f"下载的文件不存在或为空: {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} 字节")
|
||||||
|
# 尝试读取文件内容以诊断问题
|
||||||
|
try:
|
||||||
|
with open(save_path, 'rb') as f:
|
||||||
|
content_preview = f.read(200)
|
||||||
|
self.LOG.warning(f"文件内容预览(十六进制): {content_preview.hex()[:100]}")
|
||||||
|
except Exception as e:
|
||||||
|
self.LOG.error(f"读取文件内容失败: {e}")
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
# 加入首帧下载
|
# 加入首帧下载
|
||||||
|
|||||||
Reference in New Issue
Block a user