Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Mirror the desktop impl

Manav Rathi37f1a7b6303d
diff --git a/web/apps/photos/src/worker/ffmpeg.worker.ts b/web/apps/photos/src/worker/ffmpeg.worker.ts
index 8403c3f6c8..4d1c1e88bd 100644
--- a/web/apps/photos/src/worker/ffmpeg.worker.ts
+++ b/web/apps/photos/src/worker/ffmpeg.worker.ts
@@ -13,28 +13,7 @@ import { FFmpeg, createFFmpeg } from "ffmpeg-wasm";
import { getUint8ArrayView } from "services/readerService";
export class DedicatedFFmpegWorker {
- private wasmFFmpeg: WasmFFmpeg;
-
- constructor() {
- this.wasmFFmpeg = new WasmFFmpeg();
- }
-
- /**
- * Execute a ffmpeg {@link command}.
- *
- * This is a sibling of {@link ffmpegExec} in ipc.ts exposed by the desktop
- * app. See [Note: ffmpeg in Electron].
- */
- run(cmd, inputFile, outputFileName, timeoutMS) {
- return this.wasmFFmpeg.run(cmd, inputFile, outputFileName, timeoutMS);
- }
-}
-
-expose(DedicatedFFmpegWorker, self);
-
-export class WasmFFmpeg {
private ffmpeg: FFmpeg;
- private ready: Promise<void> = null;
private ffmpegTaskQueue = new QueueProcessor<File>();
constructor() {
@@ -42,76 +21,93 @@ export class WasmFFmpeg {
corePath: "/js/ffmpeg/ffmpeg-core.js",
mt: false,
});
-
- this.ready = this.init();
}
- private async init() {
- if (!this.ffmpeg.isLoaded()) {
- await this.ffmpeg.load();
- }
- }
-
- async run(
- cmd: string[],
+ /**
+ * Execute a ffmpeg {@link command}.
+ *
+ * This is a sibling of {@link ffmpegExec} in ipc.ts exposed by the desktop
+ * app. See [Note: ffmpeg in Electron].
+ */
+ async execute(
+ command: string[],
inputFile: File,
outputFileName: string,
timeoutMS,
) {
- const exec = () => this.execute(cmd, inputFile, outputFileName);
+ if (!this.ffmpeg.isLoaded()) await this.ffmpeg.load();
+
+ const exec = () =>
+ ffmpegExec(this.ffmpeg, command, inputFile, outputFileName);
+
const request = this.ffmpegTaskQueue.queueUpRequest(() =>
timeoutMS ? withTimeout<File>(exec(), timeoutMS) : exec(),
);
+
return await request.promise;
}
+}
- private async execute(
- cmd: string[],
- inputFile: File,
- outputFileName: string,
- ) {
- let tempInputFilePath: string;
- let tempOutputFilePath: string;
- try {
- await this.ready;
- const [, extension] = nameAndExtension(inputFile.name);
- const tempNameSuffix = extension ? `input.${extension}` : "input";
- tempInputFilePath = `${generateTempName(10, tempNameSuffix)}`;
- this.ffmpeg.FS(
- "writeFile",
- tempInputFilePath,
- await getUint8ArrayView(inputFile),
- );
- tempOutputFilePath = `${generateTempName(10, outputFileName)}`;
+expose(DedicatedFFmpegWorker, self);
+
+const ffmpegExec = async (
+ ffmpeg: FFmpeg,
+ command: string[],
+ inputFile: File,
+ outputFileName: string,
+) => {
+ const [, extension] = nameAndExtension(inputFile.name);
+ const tempNameSuffix = extension ? `input.${extension}` : "input";
+ const tempInputFilePath = `${generateTempName(10, tempNameSuffix)}`;
+ const tempOutputFilePath = `${generateTempName(10, outputFileName)}`;
- cmd = cmd.map((cmdPart) => {
- if (cmdPart === ffmpegPathPlaceholder) {
- return "";
- } else if (cmdPart === inputPathPlaceholder) {
- return tempInputFilePath;
- } else if (cmdPart === outputPathPlaceholder) {
- return tempOutputFilePath;
- } else {
- return cmdPart;
- }
- });
- log.info(`${cmd}`);
- await this.ffmpeg.run(...cmd);
- return new File(
- [this.ffmpeg.FS("readFile", tempOutputFilePath)],
- outputFileName,
- );
- } finally {
- try {
- this.ffmpeg.FS("unlink", tempInputFilePath);
- } catch (e) {
- log.error("unlink input file failed", e);
- }
- try {
- this.ffmpeg.FS("unlink", tempOutputFilePath);
- } catch (e) {
- log.error("unlink output file failed", e);
- }
+ const cmd = substitutePlaceholders(
+ command,
+ tempInputFilePath,
+ tempOutputFilePath,
+ );
+
+ try {
+ ffmpeg.FS(
+ "writeFile",
+ tempInputFilePath,
+ await getUint8ArrayView(inputFile),
+ );
+
+ log.info(`Running ffmpeg (wasm) command ${cmd}`);
+ await ffmpeg.run(...cmd);
+
+ return new File(
+ [ffmpeg.FS("readFile", tempOutputFilePath)],
+ outputFileName,
+ );
+ } finally {
+ try {
+ ffmpeg.FS("unlink", tempInputFilePath);
+ } catch (e) {
+ log.error("Failed to remove input ${tempInputFilePath}", e);
+ }
+ try {
+ ffmpeg.FS("unlink", tempOutputFilePath);
+ } catch (e) {
+ log.error("Failed to remove output ${tempOutputFilePath}", e);
}
}
-}
+};
+
+const substitutePlaceholders = (
+ command: string[],
+ inputFilePath: string,
+ outputFilePath: string,
+) =>
+ command.map((segment) => {
+ if (segment == ffmpegPathPlaceholder) {
+ return "";
+ } else if (segment == inputPathPlaceholder) {
+ return inputFilePath;
+ } else if (segment == outputPathPlaceholder) {
+ return outputFilePath;
+ } else {
+ return segment;
+ }
+ });