Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update WASM plugins logic #4686

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions registry/lib/plugins/video/download/wasm-output/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,40 @@ async function load(toast: Toast) {
),
})
}
export async function run(name: string, videoUrl: string, audioUrl: string, toast: Toast) {
export async function run(
name: string,
toast: Toast,
videoUrl: string,
audioUrl: string,
isFlac: boolean,
) {
if (!ffmpeg.loaded) {
await load(toast)
}

ffmpeg.writeFile('video', await httpGet(videoUrl, toastProgress(toast, '正在下载视频流')))
ffmpeg.writeFile('audio', await httpGet(audioUrl, toastProgress(toast, '正在下载音频流')))

toast.message = '混流中……'

await ffmpeg.exec(['-i', 'video', '-i', 'audio', '-c:v', 'copy', '-c:a', 'copy', 'output.mp4'])
const outputExt = isFlac ? 'mkv' : 'mp4'
name = isFlac ? name.replace(/.[^/.]+$/, `.${outputExt}`) : name
await ffmpeg.exec([
'-i',
'video',
'-i',
'audio',
'-c:v',
'copy',
'-c:a',
isFlac ? 'flac' : 'copy',
'-f',
isFlac ? 'matroska' : 'mp4',
`output.${outputExt}`,
])

const output = await ffmpeg.readFile('output.mp4')
const output = await ffmpeg.readFile(`output.${outputExt}`)
const outputBlob = new Blob([output], {
type: 'video/mp4',
type: isFlac ? 'video/x-matroska' : 'video/mp4',
})

toast.message = '完成!'
Expand Down
33 changes: 29 additions & 4 deletions registry/lib/plugins/video/download/wasm-output/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Toast } from '@/core/toast'
import { Toast, ToastType } from '@/core/toast'
import { PluginMetadata } from '@/plugins/plugin'
import { DownloadVideoOutput } from '../../../../components/video/download/types'
import { run } from './handler'
import { getComponentSettings } from '@/core/settings'
import type { Options } from '../../../../components/video/download'

const title = 'WASM 混流输出'
const desc = '使用 WASM 在浏览器中下载并合并音视频'
Expand All @@ -22,12 +24,35 @@ export const plugin: PluginMetadata = {
description: `${desc},运行过程中请勿关闭页面,初次使用或清除缓存后需要加载约 30 MB 的 WASM 文件`,
runAction: async action => {
const fragments = action.infos.flatMap(it => it.titledFragments)
if (fragments.length !== 2) {
Toast.error('仅支持 DASH 格式', title)
const { dashAudioExtension, dashFlacAudioExtension, dashVideoExtension } =
getComponentSettings<Options>('downloadVideo').options
the1812 marked this conversation as resolved.
Show resolved Hide resolved

if (
fragments.length > 2 ||
(fragments.length === 2 &&
!(
fragments[0].extension === dashVideoExtension &&
(fragments[1].extension === dashAudioExtension ||
fragments[1].extension === dashFlacAudioExtension)
))
) {
Toast.error('仅支持单个视频下载', title).duration = 1500
} else {
const toast = Toast.info('正在加载', title)
try {
await run(fragments[0].title, fragments[0].url, fragments[1].url, toast)
if (fragments.length === 2) {
await run(
fragments[0].title,
toast,
fragments[0].url,
fragments[1].url,
fragments[1].extension === dashFlacAudioExtension,
) // [dash]
} else {
toast.type = ToastType.Error
toast.message = '仅支持 dash 格式视频下载'
toast.duration = 1500
}
} catch (error) {
toast.close()
Toast.error(String(error), title)
Expand Down
Loading