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

[插件 - 下载视频 - WASM 混流输出] 修复写入元数据选项, 新增混流进度, 优化多集下载 #4984

Merged
merged 3 commits into from
Nov 5, 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
2 changes: 1 addition & 1 deletion registry/lib/plugins/video/download/wasm-output/Config.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default Vue.extend({
},
methods: {
saveOptions() {
options.muxWithMetadata = this.muxExtraAssets
options.muxWithMetadata = this.muxWithMetadata
Object.assign(storedOptions, options)
},
},
Expand Down
36 changes: 35 additions & 1 deletion registry/lib/plugins/video/download/wasm-output/ffmpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ enum FFMessageType {
EXEC = 'EXEC',
WRITE_FILE = 'WRITE_FILE',
READ_FILE = 'READ_FILE',
DELETE_FILE = 'DELETE_FILE',
ERROR = 'ERROR',
PROGRESS = 'PROGRESS',
}

export class FFmpeg {
#worker: Worker | null = null
#resolves: Callbacks = {}
#rejects: Callbacks = {}

#progressEventCallback: (event: ProgressEvent) => void

public loaded = false

#registerHandlers = () => {
Expand All @@ -38,8 +42,14 @@ export class FFmpeg {
case FFMessageType.EXEC:
case FFMessageType.WRITE_FILE:
case FFMessageType.READ_FILE:
case FFMessageType.DELETE_FILE:
this.#resolves[id](data)
break
case FFMessageType.PROGRESS:
if (this.#progressEventCallback) {
this.#progressEventCallback(<ProgressEvent>data)
}
break
case FFMessageType.ERROR:
this.#rejects[id](data)
break
Expand Down Expand Up @@ -140,6 +150,20 @@ export class FFmpeg {
undefined,
signal,
) as Promise<Uint8Array>

public deleteFile = (path: string, signal?: AbortSignal) =>
this.#send(
{
type: FFMessageType.DELETE_FILE,
data: { path },
},
undefined,
signal,
) as Promise<boolean>

public onProgress(callback: (event: ProgressEvent) => void): void {
this.#progressEventCallback = callback
}
}

// ========================================================================== //
Expand All @@ -165,18 +189,28 @@ interface FFMessageReadFileData {
encoding: string
}

interface FFMessageDeleteFileData {
path: string
}

type FFMessageData =
| FFMessageLoadConfig
| FFMessageExecData
| FFMessageWriteFileData
| FFMessageReadFileData
| FFMessageDeleteFileData

interface Message {
type: string
data?: FFMessageData
}

type CallbackData = Uint8Array | string | boolean | Error | undefined
interface ProgressEvent {
progress: number
time: number
}

type CallbackData = Uint8Array | string | boolean | Error | ProgressEvent | undefined

interface Callbacks {
[id: number | string]: (data: CallbackData) => void
Expand Down
18 changes: 14 additions & 4 deletions registry/lib/plugins/video/download/wasm-output/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DownloadPackage, PackageEntry } from '@/core/download'
import { meta } from '@/core/meta'
import { getComponentSettings } from '@/core/settings'
import { Toast } from '@/core/toast'
import { formatPercent } from '@/core/utils/formatters'
import { title as pluginTitle } from '.'
import type { Options } from '../../../../components/video/download'
import { DownloadVideoAction } from '../../../../components/video/download/types'
Expand Down Expand Up @@ -59,13 +60,13 @@ async function single(
httpGet(audioUrl, progress(1, '正在下载音频流')),
])

ffmpeg.writeFile('video', video)
ffmpeg.writeFile('audio', audio)
await ffmpeg.writeFile('video', video)
await ffmpeg.writeFile('audio', audio)

const args = ['-i', 'video', '-i', 'audio']

if (ffmetadata) {
ffmpeg.writeFile('ffmetadata', new TextEncoder().encode(ffmetadata))
await ffmpeg.writeFile('ffmetadata', new TextEncoder().encode(ffmetadata))
args.push('-i', 'ffmetadata', '-map_metadata', '2')
if (!outputMkv) {
args.push('-movflags', '+use_metadata_tags')
Expand All @@ -76,7 +77,9 @@ async function single(

console.debug('FFmpeg commandline args:', args.join(' '))

toast.message = '混流中……'
ffmpeg.onProgress(event => {
toast.message = `混流中: ${formatPercent(event.progress)}`
})
await ffmpeg.exec(args)

const output = await ffmpeg.readFile('output')
Expand All @@ -87,6 +90,13 @@ async function single(
toast.message = '完成!'
toast.duration = 1000

await Promise.all([
ffmpeg.deleteFile('video'),
ffmpeg.deleteFile('audio'),
ffmpeg.deleteFile('output'),
ffmetadata ? ffmpeg.deleteFile('ffmetadata') : Promise.resolve(),
])

await DownloadPackage.single(
name.replace(/.[^/.]+$/, `.${outputMkv ? 'mkv' : 'mp4'}`),
outputBlob,
Expand Down
Loading