Skip to content

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

refactor(bili_pack): 将FFmpeg相关功能拆分为独立的功能包

XiaoYouChRaca2a106b466
diff --git a/features/bili_pack/manifest.toml b/features/bili_pack/manifest.toml
index 784ec150..209f7371 100644
--- a/features/bili_pack/manifest.toml
+++ b/features/bili_pack/manifest.toml
@@ -1,3 +1,3 @@
[pack]
entry = "pack.py"
-dependencies = ["http_pack"]
+dependencies = ["http_pack", "ffmpeg_pack"]
diff --git a/features/bili_pack/pack.py b/features/bili_pack/pack.py
index 2df55d2e..f733f24b 100644
--- a/features/bili_pack/pack.py
+++ b/features/bili_pack/pack.py
@@ -9,13 +9,15 @@ from app.bases.interfaces import FeaturePack
from app.bases.models import Task
from app.supports.config import cfg
from .config import bilibiliConfig
-from .task import BilibiliTask, FFmpegStage
+from .task import BilibiliTask
from app.view.components.cards import UniversalTaskCard, UniversalResultCard
if TYPE_CHECKING:
+ from features.ffmpeg_pack.task import FFmpegStage
from features.http_pack.task import HttpTaskStage
else:
+ from ffmpeg_pack.task import FFmpegStage
from http_pack.task import HttpTaskStage
diff --git a/features/bili_pack/task.py b/features/bili_pack/task.py
index 5bfffa24..798bda82 100644
--- a/features/bili_pack/task.py
+++ b/features/bili_pack/task.py
@@ -1,169 +1,22 @@
import asyncio
-import shutil
-from contextlib import suppress
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, TYPE_CHECKING
from loguru import logger
-from app.bases.interfaces import Worker
-from app.bases.models import Task, TaskStage, TaskStatus
+from app.bases.models import Task, TaskStatus
from app.supports.config import DEFAULT_HEADERS
from app.supports.utils import getProxies
if TYPE_CHECKING:
+ from features.ffmpeg_pack.task import FFmpegStage, FFmpegWorker
from features.http_pack.task import HttpTaskStage, HttpWorker
else:
+ from ffmpeg_pack.task import FFmpegStage, FFmpegWorker
from http_pack.task import HttpTaskStage, HttpWorker
-@dataclass
-class FFmpegStage(TaskStage):
- videoPath: str
- audioPath: str
- resolvePath: str
- cleanupSource: bool = field(default=True)
-
-
-class FFmpegWorker(Worker):
- def __init__(self, stage: FFmpegStage):
- super().__init__(stage)
- self.stage = stage
-
- @staticmethod
- def _parseDuration(value: Any) -> float:
- try:
- duration = float(value)
- except (TypeError, ValueError):
- return 0.0
-
- if duration > 0:
- return duration
- return 0.0
-
- async def _probeDuration(self, ffprobe: str, path: str) -> float:
- process = await asyncio.create_subprocess_exec(
- ffprobe,
- "-v",
- "error",
- "-show_entries",
- "format=duration",
- "-of",
- "default=noprint_wrappers=1:nokey=1",
- path,
- stdout=asyncio.subprocess.PIPE,
- stderr=asyncio.subprocess.PIPE,
- )
- stdout, stderr = await process.communicate()
-
- if process.returncode != 0:
- message = stderr.decode("utf-8", errors="ignore").strip()
- logger.warning(f"ffprobe 获取时长失败: {path}, {message or process.returncode}")
- return 0.0
-
- return self._parseDuration(stdout.decode("utf-8", errors="ignore").strip())
-
- async def _probeVideoDuration(self, ffprobe: str | None) -> float:
- if not ffprobe:
- logger.warning("未找到 ffprobe,视频合并进度将在完成时更新")
- return 0.0
-
- return await self._probeDuration(ffprobe, self.stage.videoPath)
-
- async def _readProgress(self, stream: asyncio.StreamReader | None, totalDuration: float):
- if stream is None:
- return
-
- while True:
- rawLine = await stream.readline()
- if not rawLine:
- break
-
- line = rawLine.decode("utf-8", errors="ignore").strip()
- if not line:
- continue
-
- if line.startswith("out_time_us=") and totalDuration > 0:
- currentDuration = self._parseDuration(line.removeprefix("out_time_us=")) / 1_000_000
- if currentDuration <= 0:
- continue
- self.stage.progress = min(99.5, max(0.0, currentDuration / totalDuration * 100))
- elif line == "progress=end":
- self.stage.progress = 100
-
- async def run(self):
- ffmpeg = shutil.which("ffmpeg")
- if not ffmpeg:
- self.stage.setStatus(TaskStatus.FAILED)
- raise RuntimeError("未找到 ffmpeg,请先将 ffmpeg 加入 PATH")
- ffprobe = shutil.which("ffprobe")
-
- outputPath = Path(self.stage.resolvePath)
- outputPath.parent.mkdir(parents=True, exist_ok=True)
-
- process = None
- progressTask = None
- try:
- self.stage.progress = 0
- self.stage.speed = 0
- self.stage.receivedBytes = 0
- totalDuration = await self._probeVideoDuration(ffprobe)
- process = await asyncio.create_subprocess_exec(
- ffmpeg,
- "-y",
- "-v",
- "error",
- "-nostats",
- "-progress",
- "pipe:1",
- "-i",
- self.stage.videoPath,
- "-i",
- self.stage.audioPath,
- "-c",
- "copy",
- self.stage.resolvePath,
- stdout=asyncio.subprocess.PIPE,
- stderr=asyncio.subprocess.DEVNULL,
- )
- progressTask = asyncio.create_task(self._readProgress(process.stdout, totalDuration))
-
- await process.wait()
- await progressTask
- if process.returncode != 0:
- raise RuntimeError(f"ffmpeg 退出码异常: {process.returncode}")
-
- self.stage.setStatus(TaskStatus.COMPLETED)
- if self.stage.cleanupSource:
- self._cleanupSourceFiles()
- except asyncio.CancelledError:
- self.stage.setStatus(TaskStatus.PAUSED)
- if process is not None and process.returncode is None:
- process.kill()
- await process.wait()
- if progressTask is not None and not progressTask.done():
- progressTask.cancel()
- with suppress(asyncio.CancelledError):
- await progressTask
- raise
- except Exception:
- self.stage.setStatus(TaskStatus.FAILED)
- raise
-
- def _cleanupSourceFiles(self):
- for rawPath in (self.stage.videoPath, self.stage.audioPath):
- target = Path(rawPath)
- for path in (target, Path(str(target) + ".ghd")):
- try:
- if path.is_file() or path.is_symlink():
- path.unlink()
- except FileNotFoundError:
- continue
- except Exception as e:
- logger.error(f"failed to cleanup temporary file {path}: {repr(e)}")
-
-
@dataclass
class BilibiliTask(Task):
url: str
diff --git a/features/ffmpeg_pack/manifest.toml b/features/ffmpeg_pack/manifest.toml
new file mode 100644
index 00000000..90158411
--- /dev/null
+++ b/features/ffmpeg_pack/manifest.toml
@@ -0,0 +1,3 @@
+[pack]
+entry = "pack.py"
+dependencies = []
diff --git a/features/ffmpeg_pack/pack.py b/features/ffmpeg_pack/pack.py
new file mode 100644
index 00000000..843e266e
--- /dev/null
+++ b/features/ffmpeg_pack/pack.py
@@ -0,0 +1,6 @@
+from app.bases.interfaces import FeaturePack
+from .task import FFmpegStage, FFmpegWorker
+
+
+class FFmpegPack(FeaturePack):
+ pass
diff --git a/features/ffmpeg_pack/task.py b/features/ffmpeg_pack/task.py
new file mode 100644
index 00000000..6e59fc74
--- /dev/null
+++ b/features/ffmpeg_pack/task.py
@@ -0,0 +1,157 @@
+import asyncio
+import shutil
+from contextlib import suppress
+from dataclasses import dataclass, field
+from pathlib import Path
+from typing import Any
+
+from loguru import logger
+
+from app.bases.interfaces import Worker
+from app.bases.models import TaskStage, TaskStatus
+
+
+@dataclass
+class FFmpegStage(TaskStage):
+ videoPath: str
+ audioPath: str
+ resolvePath: str
+ cleanupSource: bool = field(default=True)
+
+
+class FFmpegWorker(Worker):
+ def __init__(self, stage: FFmpegStage):
+ super().__init__(stage)
+ self.stage = stage
+
+ @staticmethod
+ def _parseDuration(value: Any) -> float:
+ try:
+ duration = float(value)
+ except (TypeError, ValueError):
+ return 0.0
+
+ if duration > 0:
+ return duration
+ return 0.0
+
+ async def _probeDuration(self, ffprobe: str, path: str) -> float:
+ process = await asyncio.create_subprocess_exec(
+ ffprobe,
+ "-v",
+ "error",
+ "-show_entries",
+ "format=duration",
+ "-of",
+ "default=noprint_wrappers=1:nokey=1",
+ path,
+ stdout=asyncio.subprocess.PIPE,
+ stderr=asyncio.subprocess.PIPE,
+ )
+ stdout, stderr = await process.communicate()
+
+ if process.returncode != 0:
+ message = stderr.decode("utf-8", errors="ignore").strip()
+ logger.warning(f"ffprobe 获取时长失败: {path}, {message or process.returncode}")
+ return 0.0
+
+ return self._parseDuration(stdout.decode("utf-8", errors="ignore").strip())
+
+ async def _probeVideoDuration(self, ffprobe: str | None) -> float:
+ if not ffprobe:
+ logger.warning("未找到 ffprobe,视频合并进度将在完成时更新")
+ return 0.0
+
+ return await self._probeDuration(ffprobe, self.stage.videoPath)
+
+ async def _readProgress(self, stream: asyncio.StreamReader | None, totalDuration: float):
+ if stream is None:
+ return
+
+ while True:
+ rawLine = await stream.readline()
+ if not rawLine:
+ break
+
+ line = rawLine.decode("utf-8", errors="ignore").strip()
+ if not line:
+ continue
+
+ if line.startswith("out_time_us=") and totalDuration > 0:
+ currentDuration = self._parseDuration(line.removeprefix("out_time_us=")) / 1_000_000
+ if currentDuration <= 0:
+ continue
+ self.stage.progress = min(99.5, max(0.0, currentDuration / totalDuration * 100))
+ elif line == "progress=end":
+ self.stage.progress = 100
+
+ async def run(self):
+ ffmpeg = shutil.which("ffmpeg")
+ if not ffmpeg:
+ self.stage.setStatus(TaskStatus.FAILED)
+ raise RuntimeError("未找到 ffmpeg,请先将 ffmpeg 加入 PATH")
+ ffprobe = shutil.which("ffprobe")
+
+ outputPath = Path(self.stage.resolvePath)
+ outputPath.parent.mkdir(parents=True, exist_ok=True)
+
+ process = None
+ progressTask = None
+ try:
+ self.stage.progress = 0
+ self.stage.speed = 0
+ self.stage.receivedBytes = 0
+ totalDuration = await self._probeVideoDuration(ffprobe)
+ process = await asyncio.create_subprocess_exec(
+ ffmpeg,
+ "-y",
+ "-v",
+ "error",
+ "-nostats",
+ "-progress",
+ "pipe:1",
+ "-i",
+ self.stage.videoPath,
+ "-i",
+ self.stage.audioPath,
+ "-c",
+ "copy",
+ self.stage.resolvePath,
+ stdout=asyncio.subprocess.PIPE,
+ stderr=asyncio.subprocess.DEVNULL,
+ )
+ progressTask = asyncio.create_task(self._readProgress(process.stdout, totalDuration))
+
+ await process.wait()
+ await progressTask
+ if process.returncode != 0:
+ raise RuntimeError(f"ffmpeg 退出码异常: {process.returncode}")
+
+ self.stage.setStatus(TaskStatus.COMPLETED)
+ if self.stage.cleanupSource:
+ self._cleanupSourceFiles()
+ except asyncio.CancelledError:
+ self.stage.setStatus(TaskStatus.PAUSED)
+ if process is not None and process.returncode is None:
+ process.kill()
+ await process.wait()
+ if progressTask is not None and not progressTask.done():
+ progressTask.cancel()
+ with suppress(asyncio.CancelledError):
+ await progressTask
+ raise
+ except Exception:
+ self.stage.setStatus(TaskStatus.FAILED)
+ raise
+
+ def _cleanupSourceFiles(self):
+ for rawPath in (self.stage.videoPath, self.stage.audioPath):
+ target = Path(rawPath)
+ for path in (target, Path(str(target) + ".ghd")):
+ try:
+ if path.is_file() or path.is_symlink():
+ path.unlink()
+ except FileNotFoundError:
+ continue
+ except Exception as e:
+ logger.error(f"failed to cleanup temporary file {path}: {repr(e)}")