测试抖音视频

This commit is contained in:
liuwei
2025-03-12 09:26:22 +08:00
parent 1979d3703d
commit cfbd9b54e5

View File

@@ -1,4 +1,5 @@
import logging
import os
import re
import time
import tomllib
@@ -138,8 +139,8 @@ class DouyinParser:
if data.get("code") == 200:
result = data.get("data", {})
if result.get('video'):
result['video'] = self._get_real_video_url(result['video'])
# if result.get('video'):
# result['video'] = self._get_real_video_url(result['video'])
return self._clean_response_data(result)
else:
raise DouyinParserError(data.get("message", "未知错误"))
@@ -179,7 +180,47 @@ class DouyinParser:
self.wcf.send_rich_text("bot", "gh_11", title[:30], f"{title[:30]} - {author[:10]}", video_url, cover,
message.roomid)
mp4_path = self.download_stream(video_url, "douyin_parser/down_load_dir/douyin.mp4")
self.LOG.info(f"发送抖音视频:{mp4_path}")
self.wcf.send_file(mp4_path, message.roomid)
except Exception as e:
self.LOG.error("[抖音] 解析过程发生未知错误: %s\n%s", str(e), traceback.format_exc())
raise DouyinParserError(f"未知错误: {str(e)}")
return
def download_stream(self, url, save_path):
"""
从指定URL读取视频流并保存到本地
:param url: 视频流的URL
:param save_path: 本地保存路径(包含文件名,例如 "video.mp4"
"""
try:
# 发送GET请求启用流式传输
response = requests.get(url, stream=True)
# 检查请求是否成功
response.raise_for_status() # 如果状态码不是200将抛出异常
# 确保保存路径的目录存在
os.makedirs(os.path.dirname(save_path) or ".", exist_ok=True)
# 检查是否是视频流可选根据Content-Type判断
content_type = response.headers.get("Content-Type", "").lower()
if "video" not in content_type and "application/octet-stream" not in content_type:
print(f"警告: 返回的可能不是视频流Content-Type: {content_type}")
print("响应内容预览:", response.text[:100]) # 打印前100字符查看
return
# 以二进制写入模式保存流数据
with open(save_path, "wb") as file:
for chunk in response.iter_content(chunk_size=1024): # 分块读取每块1KB
if chunk: # 过滤空块
file.write(chunk)
print(f"视频已下载到: {save_path}")
return os.path.abspath(save_path)
except requests.RequestException as e:
print(f"请求失败: {e}")
except IOError as e:
print(f"文件写入失败: {e}")
except Exception as e:
print(f"发生未知错误: {e}")