Skip to content

The only downloader you need. 下载器的集大成者。

fix(bili): 音质按码率从高到低排列

XiaoYouChRc31334bc3ce1
diff --git a/features/bili_pack/stream.py b/features/bili_pack/stream.py
index 95d248f7..50678196 100644
--- a/features/bili_pack/stream.py
+++ b/features/bili_pack/stream.py
@@ -73,19 +73,22 @@ async def probeSize(url: str, headers: dict | None = None) -> int:
def parseDash(page: BiliPage, dash: dict, *, qn: int, acceptQuality: list[int], audioQn: int = 0) -> None:
videoStreams = list(dash.get("video") or [])
+ aacStreams = sorted(dash.get("audio") or [], key=lambda s: s["bandwidth"], reverse=True)
flac = (dash.get("flac") or {}).get("audio")
- audioStreams = [
- *(dash.get("audio") or []),
+ audioStreams = sorted([
+ *aacStreams,
*((dash.get("dolby") or {}).get("audio") or []),
*([flac] if flac else []),
- ]
+ ], key=lambda s: s["bandwidth"], reverse=True)
if not videoStreams:
raise ValueError("Bilibili 返回结果中不存在可用的媒体流")
video = matchStream(videoStreams, qn, acceptQuality)
audio = None
- if audioStreams:
+ if audioStreams and audioQn:
audioIds = [s.get("id") for s in audioStreams if s.get("id") is not None]
- audio = matchStream(audioStreams, audioQn or None, audioIds if audioQn else None)
+ audio = matchStream(audioStreams, audioQn, audioIds)
+ elif audioStreams:
+ audio = matchStream(aacStreams or audioStreams)
page.videoUrl = toStreamUrl(video)
page.audioUrl = toStreamUrl(audio) if audio else ""
page._videoStreams = videoStreams
diff --git a/tests/bili_pack/test_task_parser.py b/tests/bili_pack/test_task_parser.py
index 232ec026..6d0226ad 100644
--- a/tests/bili_pack/test_task_parser.py
+++ b/tests/bili_pack/test_task_parser.py
@@ -251,6 +251,27 @@ def test_set_audio_quality_picks_dolby_and_hires_tracks():
assert audioStepUrl() == "https://flac"
+def test_parse_lists_audio_from_best_and_defaults_to_aac():
+ from pathlib import Path
+ from bili_pack.stream import parseDash
+ from bili_pack.task import BiliPage, BilibiliTask, audioTiers, currentAudioTier
+
+ page = BiliPage(index=0, relativePath="p", _duration=10)
+ parseDash(page, {
+ "video": [{"id": 80, "codecid": 7, "bandwidth": 8000, "baseUrl": "https://v"}],
+ "audio": [
+ {"id": 30216, "bandwidth": 64000, "baseUrl": "https://a64"},
+ {"id": 30280, "bandwidth": 192000, "baseUrl": "https://a192"},
+ {"id": 30232, "bandwidth": 132000, "baseUrl": "https://a132"},
+ ],
+ "dolby": {"type": 2, "audio": [{"id": 30250, "bandwidth": 448000, "baseUrl": "https://atmos"}]},
+ "flac": {"display": True, "audio": {"id": 30251, "bandwidth": 1000000, "baseUrl": "https://flac"}},
+ }, qn=80, acceptQuality=[80])
+ task = BilibiliTask(name="v.mp4", url="u", outputFolder=Path("."), files=[page], _baseName="v")
+ assert [value for value, _ in audioTiers(task)] == ["30251", "30250", "30280", "30232", "30216"]
+ assert currentAudioTier(task) == "30280"
+
+
async def test_probe_size_from_content_range(startServer):
from aiohttp import web
from bili_pack.stream import probeSize