From 1113ddd9c5ee76b7f91dfef3500962c328c3d654 Mon Sep 17 00:00:00 2001 From: wukko Date: Sun, 11 Aug 2024 13:04:40 +0600 Subject: [PATCH] web/ffmpeg: universal render function for all needs --- web/src/lib/ffmpeg.ts | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/web/src/lib/ffmpeg.ts b/web/src/lib/ffmpeg.ts index 3e0b4638..c2f8899d 100644 --- a/web/src/lib/ffmpeg.ts +++ b/web/src/lib/ffmpeg.ts @@ -4,6 +4,19 @@ import ffmpegCoreWASM from "@imput/ffmpeg-core/wasm?url"; import { FFmpeg } from "@imput/ffmpeg.wasm"; import { fetchFile } from "@imput/ffmpeg-util"; +type renderParams = { + url: string, + input: { + type: string, + format: string, + }, + output: { + type: string, + format: string, + }, + args: string[], +} + export default class FFmpegWrapper { initialized: boolean; ffmpeg: FFmpeg; @@ -38,24 +51,24 @@ export default class FFmpegWrapper { return this.ffmpeg.terminate(); } - async renderFile(url: string, type: string, format: string) { - const input = `input.${format}`; + async renderFile({ url, input, output, args }: renderParams) { + const inputFile = `input.${input.format}`; await this.ffmpeg.writeFile( - input, + inputFile, await fetchFile(url) ) await this.ffmpeg.exec([ '-threads', this.concurrency.toString(), - '-i', input, - '-c', 'copy', - `output.${format}` + '-i', inputFile, + ...args, + `output.${output.format}` ]); - const data = await this.ffmpeg.readFile(`output.${format}`); + const data = await this.ffmpeg.readFile(`output.${output.format}`); const finalBlob = URL.createObjectURL( - new Blob([data], { type: `${type}/${format}` }) + new Blob([data], { type: `${output.type}/${output.format}` }) ); return finalBlob