Skip to content

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

fix(M3U8): 适配 N_m3u8DL-RE v0.6.0 进度输出格式

XiaoYouChRa5453c39dced
diff --git a/features/m3u8_pack/task.py b/features/m3u8_pack/task.py
index d007ad02..d4f1a6a9 100644
--- a/features/m3u8_pack/task.py
+++ b/features/m3u8_pack/task.py
@@ -14,7 +14,9 @@ from app.platform.filesystem import deletePath, toPosixPath
from .config import m3u8Runtime
VOD_PROGRESS_PATTERN = re.compile(
- r"(\d+)/(\d+)\s+(\d+\.\d+)%\s+(\d+\.\d+)(KB|MB|GB|B)/(\d+\.\d+)(KB|MB|GB|B)\s+(\d+\.\d+)(GBps|MBps|KBps|Bps)\s+(.+)"
+ r"(\d+)/(\d+)\s+(\d+\.\d+)%\s+"
+ r"(?:(?:(\d+\.\d+)(KB|MB|GB|B)/(\d+\.\d+)(KB|MB|GB|B))|-)?\s*"
+ 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))"
@@ -278,11 +280,12 @@ class M3U8TaskStep(TaskStep):
vodMatch = VOD_PROGRESS_PATTERN.search(text)
if vodMatch:
self.progress = float(vodMatch.group(3))
- self.receivedBytes = toBytes(vodMatch.group(4), vodMatch.group(5))
+ if vodMatch.group(4):
+ self.receivedBytes = toBytes(vodMatch.group(4), vodMatch.group(5))
+ totalSize = toBytes(vodMatch.group(6), vodMatch.group(7))
+ if totalSize > 0:
+ self.task.fileSize = totalSize
self.speed = toBytes(vodMatch.group(8), vodMatch.group(9))
- totalSize = toBytes(vodMatch.group(6), vodMatch.group(7))
- if totalSize > 0:
- self.task.fileSize = totalSize
return
liveMatch = LIVE_PROGRESS_PATTERN.search(text)
diff --git a/tests/test_m3u8_progress.py b/tests/test_m3u8_progress.py
new file mode 100644
index 00000000..9dff7432
--- /dev/null
+++ b/tests/test_m3u8_progress.py
@@ -0,0 +1,79 @@
+"""M3U8TaskStep._parseOutputLine 的输出格式解析测试。
+
+Seam: _parseOutputLine — 输入一行 N_m3u8DL-RE stdout 文本,
+观察 progress / speed / receivedBytes / task.fileSize 的更新。
+"""
+from __future__ import annotations
+
+from dataclasses import dataclass, field
+from pathlib import Path
+
+import pytest
+
+from app.format import toBytes
+from app.models.task import Task, TaskStep, TaskStatus
+from features.m3u8_pack.task import M3U8Task, M3U8TaskStep
+
+
+def makeStep() -> M3U8TaskStep:
+ task = M3U8Task(
+ name="test.mp4",
+ url="http://test",
+ fileSize=1,
+ outputFolder=Path("."),
+ )
+ step = M3U8TaskStep(stepIndex=1)
+ task.addStep(step)
+ return step
+
+
+class TestVodProgressV06:
+ """N_m3u8DL-RE v0.6.0 的 VOD 进度格式——无空格分隔。"""
+
+ def test_with_sizes_no_spaces(self):
+ step = makeStep()
+ step._parseOutputLine(
+ "Vid 1280x720 | 2000 Kbps ━━━━━━ 6/6857 0.09% 3.12MB/3.48GB1.55MBps01:27:07"
+ )
+ assert step.progress == pytest.approx(0.09)
+ assert step.speed == toBytes("1.55", "MBps")
+ assert step.receivedBytes == toBytes("3.12", "MB")
+ assert step.task.fileSize == toBytes("3.48", "GB")
+
+ def test_dash_speed_only(self):
+ step = makeStep()
+ step._parseOutputLine(
+ "Vid 1280x720 | 2000 Kbps ━━━━━━ 0/6857 0.00% -191.58KBps --:--:--"
+ )
+ assert step.progress == pytest.approx(0.0)
+ assert step.speed == toBytes("191.58", "KBps")
+
+ def test_dash_speed_with_retries(self):
+ step = makeStep()
+ step._parseOutputLine(
+ "Vid 1280x720 | 2000 Kbps ━━━━━━ 0/6857 0.00% -0.00Bps(9) --:--:--"
+ )
+ assert step.progress == pytest.approx(0.0)
+ assert step.speed == 0
+
+ def test_zero_bps(self):
+ step = makeStep()
+ step._parseOutputLine(
+ "Vid 1920x1080 | 6221 Kbps ━━━━━━ 0/64 0.00% -0.00Bps --:--:--"
+ )
+ assert step.progress == pytest.approx(0.0)
+ assert step.speed == 0
+
+
+class TestVodProgressLegacy:
+ """旧版 N_m3u8DL-RE 的格式——字段间有空格。"""
+
+ def test_legacy_format(self):
+ step = makeStep()
+ step._parseOutputLine(
+ "12/64 18.75% 12.34MB/56.78MB 1.23MBps 00:01:30"
+ )
+ assert step.progress == pytest.approx(18.75)
+ assert step.speed == toBytes("1.23", "MBps")
+ assert step.receivedBytes == toBytes("12.34", "MB")
+ assert step.task.fileSize == toBytes("56.78", "MB")