Skip to content

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

fix: FFmpeg 路径错误找不到文件

XiaoYouChR377d3668b781
diff --git a/features/ffmpeg_pack/pack.py b/features/ffmpeg_pack/pack.py
index bd7a8943..6a65fef7 100644
--- a/features/ffmpeg_pack/pack.py
+++ b/features/ffmpeg_pack/pack.py
@@ -12,7 +12,7 @@ from app.bases.models import Task, SpecialFileSize
from app.supports.config import DEFAULT_HEADERS, cfg
from app.supports.utils import getProxies, toExecutable, toSafeFilename
from .config import ffmpegConfig, ffmpegPaths
-from .task import FFmpegStage
+from .task import FFmpegResourceStage, FFmpegStage
if TYPE_CHECKING:
from features.disk_pack.pack import buildToolInstallTask
@@ -143,20 +143,18 @@ async def createMergeTask(payload: dict[str, Any]) -> Task:
baseTitle = toSafeFilename(rawTitle, fallback="merged-media")
outputTitle = baseTitle if baseTitle.lower().endswith(".mp4") else f"{baseTitle}.mp4"
- videoStage: HttpTaskStage = videoTask.stages[0]
- audioStage: HttpTaskStage = audioTask.stages[0]
- videoExt = _resourceExtension(videoTask.title, videoStage.url)
- audioExt = _resourceExtension(audioTask.title, audioStage.url)
+ videoSource: HttpTaskStage = videoTask.stages[0]
+ audioSource: HttpTaskStage = audioTask.stages[0]
+ videoExt = _resourceExtension(videoTask.title, videoSource.url)
+ audioExt = _resourceExtension(audioTask.title, audioSource.url)
- finalPath = path / outputTitle
- stem = finalPath.stem
- videoPath = finalPath.with_name(f"{stem}.video.{videoExt}" if videoExt else f"{stem}.video")
- audioPath = finalPath.with_name(f"{stem}.audio.{audioExt}" if audioExt else f"{stem}.audio")
-
- videoStage.stageIndex = 1
- videoStage.outputFile = str(videoPath)
- audioStage.stageIndex = 2
- audioStage.outputFile = str(audioPath)
+ videoStage = _toResourceStage(videoSource, role="video", extension=videoExt, stageIndex=1)
+ audioStage = _toResourceStage(audioSource, role="audio", extension=audioExt, stageIndex=2)
+ mergeStage = FFmpegStage(
+ stageIndex=3,
+ videoExtension=videoExt,
+ audioExtension=audioExt,
+ )
task = Task(
title=outputTitle,
@@ -171,15 +169,30 @@ async def createMergeTask(payload: dict[str, Any]) -> Task:
)
task.addStage(videoStage)
task.addStage(audioStage)
- task.addStage(FFmpegStage(
- stageIndex=3,
- videoPath=videoPath,
- audioPath=audioPath,
- outputFile=finalPath,
- ))
+ task.addStage(mergeStage)
+ # 触发新 stage 按 path/title 重算自身的输出路径
+ for stage in task.stages:
+ stage.updateOutputFile(task.path, task.title)
return task
+def _toResourceStage(
+ httpStage: "HttpTaskStage", *, role: str, extension: str, stageIndex: int,
+) -> FFmpegResourceStage:
+ return FFmpegResourceStage(
+ stageIndex=stageIndex,
+ url=httpStage.url,
+ fileSize=httpStage.fileSize,
+ headers=httpStage.headers,
+ proxies=httpStage.proxies,
+ outputFile=httpStage.outputFile,
+ blockNum=httpStage.blockNum,
+ supportsRange=httpStage.supportsRange,
+ role=role,
+ extension=extension,
+ )
+
+
async def createInstallTask() -> Task:
target, archLabel = _windowsTarget()
assetInfo = await _fetchLatestRelease(target)
diff --git a/features/ffmpeg_pack/task.py b/features/ffmpeg_pack/task.py
index 1dcd12d5..77c6de44 100644
--- a/features/ffmpeg_pack/task.py
+++ b/features/ffmpeg_pack/task.py
@@ -2,6 +2,7 @@ import asyncio
from contextlib import suppress
from dataclasses import dataclass, field
from pathlib import Path
+from typing import TYPE_CHECKING
from loguru import logger
@@ -9,6 +10,26 @@ from app.bases.interfaces import Worker
from app.bases.models import TaskStage, TaskStatus
from .config import ffmpegPaths
+if TYPE_CHECKING:
+ from features.http_pack.task import HttpTaskStage
+else:
+ from http_pack.task import HttpTaskStage
+
+
+def _baseTitle(taskTitle: str) -> str:
+ return taskTitle[:-4] if taskTitle.lower().endswith(".mp4") else taskTitle
+
+
+@dataclass(kw_only=True)
+class FFmpegResourceStage(HttpTaskStage):
+ # role ∈ {"video", "audio"}:合并任务里两路源各自的角色,落盘文件名带这个中缀
+ role: str = "video"
+ extension: str = ""
+
+ def updateOutputFile(self, taskPath: Path, taskTitle: str):
+ suffix = f".{self.extension}" if self.extension else ""
+ self.outputFile = str(taskPath / f"{_baseTitle(taskTitle)}.{self.role}{suffix}")
+
def _parseDuration(value: str) -> float:
try:
@@ -46,8 +67,18 @@ class FFmpegStage(TaskStage):
videoPath: Path = field(default_factory=Path)
audioPath: Path = field(default_factory=Path)
outputFile: Path = field(default_factory=Path)
+ videoExtension: str = ""
+ audioExtension: str = ""
cleanupSource: bool = True
+ def updateOutputFile(self, taskPath: Path, taskTitle: str):
+ baseTitle = _baseTitle(taskTitle)
+ videoSuffix = f".{self.videoExtension}" if self.videoExtension else ""
+ audioSuffix = f".{self.audioExtension}" if self.audioExtension else ""
+ self.outputFile = taskPath / f"{baseTitle}.mp4"
+ self.videoPath = taskPath / f"{baseTitle}.video{videoSuffix}"
+ self.audioPath = taskPath / f"{baseTitle}.audio{audioSuffix}"
+
class FFmpegWorker(Worker):
def __init__(self, stage: FFmpegStage):