Skip to content

Commit

Permalink
Merge pull request #4984 from WakelessSloth56/preview-features
Browse files Browse the repository at this point in the history
[插件 - 下载视频 - WASM 混流输出] 修复写入元数据选项,  新增混流进度, 优化多集下载
  • Loading branch information
the1812 authored Nov 5, 2024
2 parents 9658f63 + 30ec155 commit e27816a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
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

0 comments on commit e27816a

Please sign in to comment.