web/lib: remove ffmpeg

This commit is contained in:
wukko 2024-08-13 00:18:45 +06:00
parent 940826697c
commit 55ab3c36b1
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2

View file

@ -1,93 +0,0 @@
import { FFmpeg } from "@imput/ffmpeg.wasm";
import ffmpegCore from "@imput/ffmpeg-core?url";
import ffmpegCoreWASM from "@imput/ffmpeg-core/wasm?url";
import mime from "mime";
type InputFileKind = "video" | "audio";
type FileInfo = {
type?: string | null,
kind: InputFileKind,
extension: string,
}
type RenderParams = {
file: File,
output?: FileInfo,
args: string[],
}
export default class FFmpegWrapper {
initialized: boolean;
ffmpeg: FFmpeg;
concurrency: number;
constructor() {
this.ffmpeg = new FFmpeg();
this.initialized = false;
this.concurrency = Math.min(4, navigator.hardwareConcurrency);
}
async init() {
if (this.initialized) {
this.ffmpeg.terminate();
} else {
this.initialized = true;
this.ffmpeg.on("log", ({ message }) => {
console.log(message);
});
}
await this.ffmpeg.load({
coreURL: ffmpegCore,
wasmURL: ffmpegCoreWASM,
workerURL: "/ffmpeg-core.worker.js",
});
}
terminate() {
this.initialized = false;
return this.ffmpeg.terminate();
}
async renderFile({ file, output, args }: RenderParams) {
const inputKind = file.type.split("/")[0];
const inputExtension = mime.getExtension(file.type);
if (inputKind !== "video" && inputKind !== "audio") return;
if (!inputExtension) return;
const input: FileInfo = {
kind: inputKind,
extension: inputExtension,
}
if (!output) output = input;
output.type = mime.getType(output.extension);
if (!output.type) return;
const buffer = new Uint8Array(await file.arrayBuffer());
await this.ffmpeg.writeFile(
'input',
buffer
);
await this.ffmpeg.exec([
'-threads', this.concurrency.toString(),
'-i', 'input',
...args,
`output.${output.extension}`
]);
const renderBlob = new Blob(
[await this.ffmpeg.readFile(`output.${output.extension}`)],
{ type: output.type }
);
if (renderBlob.size === 0) return;
return renderBlob;
}
}