feat: wasm plugin progress event

This commit is contained in:
WakelessSloth56 2024-10-27 10:42:22 +08:00
parent cb7df01bdf
commit 30ec155fa8
No known key found for this signature in database
GPG Key ID: 94974E33833A67F3
2 changed files with 18 additions and 2 deletions

View File

@ -19,6 +19,7 @@ enum FFMessageType {
READ_FILE = 'READ_FILE',
DELETE_FILE = 'DELETE_FILE',
ERROR = 'ERROR',
PROGRESS = 'PROGRESS',
}
export class FFmpeg {
@ -26,6 +27,8 @@ export class FFmpeg {
#resolves: Callbacks = {}
#rejects: Callbacks = {}
#progressEventCallback: (event: ProgressEvent) => void
public loaded = false
#registerHandlers = () => {
@ -42,6 +45,11 @@ export class FFmpeg {
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
@ -197,7 +205,12 @@ interface Message {
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

View File

@ -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'
@ -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')