From 65a55aba4815445af49ef630a7bfce744e35b103 Mon Sep 17 00:00:00 2001 From: Dr_rOot Date: Mon, 12 Oct 2026 07:23:17 +0800 Subject: [PATCH 1/2] feat(media-merge): migrate to declarative plugin operations --- plugins/motrix.media-merge/README.md | 39 ++-- plugins/motrix.media-merge/README.zh-CN.md | 28 ++- plugins/motrix.media-merge/locales/en-US.json | 25 ++- plugins/motrix.media-merge/locales/zh-CN.json | 25 ++- plugins/motrix.media-merge/motrix-plugin.json | 195 ++++++++++++++++-- plugins/motrix.media-merge/src/index.test.ts | 165 +++++++-------- plugins/motrix.media-merge/src/index.ts | 51 +++-- 7 files changed, 364 insertions(+), 164 deletions(-) diff --git a/plugins/motrix.media-merge/README.md b/plugins/motrix.media-merge/README.md index d40eb6b..57f163b 100644 --- a/plugins/motrix.media-merge/README.md +++ b/plugins/motrix.media-merge/README.md @@ -9,21 +9,24 @@ inputs. Unambiguously reversed inputs are detected and swapped automatically. ## Requirements -- Motrix `>=2.0.0-beta.47 <3.0.0` with the **manual media merge interface**. The host change is [Motrix PR #2308](https://github.com/agalwood/Motrix/pull/2308); confirm the minimum release contains it before publishing the plugin. The `.moext` alone does not add the interface to an older build. +- **Motrix 2.0.0-beta.48 or later, below 3.0.0**, with plugin operations v1, composable UI, selected-file grants and the scoped media capability. Beta.48 is the planned first supporting release; this mechanism and plugin remain unpublished. If the host feature ships later, raise the package and registry minimum together before signing. Beta.47 is rejected, including development builds still reporting that version. - FFmpeg configured in **Settings → Integration → Media Tools**. Re-enable the plugin after changing FFmpeg configuration. ## Use -After the signed release is published, install it from the [plugin directory](https://motrix.app/plugins/motrix.media-merge/) and enable it. For local builds, select `dist/artifacts/motrix.media-merge-0.1.0.moext` on the Plugins page. +After the signed release is published, install it from the [plugin directory](https://motrix.app/plugins/motrix.media-merge/) and enable it. Local archives using the `motrix.*` namespace also require a valid official signature before installation. Then either select two completed single-file downloads and choose **Merge audio and video**, or open this plugin's **Operations** tab to select local files. The tab embeds the same form as the task-menu dialog and retains selections when switching tabs. Select a new `.mp4` or `.mkv` output filename and click **Merge**. +**Merge another file** clears both inputs, the output location and the previous result, +while retaining the selected MP4/MKV format. Choose the new files explicitly; the +previous task selection is not filled in again. + The host owns progress, cancellation, temporary files and non-overwriting output -publication. Original files remain untouched. The destination filesystem must -support hard links. The Web UI reuses the server file browser to select inputs and a new output +publication. Original files remain untouched. The host snapshots selected inputs and publishes the output atomically without replacement. The destination filesystem must support private directories and native no-replace publication; unsupported filesystems are rejected. The Web UI reuses the server file browser to select inputs and a new output filename, within its download directory policy. Files on the browser’s computer are not uploaded. @@ -48,14 +51,20 @@ pnpm lint node scripts/pack.mjs motrix.media-merge ``` -The only requested permission is `ffmpeg`. The plugin has no network permission -and registers no task lifecycle hooks. It exposes the existing public-command -contract `motrix.media-merge.mergeStreams` with `{ videoInput, audioInput, -output }` and returns `{ outputPath }` after the operation succeeds. Both the -launch call and its `result` are awaited for compatibility with the asynchronous -QuickJS handle transport. - -This command is reserved for the host's manual merge interface. Its public -declaration enables provider discovery, not cross-plugin execution. The host -rejects generic and cross-plugin calls before the handler runs; do not invoke -it with `commands.execute` or bypass the host's selected-file context. +The plugin declares `contributes.operations` with a private command registered using +`commands.registerOperation`. It requests `ffmpeg`, `files.selected.read` and +`files.selected.write`. These declarations do not grant arbitrary file access: +the host binds opaque selected-file references to one authorized operation. +There is no network permission or download lifecycle hook. + +The handler receives `{ videoInput, audioInput, format, output }`, probes the selected +inputs with `context.media.probe`, swaps clearly reversed tracks, and awaits +`context.media.mergeStreams`. It returns the granted output reference; the host +validates and publishes the file. Generic and cross-plugin command calls cannot +invoke this operation. The manifest owns the form, localized labels, entry +points and error messages; no media-specific renderer is required. + +Inputs are limited to 8 GiB each and 16 GiB per operation. Snapshot and output +staging require free disk space. Cancelling stops host-owned work before temporary +resources are removed; closing the form keeps the job running. Host restarts +interrupt jobs and never replay a merge automatically. diff --git a/plugins/motrix.media-merge/README.zh-CN.md b/plugins/motrix.media-merge/README.zh-CN.md index b210721..f5ccc56 100644 --- a/plugins/motrix.media-merge/README.zh-CN.md +++ b/plugins/motrix.media-merge/README.zh-CN.md @@ -8,17 +8,18 @@ ## 使用要求 -- Motrix `>=2.0.0-beta.47 <3.0.0`,且包含 **手动媒体合并界面**。当前宿主功能见 [Motrix PR #2308](https://github.com/agalwood/Motrix/pull/2308);正式发布插件前必须确认最低版本已包含该功能。单独安装 `.moext` 不会为旧版本补上界面。 +- **Motrix 2.0.0-beta.48 及以上、低于 3.0.0**,并具备插件操作 v1、可组合 UI、所选文件授权和操作上下文媒体能力。beta.48 是计划中的首个支持版本,机制和插件仍未发布;若宿主功能延期,签名前必须同步提高包与注册表的最低版本。beta.47 会被拒绝,包括版本号仍为 beta.47 的开发构建。 - 在 **设置 → 集成 → 媒体工具** 中配置 FFmpeg。修改 FFmpeg 配置后请重新启用插件。 ## 使用方法 -正式签名包发布后,可从 [插件目录](https://motrix.app/zh/plugins/motrix.media-merge/) 安装并启用。也可以在插件页面选择本仓库构建的 `dist/artifacts/motrix.media-merge-0.1.0.moext`。随后可以选中两个 +正式签名包发布后,可从 [插件目录](https://motrix.app/zh/plugins/motrix.media-merge/) 安装并启用。使用 `motrix.*` 命名空间的本地包同样需要有效官方签名才能安装。随后可以选中两个 已完成的单文件下载任务,选择 **合并音频和视频**;也可以进入本插件详情的 **操作** 页,直接选择本地文件。操作页和任务菜单弹窗共用表单,切换 Tab 会保留选择。填写新的 `.mp4` 或 `.mkv` 输出文件名后点击 **合并**。 -进度、取消、临时文件和不覆盖的输出保存由宿主管理。源文件保持不变,目标文件系统需 -支持硬链接。Web UI 复用服务器文件浏览器选择输入和新的输出文件名,并受服务器下载目录策略约束; +点击 **再次合并** 会清空视频、音频、输出位置和上次结果,保留所选 MP4/MKV 格式。新一轮需要重新选择文件,不会再次填入上次选中的下载任务。 + +进度、取消、临时文件和不覆盖的输出保存由宿主管理。源文件保持不变,宿主先为所选输入建立快照,再以原子方式保存结果且不覆盖已有文件。目标文件系统必须支持私有目录和原生无覆盖发布;不满足保证时会拒绝执行。Web UI 复用服务器文件浏览器选择输入和新的输出文件名,并受服务器下载目录策略约束; 不会上传浏览器所在电脑的文件。 本插件不做片段首尾拼接、音画偏移调整或不兼容编码的转码,也不在下载后自动执行。 @@ -41,11 +42,16 @@ pnpm lint node scripts/pack.mjs motrix.media-merge ``` -仅申请 `ffmpeg` 权限,不申请网络权限,也不注册下载任务生命周期 Hook。插件通过既有 -公开命令机制暴露 `motrix.media-merge.mergeStreams`,接收 `{ videoInput, -audioInput, output }`,操作成功后返回 `{ outputPath }`。启动调用和操作句柄的 -`result` 都会被等待,以兼容 QuickJS 异步句柄传输。 +插件通过 `contributes.operations` 声明界面,通过 `commands.registerOperation` +注册私有处理命令,申请 `ffmpeg`、`files.selected.read` 和 `files.selected.write`。 +声明权限不等于获得任意文件访问能力:宿主将用户选择得到的不透明文件引用绑定到单次操作。 +插件不申请网络权限,也不注册下载任务生命周期 Hook。 + +处理函数接收 `{ videoInput, audioInput, format, output }`,通过 `context.media.probe` +探测输入,自动交换明确选反的轨道,等待 `context.media.mergeStreams` 完成, +再返回已授权的输出引用。宿主校验结果并保存文件。普通命令及跨插件调用均不能调用此操作。 +表单、多语言文案、入口和业务错误由插件清单定义,无需宿主提供媒体专用界面。 -该命令保留给宿主的手动合并界面。公开声明用于发现提供此功能的插件,不允许跨插件执行。 -宿主会在处理函数运行前拒绝通用调用和跨插件调用;不要通过 `commands.execute` 调用它, -也不要绕过宿主授权的文件选择上下文。 +每个输入最多 8 GiB,单次操作总输入最多 16 GiB。输入快照和输出暂存需要足够磁盘空间。 +取消时先停止宿主管理的任务,再清理临时资源;关闭表单不会中止任务。 +宿主重启会中断操作,不会自动重放合并。 diff --git a/plugins/motrix.media-merge/locales/en-US.json b/plugins/motrix.media-merge/locales/en-US.json index 1285ff5..de76911 100644 --- a/plugins/motrix.media-merge/locales/en-US.json +++ b/plugins/motrix.media-merge/locales/en-US.json @@ -1,9 +1,26 @@ { "name": "Media Merge", - "description": "Manually combine a downloaded or local video and audio file into MP4 or MKV without re-encoding. Requires a Motrix build with the manual media merge interface.", + "description": "Combine local video and audio into MP4 or MKV through plugin operations without re-encoding.", "merge": "Merge audio and video", - "ffmpegMissing": "Configure FFmpeg in Settings > Integration > Media Tools, then restart the plugin.", "differentFiles": "Choose two different files.", - "outputFormat": "Choose an MP4 or MKV output file.", - "missingStreams": "The selected files must provide a video track and an audio track. Check the files and try again." + "missingStreams": "The selected files must provide a video track and an audio track. Check the files and try again.", + "operationDescription": "Bring your video and audio together in one file, without re-encoding.", + "videoInput": "Video file", + "audioInput": "Audio file", + "format": "Output format", + "output": "Output file", + "videoInputPlaceholder": "Choose video", + "audioInputPlaceholder": "Choose audio", + "outputPlaceholder": "Choose a save location", + "videoInputBrowse": "Choose video file", + "audioInputBrowse": "Choose audio file", + "outputBrowse": "Choose output file", + "videoInputHint": "The picture for your merged file", + "audioInputHint": "The sound for your merged file", + "outputHint": "Save as MP4 or MKV", + "outputNotice": "Use a new filename; existing files cannot be overwritten. If MP4 is incompatible, try MKV.", + "serverNotice": "Select files on the Motrix server. Files on this computer are not uploaded.", + "mergeAction": "Merge", + "mergeAgain": "Merge another file", + "preserveOriginals": "Original files are kept" } diff --git a/plugins/motrix.media-merge/locales/zh-CN.json b/plugins/motrix.media-merge/locales/zh-CN.json index 1d223c9..2bf22ae 100644 --- a/plugins/motrix.media-merge/locales/zh-CN.json +++ b/plugins/motrix.media-merge/locales/zh-CN.json @@ -1,9 +1,26 @@ { "name": "音视频合并", - "description": "手动将已下载或本地的视频和音频无损封装为 MP4 或 MKV。需要支持手动媒体合并界面的 Motrix 版本。", + "description": "通过插件操作将本地音视频合并为 MP4 或 MKV,无需重新编码。", "merge": "合并音频和视频", - "ffmpegMissing": "请在“设置 > 集成 > 媒体工具”中配置 FFmpeg,然后重新启用插件。", "differentFiles": "请选择两个不同的文件。", - "outputFormat": "请选择 MP4 或 MKV 输出文件。", - "missingStreams": "所选文件需要分别提供视频轨和音频轨,请检查文件后重试。" + "missingStreams": "所选文件需要分别提供视频轨和音频轨,请检查文件后重试。", + "operationDescription": "将视频画面与音频合为一个文件,无需重新编码。", + "videoInput": "视频文件", + "audioInput": "音频文件", + "format": "输出格式", + "output": "输出文件", + "videoInputPlaceholder": "选择视频", + "audioInputPlaceholder": "选择音频", + "outputPlaceholder": "选择保存位置", + "videoInputBrowse": "选择视频文件", + "audioInputBrowse": "选择音频文件", + "outputBrowse": "选择输出文件", + "videoInputHint": "提供合并后的画面", + "audioInputHint": "提供合并后的声音", + "outputHint": "保存为 MP4 或 MKV", + "outputNotice": "请使用新文件名,已有文件不会被覆盖。MP4 不兼容时可尝试 MKV。", + "serverNotice": "选择 Motrix 服务器上的文件;不会上传本机文件。", + "mergeAction": "合并", + "mergeAgain": "再次合并", + "preserveOriginals": "无损合并,保留源文件" } diff --git a/plugins/motrix.media-merge/motrix-plugin.json b/plugins/motrix.media-merge/motrix-plugin.json index fd8372e..afcf653 100644 --- a/plugins/motrix.media-merge/motrix-plugin.json +++ b/plugins/motrix.media-merge/motrix-plugin.json @@ -7,48 +7,219 @@ "description": "%description%", "categories": ["post-action"], "engines": { - "motrix": ">=2.0.0-beta.47 <3.0.0" + "motrix": ">=2.0.0-beta.48 <3.0.0" }, "main": "dist/plugin.js", - "permissions": ["ffmpeg"], + "permissions": ["ffmpeg", "files.selected.read", "files.selected.write"], "activationEvents": ["onCommand:motrix.media-merge.mergeStreams"], "contributes": { "commands": [ { "id": "motrix.media-merge.mergeStreams", "title": "%merge%", - "public": true, + "public": false, "argsSchema": { "type": "object", "properties": { "videoInput": { "type": "string", - "minLength": 1 + "minLength": 36, + "maxLength": 36 }, "audioInput": { "type": "string", - "minLength": 1 + "minLength": 36, + "maxLength": 36 }, "output": { "type": "string", - "minLength": 1 + "minLength": 36, + "maxLength": 36 + }, + "format": { + "type": "string", + "enum": ["mp4", "mkv"], + "default": "mp4" } }, - "required": ["videoInput", "audioInput", "output"], + "required": ["videoInput", "audioInput", "output", "format"], "additionalProperties": false }, "resultSchema": { "type": "object", "properties": { - "outputPath": { - "type": "string" + "output": { + "type": "string", + "minLength": 36, + "maxLength": 36 } }, - "required": ["outputPath"], + "required": ["output"], "additionalProperties": false - } + }, + "execution": "operation" } - ] + ], + "operations": { + "version": 1, + "items": [ + { + "id": "motrix.media-merge.merge", + "title": "%merge%", + "description": "%operationDescription%", + "icon": "media-merge", + "command": "motrix.media-merge.mergeStreams", + "requires": [ + "operations.v1", + "files.selected.v1", + "media.merge-streams.v1", + "operations.ui.v1" + ], + "fields": [ + { + "field": "videoInput", + "label": "%videoInput%", + "appearance": { + "icon": "video", + "tone": "blue", + "placeholder": "%videoInputPlaceholder%", + "browseLabel": "%videoInputBrowse%", + "drop": true + }, + "help": "%videoInputHint%" + }, + { + "field": "audioInput", + "label": "%audioInput%", + "appearance": { + "icon": "audio", + "tone": "violet", + "placeholder": "%audioInputPlaceholder%", + "browseLabel": "%audioInputBrowse%", + "drop": true + }, + "help": "%audioInputHint%" + }, + { + "field": "format", + "label": "%format%", + "widget": "segmented", + "optionLabels": { + "mp4": "MP4", + "mkv": "MKV" + } + }, + { + "field": "output", + "label": "%output%", + "appearance": { + "icon": "folder", + "tone": "amber", + "placeholder": "%outputPlaceholder%", + "browseLabel": "%outputBrowse%" + }, + "help": "%outputHint%" + } + ], + "files": { + "videoInput": { + "kind": "input-file", + "use": "media" + }, + "audioInput": { + "kind": "input-file", + "use": "media" + }, + "output": { + "kind": "output-file", + "use": "media", + "extensionFrom": "format", + "suggestedName": { + "stemFrom": "videoInput", + "suffix": "-merged" + } + } + }, + "entries": [ + { + "surface": "plugin-detail" + }, + { + "surface": "task-menu", + "selection": { + "count": { + "min": 2, + "max": 2 + }, + "every": { + "status": "completed", + "files": "single", + "local": true + } + }, + "bind": { + "videoInput": { + "source": "task-file", + "index": 0 + }, + "audioInput": { + "source": "task-file", + "index": 1 + } + } + } + ], + "result": { + "files": ["output"] + }, + "errors": { + "differentFiles": "%differentFiles%", + "missingStreams": "%missingStreams%" + }, + "ui": { + "layout": { + "type": "stack", + "children": [ + { + "type": "grid", + "columns": 2, + "children": [ + { + "type": "field", + "field": "videoInput" + }, + { + "type": "field", + "field": "audioInput" + }, + { + "type": "field", + "field": "output", + "span": 2, + "accessory": "format" + } + ] + }, + { + "type": "text", + "text": "%outputNotice%", + "platform": "desktop" + }, + { + "type": "text", + "text": "%serverNotice%", + "platform": "web" + } + ] + }, + "submitLabel": "%mergeAction%", + "repeatLabel": "%mergeAgain%", + "footer": "%preserveOriginals%", + "repeat": "preserve-options" + } + } + ] + } }, "l10n": "locales" } diff --git a/plugins/motrix.media-merge/src/index.test.ts b/plugins/motrix.media-merge/src/index.test.ts index a340b6f..f6cb184 100644 --- a/plugins/motrix.media-merge/src/index.test.ts +++ b/plugins/motrix.media-merge/src/index.test.ts @@ -1,121 +1,106 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' const mock = vi.hoisted(() => ({ - handlers: new Map Promise>(), - ffmpeg: { available: true, probe: vi.fn(), mergeStreams: vi.fn() }, + handler: undefined as + | ((args: unknown, context: unknown) => Promise) + | undefined, })) vi.mock('motrix:plugin-api', () => ({ commands: { - register: (id: string, handler: (args: unknown) => Promise) => - mock.handlers.set(id, handler), + registerOperation: (_id: string, handler: typeof mock.handler) => { + mock.handler = handler + }, }, - ffmpeg: mock.ffmpeg, - i18n: { t: (key: string) => key }, })) import './index' -const invoke = (args: unknown) => - mock.handlers.get('motrix.media-merge.mergeStreams')!(args) const args = { - videoInput: '/video.mp4', - audioInput: '/audio.mp4', - output: '/merged.mp4', + videoInput: 'video-reference', + audioInput: 'audio-reference', + output: 'output-reference', + format: 'mp4', } - -describe('media merge plugin', () => { +const context = { + signal: new AbortController().signal, + fail: (code: string): never => { + throw new Error(code) + }, + media: { probe: vi.fn(), mergeStreams: vi.fn() }, +} +const invoke = (value = args) => mock.handler?.(value, context) +describe('media merge operation', () => { beforeEach(() => { - vi.clearAllMocks() - mock.ffmpeg.available = true - mock.ffmpeg.probe + context.signal = new AbortController().signal + context.media.probe .mockReset() - .mockResolvedValueOnce({ durationMs: 3000, streams: [{ type: 'video' }] }) + .mockResolvedValueOnce({ durationMs: 1000, streams: [{ type: 'video' }] }) .mockResolvedValueOnce({ streams: [{ type: 'audio' }] }) - mock.ffmpeg.mergeStreams.mockResolvedValue({ - result: Promise.resolve({ outputPath: args.output }), - }) + context.media.mergeStreams.mockReset().mockResolvedValue(undefined) }) - it('detects tracks and waits for the asynchronous operation handle and result', async () => { - let finish: (value: unknown) => void = () => {} - mock.ffmpeg.mergeStreams.mockResolvedValue({ - result: new Promise((resolve) => { - finish = resolve - }), - }) - let completed = false - const pending = invoke(args).then((result) => { - completed = true - return result - }) - await vi.waitFor(() => expect(mock.ffmpeg.mergeStreams).toHaveBeenCalled()) - expect(completed).toBe(false) - finish({ outputPath: args.output }) - await expect(pending).resolves.toEqual({ outputPath: args.output }) - expect(mock.ffmpeg.mergeStreams).toHaveBeenCalledWith({ - ...args, - expectedDurationMs: 3000, + it('uses opaque references and returns the output reference', async () => { + await expect(invoke()).resolves.toEqual({ output: args.output }) + expect(context.media.mergeStreams).toHaveBeenCalledWith({ + video: args.videoInput, + audio: args.audioInput, + output: args.output, + format: 'mp4', + expectedDurationMs: 1000, }) }) - it('swaps unambiguously reversed inputs, including audio in an MP4 container', async () => { - mock.ffmpeg.probe + it('swaps reversed track roles and supports MKV', async () => { + context.media.probe .mockReset() .mockResolvedValueOnce({ streams: [{ type: 'audio' }] }) - .mockResolvedValueOnce({ durationMs: 1000, streams: [{ type: 'video' }] }) - await invoke(args) - expect(mock.ffmpeg.mergeStreams).toHaveBeenCalledWith({ - videoInput: args.audioInput, - audioInput: args.videoInput, + .mockResolvedValueOnce({ durationMs: 2000, streams: [{ type: 'video' }] }) + await invoke({ ...args, format: 'mkv' }) + expect(context.media.mergeStreams).toHaveBeenCalledWith({ + video: args.audioInput, + audio: args.videoInput, output: args.output, - expectedDurationMs: 1000, + format: 'mkv', + expectedDurationMs: 2000, }) }) - it('rejects missing streams without launching a merge', async () => { - mock.ffmpeg.probe + it('keeps roles for dual-track inputs', async () => { + context.media.probe .mockReset() - .mockResolvedValue({ streams: [{ type: 'audio' }] }) - await expect(invoke(args)).rejects.toThrow('missingStreams') - expect(mock.ffmpeg.mergeStreams).not.toHaveBeenCalled() - }) - it('guides users to configure FFmpeg when unavailable', async () => { - mock.ffmpeg.available = false - await expect(invoke(args)).rejects.toThrow('ffmpegMissing') - expect(mock.ffmpeg.probe).not.toHaveBeenCalled() + .mockResolvedValue({ streams: [{ type: 'video' }, { type: 'audio' }] }) + await invoke() + expect(context.media.mergeStreams).toHaveBeenCalledWith( + expect.objectContaining({ video: args.videoInput }) + ) }) - it('propagates a codec or cancellation failure', async () => { - mock.ffmpeg.mergeStreams.mockRejectedValue(new Error('unsupported codec')) - await expect(invoke(args)).rejects.toThrow('unsupported codec') + it('rejects missing tracks and duplicate references', async () => { + context.media.probe + .mockReset() + .mockResolvedValue({ streams: [{ type: 'audio' }] }) + await expect(invoke()).rejects.toThrow('missingStreams') + await expect( + invoke({ ...args, audioInput: args.videoInput }) + ).rejects.toThrow('differentFiles') + expect(context.media.mergeStreams).not.toHaveBeenCalled() }) - it('propagates an operation failure after a successful launch', async () => { - let fail: (reason: Error) => void = () => {} - mock.ffmpeg.mergeStreams.mockResolvedValue({ - result: new Promise((_resolve, reject) => { - fail = reject - }), - }) - const pending = expect(invoke(args)).rejects.toThrow('cancelled') - await vi.waitFor(() => expect(mock.ffmpeg.mergeStreams).toHaveBeenCalled()) - fail(new Error('cancelled')) + it('awaits completion and propagates failures', async () => { + let reject!: (e: Error) => void + context.media.mergeStreams.mockImplementation( + () => + new Promise((_resolve, fail) => { + reject = fail + }) + ) + const pending = expect(invoke()).rejects.toThrow('cancelled') + await vi.waitFor(() => + expect(context.media.mergeStreams).toHaveBeenCalled() + ) + reject(new Error('cancelled')) await pending }) - it('keeps the selected track roles for dual-track files and supports MKV', async () => { - mock.ffmpeg.probe.mockReset().mockResolvedValue({ - durationMs: Number.NaN, - streams: [{ type: 'video' }, { type: 'audio' }], - }) - const mkv = { ...args, output: '/merged.mkv' } - await expect(invoke(mkv)).resolves.toEqual({ outputPath: mkv.output }) - expect(mock.ffmpeg.mergeStreams).toHaveBeenCalledWith({ - ...mkv, - expectedDurationMs: 0, - }) - }) - it.each([ - [{ ...args, videoInput: '' }, 'differentFiles'], - [{ ...args, audioInput: args.videoInput }, 'differentFiles'], - [{ ...args, output: '/merged.avi' }, 'outputFormat'], - ])('rejects invalid selections before probing', async (selection, error) => { - await expect(invoke(selection)).rejects.toThrow(error) - expect(mock.ffmpeg.probe).not.toHaveBeenCalled() - expect(mock.ffmpeg.mergeStreams).not.toHaveBeenCalled() + it('does not launch after cancellation during probe', async () => { + const controller = new AbortController() + context.signal = controller.signal + controller.abort() + await expect(invoke()).rejects.toThrow() + expect(context.media.mergeStreams).not.toHaveBeenCalled() }) }) diff --git a/plugins/motrix.media-merge/src/index.ts b/plugins/motrix.media-merge/src/index.ts index 5949ddb..ed9a94d 100644 --- a/plugins/motrix.media-merge/src/index.ts +++ b/plugins/motrix.media-merge/src/index.ts @@ -1,41 +1,36 @@ -import { commands, ffmpeg, i18n } from 'motrix:plugin-api' +import { commands } from 'motrix:plugin-api' -interface MediaInfo { - durationMs?: number - streams?: Array<{ type?: string }> -} - -commands.register< - { videoInput: string; audioInput: string; output: string }, - { outputPath: string } +commands.registerOperation< + { + videoInput: string + audioInput: string + output: string + format: 'mp4' | 'mkv' + }, + { output: string } >( 'motrix.media-merge.mergeStreams', - async ({ videoInput, audioInput, output }) => { - if (!ffmpeg.available) throw new Error(i18n.t('ffmpegMissing')) - if (!videoInput || !audioInput || videoInput === audioInput) - throw new Error(i18n.t('differentFiles')) - if (!/\.(mp4|mkv)$/i.test(output)) throw new Error(i18n.t('outputFormat')) - const first = (await ffmpeg.probe({ path: videoInput })) as MediaInfo - const second = (await ffmpeg.probe({ path: audioInput })) as MediaInfo - const has = (info: MediaInfo, type: string) => - info?.streams?.some((stream) => stream.type === type) + async ({ videoInput, audioInput, output, format }, context) => { + if (videoInput === audioInput) context.fail('differentFiles') + const first = await context.media.probe(videoInput) + const second = await context.media.probe(audioInput) + const has = (info: typeof first, type: string) => + info.streams.some((stream) => stream.type === type) let durationMs = first.durationMs if (!(has(first, 'video') && has(second, 'audio'))) { if (!(has(second, 'video') && has(first, 'audio'))) - throw new Error(i18n.t('missingStreams')) + context.fail('missingStreams') ;[videoInput, audioInput] = [audioInput, videoInput] durationMs = second.durationMs } - const operation = await ffmpeg.mergeStreams({ - videoInput, - audioInput, + context.signal.throwIfAborted() + await context.media.mergeStreams({ + video: videoInput, + audio: audioInput, output, - expectedDurationMs: - typeof durationMs === 'number' && Number.isFinite(durationMs) - ? Math.max(0, durationMs) - : 0, + format, + expectedDurationMs: durationMs, }) - await operation.result - return { outputPath: output } + return { output } } ) From 055edd8c31929ba523547ada85524724477bb4e0 Mon Sep 17 00:00:00 2001 From: Dr_rOot Date: Mon, 12 Oct 2026 12:16:45 +0800 Subject: [PATCH 2/2] chore(media-merge): consume published SDK 2.2.0 --- README.md | 7 ++++-- README.zh-CN.md | 2 +- package.json | 4 ++-- plugins/motrix.media-merge/package.json | 2 +- pnpm-lock.yaml | 31 ++++++++++++++----------- pnpm-workspace.yaml | 4 ++-- 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 96e113f..e6d95d7 100644 --- a/README.md +++ b/README.md @@ -164,8 +164,11 @@ re-encoding. Choose MP4 or MKV, keep the originals, follow progress, or cancel. Open its **Operations** tab, or select two completed single-file downloads. Configure FFmpeg in **Settings → Integration → Media Tools** first. -It is **not bundled with Motrix**. The first release requires a host containing -the manual merge interface, currently planned for `2.0.0-beta.47` or later. +It is **not bundled with Motrix** and has not been released. It requires +`>=2.0.0-beta.48 <3.0.0` with declarative plugin operations; beta.48 is the +planned first supporting host release. Raise this minimum before signing if +the host feature ships later. The published SDK 2.2.0 supplies the build-time +API and manifest schema; installing it does not add support to an older host. Read the [plugin guide](plugins/motrix.media-merge/README.md) for compatibility, Server file selection, logs, and release prerequisites. diff --git a/README.zh-CN.md b/README.zh-CN.md index c8b3651..45daf7c 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -126,7 +126,7 @@ 按需安装的官方插件,将视频和音频合成为一个文件,无需重新编码。可以选择 MP4 或 MKV,保留源文件、查看进度或取消操作。从插件的 **操作** 页进入,也可以选中两个已完成的单文件下载。使用前先在 **设置 → 集成 → 媒体工具** 中配置 FFmpeg。 -此插件 **默认不随 Motrix 安装**。首版需要包含手动合并界面的宿主,当前计划最低版本为 `2.0.0-beta.47`。版本要求、Server 文件选择、日志和发布前提见[插件指南](plugins/motrix.media-merge/README.zh-CN.md)。 +此插件 **默认不随 Motrix 安装**,目前尚未发布。它要求宿主版本为 `>=2.0.0-beta.48 <3.0.0`,并包含声明式插件操作机制;beta.48 是计划中的首个支持版本。如果宿主功能延期,签名前必须提高最低版本。已发布的 SDK 2.2.0 提供构建所需的 API 和清单 schema,安装 SDK 不会让旧宿主获得新机制。版本要求、Server 文件选择、日志和发布前提见[插件指南](plugins/motrix.media-merge/README.zh-CN.md)。 ## 在 Motrix 中使用 diff --git a/package.json b/package.json index 65405aa..6fbbbd8 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,8 @@ }, "devDependencies": { "@biomejs/biome": "^2.5.4", - "@motrix/plugin-api": "^2.0.0", - "@motrix/plugin-manifest-schema": "^2.1.0", + "@motrix/plugin-api": "^2.2.0", + "@motrix/plugin-manifest-schema": "^2.2.0", "esbuild": "^0.24.2", "vitest": "^3", "yauzl": "^3", diff --git a/plugins/motrix.media-merge/package.json b/plugins/motrix.media-merge/package.json index 2127bb9..ac0b10a 100644 --- a/plugins/motrix.media-merge/package.json +++ b/plugins/motrix.media-merge/package.json @@ -7,7 +7,7 @@ "typecheck": "tsc --noEmit" }, "devDependencies": { - "@motrix/plugin-api": "^2.0.0", + "@motrix/plugin-api": "^2.2.0", "esbuild": "^0.24.2", "typescript": "^5.6.3" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d1e2c06..c43ab6f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,11 +12,11 @@ importers: specifier: ^2.5.4 version: 2.5.4 '@motrix/plugin-api': - specifier: ^2.0.0 - version: 2.0.0 + specifier: ^2.2.0 + version: 2.2.0 '@motrix/plugin-manifest-schema': - specifier: ^2.1.0 - version: 2.1.0 + specifier: ^2.2.0 + version: 2.2.0 esbuild: specifier: ^0.24.2 version: 0.24.2 @@ -45,8 +45,8 @@ importers: plugins/motrix.media-merge: devDependencies: '@motrix/plugin-api': - specifier: ^2.0.0 - version: 2.0.0 + specifier: ^2.2.0 + version: 2.2.0 esbuild: specifier: ^0.24.2 version: 0.24.2 @@ -449,8 +449,11 @@ packages: '@motrix/plugin-api@2.0.0': resolution: {integrity: sha512-S4YsMvAo9ehDN8R6jwQAzcTHBstEFH1kPw8CRePMVLSNT0pb+5jlaQdBXd1NfsxY9O4FFX4e63uuAYWlkD9Urw==} - '@motrix/plugin-manifest-schema@2.1.0': - resolution: {integrity: sha512-S6p1uhoQzYbepVkYV86dmmD+7xLagto7ft3vWcLK4Fg8KtvEikV1b+ZPyE5OjgpZO3WEFcMe8uuc/Iymai0fmw==} + '@motrix/plugin-api@2.2.0': + resolution: {integrity: sha512-87gTWZY3k8wAlxQxp4RQ1dZ4Egi9zMoeW1lsPFZeBd6nOsQTn/hWDyJcc3MmRYMv4ZNteb210hTzlzeHCNC7lA==} + + '@motrix/plugin-manifest-schema@2.2.0': + resolution: {integrity: sha512-lR1D7+AHM6ElzaP5BvCgVkxL5WXmU3v/WO0GwGs2bP8+axm5PK8jnxzd+JlgGja3/b1nl+lCSksf6G9VOyVc+w==} '@rollup/rollup-android-arm-eabi@4.62.2': resolution: {integrity: sha512-6o7ZLZK+BeenkZCFNDXqpbjw9bD6nuWonvS/lwQJp7NoVVxm6p3qE7qQ5jGuBjiFsgvqjD8mZAU5oWxTmbOeOg==} @@ -865,8 +868,8 @@ packages: yazl@2.5.1: resolution: {integrity: sha512-phENi2PLiHnHb6QBVot+dJnaAZ0xosj7p3fWl+znIjBDlnMI2PsZCJZ306BPTFOaHf5qdDEI8x5qFrSOBN5vrw==} - zod@4.4.3: - resolution: {integrity: sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ==} + zod@4.6.5: + resolution: {integrity: sha512-v5l/aFXZQeai4awLbOpSoHecE9UiMrnfx75tEXLjNonXVARxQ5mOeipTjROUchszUNCqnE+hqAMujRsRHsut2Q==} snapshots: @@ -1062,9 +1065,11 @@ snapshots: '@motrix/plugin-api@2.0.0': {} - '@motrix/plugin-manifest-schema@2.1.0': + '@motrix/plugin-api@2.2.0': {} + + '@motrix/plugin-manifest-schema@2.2.0': dependencies: - zod: 4.4.3 + zod: 4.6.5 '@rollup/rollup-android-arm-eabi@4.62.2': optional: true @@ -1458,4 +1463,4 @@ snapshots: dependencies: buffer-crc32: 0.2.13 - zod@4.4.3: {} + zod@4.6.5: {} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 3cb793e..70d5ddf 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -6,5 +6,5 @@ allowBuilds: esbuild: true minimumReleaseAgeExclude: - - '@motrix/plugin-api@2.0.0' - - '@motrix/plugin-manifest-schema@2.1.0' + - '@motrix/plugin-api@2.0.0 || 2.2.0' + - '@motrix/plugin-manifest-schema@2.2.0'