Skip to content

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

fix(m3u8): 修复进度解析不到的问题

XiaoYouChR7f3cc9ba903e
diff --git a/features/m3u8_pack/task.py b/features/m3u8_pack/task.py
index d4f1a6a9..577238e4 100644
--- a/features/m3u8_pack/task.py
+++ b/features/m3u8_pack/task.py
@@ -18,8 +18,13 @@ VOD_PROGRESS_PATTERN = re.compile(
r"(?:(?:(\d+\.\d+)(KB|MB|GB|B)/(\d+\.\d+)(KB|MB|GB|B))|-)?\s*"
r"(\d+\.\d+)(GBps|MBps|KBps|Bps)"
)
+_DURATION = r"(?:\d{2}h)?\d{2}m\d{2}s"
+_SPEED = r"(\d+\.\d+)(GBps|MBps|KBps|Bps)"
LIVE_PROGRESS_PATTERN = re.compile(
- r"(\d{2}m\d{2}s)/(\d{2}m\d{2}s)\s+\d+/\d+\s+(Recording|Waiting)\s+(\d+)%\s+(-|(\d+\.\d+)(GBps|MBps|KBps|Bps))"
+ rf"({_DURATION})/({_DURATION})\s+\d+/\d+\s+(Recording|Waiting)\s+(\d+)%\s+(-|{_SPEED})"
+)
+LIVE_RECORD_PATTERN = re.compile(
+ rf"({_DURATION})/({_DURATION})\s+\d+\.\d+(?:KB|MB|GB|B)\s+\d+/\d+\s+(Recording|Waiting)\s+(-|{_SPEED})"
)
IGNORED_OUTPUT_SUFFIXES = {".json", ".txt", ".log", ".tmp", ".ghd"}
DECRYPTION_ENGINES = {
@@ -275,9 +280,11 @@ class M3U8TaskStep(TaskStep):
if not text:
return
- self.lastMessage = text[:1000]
+ self.lastMessage = text[-1000:]
- vodMatch = VOD_PROGRESS_PATTERN.search(text)
+ vodMatch = None
+ for m in VOD_PROGRESS_PATTERN.finditer(text):
+ vodMatch = m
if vodMatch:
self.progress = float(vodMatch.group(3))
if vodMatch.group(4):
@@ -288,13 +295,25 @@ class M3U8TaskStep(TaskStep):
self.speed = toBytes(vodMatch.group(8), vodMatch.group(9))
return
- liveMatch = LIVE_PROGRESS_PATTERN.search(text)
+ liveMatch = None
+ for m in LIVE_PROGRESS_PATTERN.finditer(text):
+ liveMatch = m
if liveMatch:
self.liveElapsed = liveMatch.group(1)
self.liveTotal = liveMatch.group(2)
self.liveStatus = liveMatch.group(3)
self.progress = float(liveMatch.group(4))
self.speed = 0 if liveMatch.group(5) == "-" else toBytes(liveMatch.group(6), liveMatch.group(7))
+ return
+
+ recordMatch = None
+ for m in LIVE_RECORD_PATTERN.finditer(text):
+ recordMatch = m
+ if recordMatch:
+ self.liveElapsed = recordMatch.group(1)
+ self.liveTotal = recordMatch.group(2)
+ self.liveStatus = recordMatch.group(3)
+ self.speed = 0 if recordMatch.group(4) == "-" else toBytes(recordMatch.group(5), recordMatch.group(6))
async def _readOutput(self, stream: asyncio.StreamReader):
buffer = ""
@@ -308,6 +327,10 @@ class M3U8TaskStep(TaskStep):
buffer = lines.pop()
for line in lines:
self._parseOutputLine(line)
+ # NonAnsiWriter 可能剥掉换行符,进度更新堆积在 buffer 里
+ self._parseOutputLine(buffer)
+ if len(buffer) > 8192:
+ buffer = buffer[-4096:]
if buffer.strip():
self._parseOutputLine(buffer)
diff --git a/tests/test_m3u8_progress.py b/tests/test_m3u8_progress.py
index 9dff7432..1c6acfaf 100644
--- a/tests/test_m3u8_progress.py
+++ b/tests/test_m3u8_progress.py
@@ -77,3 +77,91 @@ class TestVodProgressLegacy:
assert step.speed == toBytes("1.23", "MBps")
assert step.receivedBytes == toBytes("12.34", "MB")
assert step.task.fileSize == toBytes("56.78", "MB")
+
+
+class TestConcatenatedBuffer:
+ """NonAnsiWriter 剥掉换行符后,多条进度拼成一串——取最后一条。"""
+
+ def test_takes_last_vod_match(self):
+ step = makeStep()
+ blob = (
+ "Vid 1280x720 | 1650 Kbps | 30 ━━━━━━ 3/155 1.94% -0.00Bps00:00:37"
+ "Vid 1280x720 | 1650 Kbps | 30 ━━━━━━ 35/155 22.58% 30.99MB/137.26MB28.45MBps00:00:06"
+ "Vid 1280x720 | 1650 Kbps | 30 ━━━━━━ 100/155 64.52% 71.82MB/148.43MB40.83MBps00:00:02"
+ )
+ step._parseOutputLine(blob)
+ assert step.progress == pytest.approx(64.52)
+ assert step.speed == toBytes("40.83", "MBps")
+ assert step.receivedBytes == toBytes("71.82", "MB")
+ assert step.task.fileSize == toBytes("148.43", "MB")
+
+ def test_last_message_takes_tail(self):
+ step = makeStep()
+ filler = "x" * 2000
+ step._parseOutputLine(filler)
+ assert step.lastMessage == filler[-1000:]
+
+
+class TestLiveProgress:
+ """SimpleLiveRecordManager2 的直播进度格式。"""
+
+ def test_basic(self):
+ step = makeStep()
+ step._parseOutputLine(
+ "Vid ━━━━━━ 01m30s/02m00s 125/250 Recording 50% 1.55MBps"
+ )
+ assert step.liveElapsed == "01m30s"
+ assert step.liveTotal == "02m00s"
+ assert step.liveStatus == "Recording"
+ assert step.progress == pytest.approx(50)
+ assert step.speed == toBytes("1.55", "MBps")
+
+ def test_over_one_hour(self):
+ step = makeStep()
+ step._parseOutputLine(
+ "Vid ━━━━━━ 01h05m30s/01h30m00s 3000/4500 Recording 66% 2.10MBps"
+ )
+ assert step.liveElapsed == "01h05m30s"
+ assert step.liveTotal == "01h30m00s"
+ assert step.progress == pytest.approx(66)
+
+ def test_waiting_with_dash_speed(self):
+ step = makeStep()
+ step._parseOutputLine(
+ "Vid ━━━━━━ 00m00s/00m00s 0/0 Waiting 0% -"
+ )
+ assert step.liveStatus == "Waiting"
+ assert step.progress == pytest.approx(0)
+ assert step.speed == 0
+
+
+class TestLiveRecordProgress:
+ """HTTPLiveRecordManager 的直播录制格式——无百分比。"""
+
+ def test_basic(self):
+ step = makeStep()
+ step._parseOutputLine(
+ "Vid ━━━━━━ 01m30s/02m00s 130.00MB 125/250 Recording 1.55MBps"
+ )
+ assert step.liveElapsed == "01m30s"
+ assert step.liveTotal == "02m00s"
+ assert step.liveStatus == "Recording"
+ assert step.speed == toBytes("1.55", "MBps")
+ assert step.progress == 0
+
+ def test_over_one_hour(self):
+ step = makeStep()
+ step._parseOutputLine(
+ "Vid ━━━━━━ 02h10m00s/02h15m30s 5.12GB 9000/9500 Recording 30.00MBps"
+ )
+ assert step.liveElapsed == "02h10m00s"
+ assert step.liveTotal == "02h15m30s"
+ assert step.speed == toBytes("30.00", "MBps")
+
+ def test_dash_speed(self):
+ step = makeStep()
+ step._parseOutputLine(
+ "Vid ━━━━━━ 00m10s/00m30s 0.50MB 5/15 Recording -"
+ )
+ assert step.liveStatus == "Recording"
+ assert step.speed == 0