This commit is contained in:
Manav Rathi
2024-11-21 12:46:18 +05:30
parent c001ea5682
commit b9a77cc8ce
3 changed files with 35 additions and 14 deletions

View File

@@ -2,7 +2,9 @@
"name": "@/gallery",
"version": "0.0.0",
"private": true,
"dependencies": {},
"dependencies": {
"@ffmpeg/ffmpeg": "^0.12.10"
},
"devDependencies": {
"@/build-config": "*"
}

View File

@@ -1,5 +1,6 @@
import log from "@/base/log";
import QueueProcessor from "@ente/shared/utils/queueProcessor";
import { FFmpeg } from "@ffmpeg/ffmpeg";
import { expose } from "comlink";
import {
ffmpegPathPlaceholder,
@@ -24,17 +25,13 @@ import {
//
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error
// @ts-ignore
import { createFFmpeg, type FFmpeg } from "ffmpeg-wasm";
export class DedicatedFFmpegWorker {
private ffmpeg: FFmpeg;
private ffmpegTaskQueue = new QueueProcessor<Uint8Array>();
constructor() {
this.ffmpeg = createFFmpeg({
corePath: "/js/ffmpeg/ffmpeg-core.js",
mt: false,
});
this.ffmpeg = new FFmpeg();
}
/**
@@ -48,7 +45,16 @@ export class DedicatedFFmpegWorker {
blob: Blob,
outputFileExtension: string,
): Promise<Uint8Array> {
if (!this.ffmpeg.isLoaded()) await this.ffmpeg.load();
if (!this.ffmpeg.loaded) {
await this.ffmpeg.load();
// this.ffmpeg = createFFmpeg({
// corePath: "/js/ffmpeg/ffmpeg-core.js",
// mt: false,
// });
// await this.ffmpeg.load();
}
const request = this.ffmpegTaskQueue.queueUpRequest(() =>
ffmpegExec(this.ffmpeg, command, outputFileExtension, blob),
@@ -77,22 +83,23 @@ const ffmpegExec = async (
try {
const startTime = Date.now();
ffmpeg.FS("writeFile", inputPath, inputData);
await ffmpeg.run(...cmd);
await ffmpeg.writeFile(inputPath, inputData);
await ffmpeg.exec(cmd);
const result = ffmpeg.FS("readFile", outputPath);
const result = await ffmpeg.readFile(outputPath);
if (typeof result == "string") throw new Error("Expected binary data");
const ms = Date.now() - startTime;
log.debug(() => `[wasm] ffmpeg ${cmd.join(" ")} (${ms} ms)`);
return result;
} finally {
try {
ffmpeg.FS("unlink", inputPath);
ffmpeg.deleteFile(inputPath);
} catch (e) {
log.error(`Failed to remove input ${inputPath}`, e);
}
try {
ffmpeg.FS("unlink", outputPath);
ffmpeg.deleteFile(outputPath);
} catch (e) {
log.error(`Failed to remove output ${outputPath}`, e);
}

View File

@@ -217,7 +217,7 @@
source-map "^0.5.7"
stylis "4.2.0"
"@emotion/cache@11.13.1", "@emotion/cache@^11.11.0", "@emotion/cache@^11.13.0", "@emotion/cache@^11.4.0":
"@emotion/cache@^11.11.0", "@emotion/cache@^11.13.0", "@emotion/cache@^11.4.0":
version "11.13.1"
resolved "https://registry.yarnpkg.com/@emotion/cache/-/cache-11.13.1.tgz#fecfc54d51810beebf05bf2a161271a1a91895d7"
integrity sha512-iqouYkuEblRcXmylXIwwOodiEK5Ifl7JcX7o6V4jI3iW4mLXX3dmt5xwBtIkJiQEXFAI+pC8X0i67yiPkH9Ucw==
@@ -245,7 +245,7 @@
resolved "https://registry.yarnpkg.com/@emotion/memoize/-/memoize-0.9.0.tgz#745969d649977776b43fc7648c556aaa462b4102"
integrity sha512-30FAj7/EoJ5mwVPOWhAyCX+FPfMDrVecJAM+Iw9NRoSl4BBAQeqj4cApHHUXOVvIPgLVDsCFoz/hGD+5QQD1GQ==
"@emotion/react@11.13.3", "@emotion/react@^11.13.3", "@emotion/react@^11.8.1":
"@emotion/react@11.13.3", "@emotion/react@^11.8.1":
version "11.13.3"
resolved "https://registry.yarnpkg.com/@emotion/react/-/react-11.13.3.tgz#a69d0de2a23f5b48e0acf210416638010e4bd2e4"
integrity sha512-lIsdU6JNrmYfJ5EbUCf4xW1ovy5wKQ2CkPRM4xogziOxH1nXxBSjpC9YqbFAP7circxMfYp+6x676BqWcEiixg==
@@ -487,6 +487,18 @@
dependencies:
levn "^0.4.1"
"@ffmpeg/ffmpeg@^0.12.10":
version "0.12.10"
resolved "https://registry.yarnpkg.com/@ffmpeg/ffmpeg/-/ffmpeg-0.12.10.tgz#e3cce21f21f11f33dfc1ec1d5ad5694f4a3073c9"
integrity sha512-lVtk8PW8e+NUzGZhPTWj2P1J4/NyuCrbDD3O9IGpSeLYtUZKBqZO8CNj1WYGghep/MXoM8e1qVY1GztTkf8YYQ==
dependencies:
"@ffmpeg/types" "^0.12.2"
"@ffmpeg/types@^0.12.2":
version "0.12.2"
resolved "https://registry.yarnpkg.com/@ffmpeg/types/-/types-0.12.2.tgz#bc7eef321ae50225c247091f1f23fd3087c6aa1d"
integrity sha512-NJtxwPoLb60/z1Klv0ueshguWQ/7mNm106qdHkB4HL49LXszjhjCCiL+ldHJGQ9ai2Igx0s4F24ghigy//ERdA==
"@floating-ui/core@^1.6.0":
version "1.6.7"
resolved "https://registry.yarnpkg.com/@floating-ui/core/-/core-1.6.7.tgz#7602367795a390ff0662efd1c7ae8ca74e75fb12"