feat(bili): 支持合并B站分离的音视频流并添加请求头支持

添加对B站分离音视频流的合并功能,使用ffmpeg合并m4s格式的视频和音频流
扩展download_file接口支持自定义请求头,用于B站视频下载的Referer校验
This commit is contained in:
2026-03-15 01:34:00 +08:00
parent 958c1df1fc
commit 2a6e9b8f89
2 changed files with 254 additions and 8 deletions

View File

@@ -72,13 +72,14 @@ class LocalFileServer:
url_hash = hashlib.md5(url.encode()).hexdigest()[:16]
return f"file_{url_hash}"
async def download_file(self, url: str, timeout: int = 60) -> Optional[str]:
async def download_file(self, url: str, timeout: int = 60, headers: Optional[Dict[str, str]] = None) -> Optional[str]:
"""
下载远程文件到本地
Args:
url (str): 远程文件 URL
timeout (int): 下载超时时间(秒)
headers (Optional[Dict[str, str]]): 请求头
Returns:
Optional[str]: 本地文件 ID如果失败则返回 None
@@ -96,7 +97,7 @@ class LocalFileServer:
# 使用 aiohttp 下载文件
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=timeout) as response:
async with session.get(url, timeout=timeout, headers=headers) as response:
if response.status != 200:
logger.error(f"[LocalFileServer] 下载失败: HTTP {response.status}")
return None
@@ -195,13 +196,14 @@ async def stop_local_file_server():
_local_file_server = None
async def download_to_local(url: str, timeout: int = 60) -> Optional[str]:
async def download_to_local(url: str, timeout: int = 60, headers: Optional[Dict[str, str]] = None) -> Optional[str]:
"""
下载远程文件到本地并返回本地访问 URL
Args:
url (str): 远程文件 URL
timeout (int): 下载超时时间(秒)
headers (Optional[Dict[str, str]]): 请求头
Returns:
Optional[str]: 本地访问 URL如果失败则返回 None
@@ -210,7 +212,7 @@ async def download_to_local(url: str, timeout: int = 60) -> Optional[str]:
if not server:
return None
file_id = await server.download_file(url, timeout)
file_id = await server.download_file(url, timeout, headers)
if not file_id:
return None