Skip to content

Commit 7b9db2f

Browse files
authored
Merge pull request #726 from LePao1/main
feat(bilibili):增加视频清晰度参数,可以通过`BILI_QN`更改下载的视频清晰度;
2 parents e255428 + 3954c40 commit 7b9db2f

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

config/bilibili_config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
# 搜索模式
3535
BILI_SEARCH_MODE = "normal"
3636

37+
# 视频清晰度(qn)配置,常见取值:
38+
# 16=360p, 32=480p, 64=720p, 80=1080p, 112=1080p高码率, 116=1080p60, 120=4K
39+
# 注意:更高清晰度需要账号/视频本身支持
40+
BILI_QN = 80
41+
3742
# 是否爬取用户信息
3843
CREATOR_MODE = True
3944

media_platform/bilibili/client.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,11 @@ async def get_video_play_url(self, aid: int, cid: int) -> Dict:
189189
if not aid or not cid or aid <= 0 or cid <= 0:
190190
raise ValueError("aid 和 cid 必须存在")
191191
uri = "/x/player/wbi/playurl"
192+
qn_value = getattr(config, "BILI_QN", 80)
192193
params = {
193194
"avid": aid,
194195
"cid": cid,
195-
"qn": 80,
196+
"qn": qn_value,
196197
"fourk": 1,
197198
"fnval": 1,
198199
"platform": "pc",
@@ -201,15 +202,17 @@ async def get_video_play_url(self, aid: int, cid: int) -> Dict:
201202
return await self.get(uri, params, enable_params_sign=True)
202203

203204
async def get_video_media(self, url: str) -> Union[bytes, None]:
204-
async with httpx.AsyncClient(proxy=self.proxy) as client:
205+
# Follow CDN 302 redirects and treat any 2xx as success (some endpoints return 206)
206+
async with httpx.AsyncClient(proxy=self.proxy, follow_redirects=True) as client:
205207
try:
206208
response = await client.request("GET", url, timeout=self.timeout, headers=self.headers)
207209
response.raise_for_status()
208-
if not response.reason_phrase == "OK":
209-
utils.logger.error(f"[BilibiliClient.get_video_media] request {url} err, res:{response.text}")
210-
return None
211-
else:
210+
if 200 <= response.status_code < 300:
212211
return response.content
212+
utils.logger.error(
213+
f"[BilibiliClient.get_video_media] Unexpected status {response.status_code} for {url}"
214+
)
215+
return None
213216
except httpx.HTTPError as exc: # some wrong when call httpx.request method, such as connection error, client error, server error or response status code is not 2xx
214217
utils.logger.error(f"[BilibiliClient.get_video_media] {exc.__class__.__name__} for {exc.request.url} - {exc}") # 保留原始异常类型名称,以便开发者调试
215218
return None

0 commit comments

Comments
 (0)