From 7c2c593c962f25e37d768b189c76127c38d2c9ee Mon Sep 17 00:00:00 2001 From: oxygenkun Date: Sun, 23 Jun 2024 22:50:23 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E4=B8=BAaria2=E6=8F=90=E4=BE=9B=E5=8F=AF?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=B0=81=E9=9D=A2=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 优化视频下载组件的结构,现在仅在需要的时候获取assets的blob并下载 2. 扩展视频下载组件的接口,插件可以代理处理assets的下载逻辑 3. 扩展视频封面组件和aria2插件以支持aria2下载封面 --- .../lib/components/utils/view-cover/index.ts | 22 +++++- .../video/download/DownloadVideo.vue | 22 +++--- .../lib/components/video/download/types.ts | 20 +++++- .../video/download/aria2-output/RpcConfig.vue | 11 ++- .../video/download/aria2-output/aria2-rpc.ts | 71 +++++++++++++------ 5 files changed, 109 insertions(+), 37 deletions(-) diff --git a/registry/lib/components/utils/view-cover/index.ts b/registry/lib/components/utils/view-cover/index.ts index b56af3d7c0..4c5fef9af1 100644 --- a/registry/lib/components/utils/view-cover/index.ts +++ b/registry/lib/components/utils/view-cover/index.ts @@ -1,5 +1,5 @@ import { defineComponentMetadata } from '@/components/define' -import { getBlobByAid } from '@/components/video/video-cover' +import { getVideoCoverUrlByAid, getBlobByAid } from '@/components/video/video-cover' import { PackageEntry } from '@/core/download' import { videoAndBangumiUrls } from '@/core/utils/urls' import { Toast } from '@/core/toast' @@ -55,6 +55,26 @@ export const component = defineComponentMetadata({ toast.message = `获取完成. 成功 ${success.length} 个, 失败 ${fail.length} 个.` return success.map(it => it.value) }, + getUrls: async ( + infos, + instance: { + type: CoverDownloadType + enabled: boolean + }, + ) => { + const { type, enabled } = instance + if (!enabled) { + return [] + } + return Promise.all( + infos.map(async info => { + return { + name: `${info.input.title}.${type}`, + url: await getVideoCoverUrlByAid(info.input.aid), + } + }), + ) + }, component: () => import('./Plugin.vue').then(m => m.default), }) }) diff --git a/registry/lib/components/video/download/DownloadVideo.vue b/registry/lib/components/video/download/DownloadVideo.vue index 61b64505f6..25c2b5d7a5 100644 --- a/registry/lib/components/video/download/DownloadVideo.vue +++ b/registry/lib/components/video/download/DownloadVideo.vue @@ -332,18 +332,16 @@ export default Vue.extend({ }) } const action = new DownloadVideoAction(videoInfos) - const extraAssets = ( - await Promise.all( - assets.map(a => - a.getAssets( - videoInfos, - this.$refs.assetsOptions.find((c: any) => c.$attrs.name === a.name), - ), - ), - ) - ).flat() - action.extraAssets.push(...extraAssets) - await action.downloadExtraAssets() + assets.forEach(a => { + action.extraAssets.push({ + asset: a, + instance: this.$refs.assetsOptions.find((c: any) => c.$attrs.name === a.name), + }) + }) + /** 若视频输出的插件设置了proxyExtraAssets,则由插件在runAction中处理 */ + if (!output?.proxyExtraAssets) { + await action.downloadExtraAssets() + } await output.runAction(action, instance) } catch (error) { logError(error) diff --git a/registry/lib/components/video/download/types.ts b/registry/lib/components/video/download/types.ts index 9f68a33ae8..abcc677f9e 100644 --- a/registry/lib/components/video/download/types.ts +++ b/registry/lib/components/video/download/types.ts @@ -77,11 +77,17 @@ export interface DownloadVideoApi extends WithName { /** 表示下载时额外附带的产物, 如弹幕 / 字幕等 */ export interface DownloadVideoAssets extends VueInstanceInput, WithName { getAssets: (infos: DownloadVideoInfo[], instance: AssetsParameter) => Promise + /** 获取可直接下载的链接 */ + getUrls?: ( + infos: DownloadVideoInfo[], + instance: AssetsParameter, + ) => Promise<{ name: string; url: string }[]> } /** 表示视频的下载信息以及携带的额外产物 */ -export class DownloadVideoAction { +export class DownloadVideoAction { readonly inputs: DownloadVideoInputItem[] = [] - extraAssets: PackageEntry[] = [] + /** 可下载的asset和对应的参数 */ + extraAssets: { asset: DownloadVideoAssets; instance: AssetsParameter }[] = [] constructor(public infos: DownloadVideoInfo[]) { this.inputs = infos.map(it => it.input) @@ -92,11 +98,19 @@ export class DownloadVideoAction { async downloadExtraAssets() { console.log('[downloadExtraAssets]', this.extraAssets) const filename = `${getFriendlyTitle(false)}.zip` - await new DownloadPackage(this.extraAssets).emit(filename) + const { infos } = this + const extraAssetsBlob = ( + await Promise.all( + this.extraAssets.map(({ asset, instance }) => asset.getAssets(infos, instance)), + ) + ).flat() + await new DownloadPackage(extraAssetsBlob).emit(filename) } } /** 下载视频的最终输出处理 */ export interface DownloadVideoOutput extends VueInstanceInput, WithName { runAction: (action: DownloadVideoAction, instance: OutputParameter) => Promise + /** 是否需要代理下载assets */ + proxyExtraAssets?: boolean description?: string } diff --git a/registry/lib/plugins/video/download/aria2-output/RpcConfig.vue b/registry/lib/plugins/video/download/aria2-output/RpcConfig.vue index 067638faa3..9e2d059022 100644 --- a/registry/lib/plugins/video/download/aria2-output/RpcConfig.vue +++ b/registry/lib/plugins/video/download/aria2-output/RpcConfig.vue @@ -1,5 +1,9 @@ diff --git a/registry/lib/plugins/video/download/wasm-output/handler.ts b/registry/lib/plugins/video/download/wasm-output/handler.ts index 1f97330e3e..3a22090ec5 100644 --- a/registry/lib/plugins/video/download/wasm-output/handler.ts +++ b/registry/lib/plugins/video/download/wasm-output/handler.ts @@ -87,7 +87,7 @@ async function single( ) } -export async function run(action: DownloadVideoAction) { +export async function run(action: DownloadVideoAction, muxWithMetadata: boolean) { if (!ffmpeg.loaded) { await loadFFmpeg() } @@ -95,15 +95,17 @@ export async function run(action: DownloadVideoAction) { const { infos: pages, extraAssets } = action let ffmetadata: PackageEntry[] - const extraAssetsForBrowser = [] - for (const { asset, instance } of extraAssets) { - if (!ffmetadata && asset.name === 'saveVideoMetadata' && instance.type === 'ffmetadata') { - ffmetadata = await asset.getAssets(pages, instance) - } else { - extraAssetsForBrowser.push({ asset, instance }) + if (muxWithMetadata) { + const extraAssetsForBrowser = [] + for (const { asset, instance } of extraAssets) { + if (!ffmetadata && asset.name === 'saveVideoMetadata' && instance.type === 'ffmetadata') { + ffmetadata = await asset.getAssets(pages, instance) + } else { + extraAssetsForBrowser.push({ asset, instance }) + } } + action.extraAssets = extraAssetsForBrowser } - action.extraAssets = extraAssetsForBrowser const { dashAudioExtension, dashFlacAudioExtension, dashVideoExtension } = getComponentSettings('downloadVideo').options diff --git a/registry/lib/plugins/video/download/wasm-output/index.ts b/registry/lib/plugins/video/download/wasm-output/index.ts index 3cde08f64c..d5039f2592 100644 --- a/registry/lib/plugins/video/download/wasm-output/index.ts +++ b/registry/lib/plugins/video/download/wasm-output/index.ts @@ -19,14 +19,15 @@ export const plugin: PluginMetadata = { outputs.push({ name: 'wasm', displayName: 'WASM', - description: `${desc},运行过程中请勿关闭页面,初次使用或清除缓存后需要加载约 30 MB 的 WASM 文件`, - runAction: async action => { + description: `${desc}。运行过程中请勿关闭页面,初次使用或清除缓存后需要加载约 30 MB 的 WASM 文件。`, + runAction: async (action, instance) => { try { - await run(action) + await run(action, instance.muxWithMetadata) } catch (error) { Toast.error(String(error), title) } }, + component: () => import('./Config.vue').then(m => m.default), }) }) },