Skip to content

armgit / ente

Public Imported from GitHub
0

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

trampoline

Manav Rathi3513b51477da
diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts
index 74f7821dcf..b74b19ec6c 100644
--- a/desktop/src/main/ipc.ts
+++ b/desktop/src/main/ipc.ts
@@ -13,6 +13,7 @@ import type { BrowserWindow } from "electron";
import { ipcMain } from "electron/main";
import type {
CollectionMapping,
+ FFmpegCommand,
FolderWatch,
PendingUploads,
UtilityProcessType,
@@ -31,6 +32,7 @@ import {
openLogDirectory,
selectDirectory,
} from "./services/dir";
+import { ffmpegExec } from "./services/ffmpeg";
import {
fsExists,
fsFindFiles,
@@ -186,6 +188,16 @@ export const attachIPCHandlers = () => {
) => generateImageThumbnail(dataOrPathOrZipItem, maxDimension, maxSize),
);
+ ipcMain.handle(
+ "ffmpegExec",
+ (
+ _,
+ command: FFmpegCommand,
+ dataOrPathOrZipItem: Uint8Array | string | ZipItem,
+ outputFileExtension: string,
+ ) => ffmpegExec(command, dataOrPathOrZipItem, outputFileExtension),
+ );
+
// - Upload
ipcMain.handle("listZipItems", (_, zipPath: string) =>
diff --git a/desktop/src/main/services/ffmpeg-worker.ts b/desktop/src/main/services/ffmpeg-worker.ts
index f19e0e9d86..e3f99f230d 100644
--- a/desktop/src/main/services/ffmpeg-worker.ts
+++ b/desktop/src/main/services/ffmpeg-worker.ts
@@ -9,15 +9,10 @@ import pathToFfmpeg from "ffmpeg-static";
import { randomBytes } from "node:crypto";
import fs from "node:fs/promises";
import path, { basename } from "node:path";
-import type { FFmpegCommand, ZipItem } from "../../types/ipc";
+import type { FFmpegCommand } from "../../types/ipc";
import log from "../log-worker";
import { messagePortMainEndpoint } from "../utils/comlink";
import { execAsyncWorker } from "../utils/exec-worker";
-import {
- deleteTempFileIgnoringErrors,
- makeFileForDataOrStreamOrPathOrZipItem,
- makeTempFilePath,
-} from "../utils/temp";
/* Ditto in the web app's code (used by the Wasm FFmpeg invocation). */
const ffmpegPathPlaceholder = "FFMPEG";
@@ -33,9 +28,9 @@ const outputPathPlaceholder = "OUTPUT";
export interface FFmpegUtilityProcess {
ffmpegExec: (
command: FFmpegCommand,
- dataOrPathOrZipItem: Uint8Array | string | ZipItem,
- outputFileExtension: string,
- ) => Promise<Uint8Array>;
+ inputFilePath: string,
+ outputFilePath: string,
+ ) => Promise<void>;
ffmpegConvertToMP4: (
inputFilePath: string,
@@ -92,44 +87,27 @@ process.parentPort.once("message", (e) => {
*/
const ffmpegExec = async (
command: FFmpegCommand,
- dataOrPathOrZipItem: Uint8Array | string | ZipItem,
- outputFileExtension: string,
-): Promise<Uint8Array> => {
- const {
- path: inputFilePath,
- isFileTemporary: isInputFileTemporary,
- writeToTemporaryFile: writeToTemporaryInputFile,
- } = await makeFileForDataOrStreamOrPathOrZipItem(dataOrPathOrZipItem);
-
- const outputFilePath = await makeTempFilePath(outputFileExtension);
- try {
- await writeToTemporaryInputFile();
-
- let resolvedCommand: string[];
- if (Array.isArray(command)) {
- resolvedCommand = command;
- } else {
- const isHDR = await isHDRVideo(inputFilePath);
- log.debugString(
- `${basename(inputFilePath)} ${JSON.stringify({ isHDR })}`,
- );
- resolvedCommand = isHDR ? command.hdr : command.default;
- }
-
- const cmd = substitutePlaceholders(
- resolvedCommand,
- inputFilePath,
- outputFilePath,
+ inputFilePath: string,
+ outputFilePath: string,
+): Promise<void> => {
+ let resolvedCommand: string[];
+ if (Array.isArray(command)) {
+ resolvedCommand = command;
+ } else {
+ const isHDR = await isHDRVideo(inputFilePath);
+ log.debugString(
+ `${basename(inputFilePath)} ${JSON.stringify({ isHDR })}`,
);
+ resolvedCommand = isHDR ? command.hdr : command.default;
+ }
- await execAsyncWorker(cmd);
+ const cmd = substitutePlaceholders(
+ resolvedCommand,
+ inputFilePath,
+ outputFilePath,
+ );
- return await fs.readFile(outputFilePath);
- } finally {
- if (isInputFileTemporary)
- await deleteTempFileIgnoringErrors(inputFilePath);
- await deleteTempFileIgnoringErrors(outputFilePath);
- }
+ await execAsyncWorker(cmd);
};
const substitutePlaceholders = (
@@ -494,22 +472,37 @@ const ffmpegGenerateHLSPlaylistAndSegments = async (
} catch (e) {
log.error("HLS generation failed", e);
await Promise.all([
- deleteTempFileIgnoringErrors(playlistPath),
- deleteTempFileIgnoringErrors(videoPath),
+ deletePathIgnoringErrors(playlistPath),
+ deletePathIgnoringErrors(videoPath),
]);
throw e;
} finally {
await Promise.all([
- deleteTempFileIgnoringErrors(keyInfoPath),
- deleteTempFileIgnoringErrors(keyPath),
+ deletePathIgnoringErrors(keyInfoPath),
+ deletePathIgnoringErrors(keyPath),
// ffmpeg writes a /path/output.ts.tmp, clear it out too.
- deleteTempFileIgnoringErrors(videoPath + ".tmp"),
+ deletePathIgnoringErrors(videoPath + ".tmp"),
]);
}
return { playlistPath, videoPath, dimensions, videoSize };
};
+/**
+ * A variant of {@link deletePathIgnoringErrors} (which we can't directly use in
+ * the utility process). It unconditionally removes the item at the provided
+ * path; in particular, this will not raise any errors if there is no item at
+ * the given path (as may be expected to happen when we run during catch
+ * handlers).
+ */
+const deletePathIgnoringErrors = async (tempFilePath: string) => {
+ try {
+ await fs.rm(tempFilePath, { force: true });
+ } catch (e) {
+ log.error(`Could not delete item at path ${tempFilePath}`, e);
+ }
+};
+
/**
* A regex that matches the first line of the form
*
diff --git a/desktop/src/main/services/ffmpeg.ts b/desktop/src/main/services/ffmpeg.ts
index 3b66bb2913..4567d7b704 100644
--- a/desktop/src/main/services/ffmpeg.ts
+++ b/desktop/src/main/services/ffmpeg.ts
@@ -3,6 +3,13 @@
* process.
*/
+import fs from "node:fs/promises";
+import type { FFmpegCommand, ZipItem } from "../../types/ipc";
+import {
+ deleteTempFileIgnoringErrors,
+ makeFileForDataOrStreamOrPathOrZipItem,
+ makeTempFilePath,
+} from "../utils/temp";
import type { FFmpegUtilityProcess } from "./ffmpeg-worker";
import { ffmpegUtilityProcessPort } from "./workers";
@@ -12,3 +19,32 @@ import { ffmpegUtilityProcessPort } from "./workers";
export const ffmpegUtilityProcess = () => {
return ffmpegUtilityProcessPort() as unknown as FFmpegUtilityProcess;
};
+
+export const ffmpegExec = async (
+ command: FFmpegCommand,
+ dataOrPathOrZipItem: Uint8Array | string | ZipItem,
+ outputFileExtension: string,
+): Promise<Uint8Array> => {
+ const {
+ path: inputFilePath,
+ isFileTemporary: isInputFileTemporary,
+ writeToTemporaryFile: writeToTemporaryInputFile,
+ } = await makeFileForDataOrStreamOrPathOrZipItem(dataOrPathOrZipItem);
+
+ const outputFilePath = await makeTempFilePath(outputFileExtension);
+ try {
+ await writeToTemporaryInputFile();
+
+ await ffmpegUtilityProcess().ffmpegExec(
+ command,
+ inputFilePath,
+ outputFilePath,
+ );
+
+ return await fs.readFile(outputFilePath);
+ } finally {
+ if (isInputFileTemporary)
+ await deleteTempFileIgnoringErrors(inputFilePath);
+ await deleteTempFileIgnoringErrors(outputFilePath);
+ }
+};
diff --git a/desktop/src/main/stream.ts b/desktop/src/main/stream.ts
index 04332b5894..0f0a37bd93 100644
--- a/desktop/src/main/stream.ts
+++ b/desktop/src/main/stream.ts
@@ -8,8 +8,8 @@ import fs from "node:fs/promises";
import { Readable, Writable } from "node:stream";
import { pathToFileURL } from "node:url";
import log from "./log";
+import { ffmpegUtilityProcess } from "./services/ffmpeg";
import { type FFmpegGenerateHLSPlaylistAndSegmentsResult } from "./services/ffmpeg-worker";
-import { electronFFmpegWorkerNodeIfRunning } from "./services/workers";
import { markClosableZip, openZip } from "./services/zip";
import { wait } from "./utils/common";
import { writeStream } from "./utils/stream";
@@ -235,7 +235,7 @@ const handleConvertToMP4Write = async (request: Request) => {
await writeStream(inputTempFilePath, request.body!);
const outputTempFilePath = await makeTempFilePath("mp4");
- const worker = electronFFmpegWorkerNodeIfRunning();
+ const worker = ffmpegUtilityProcess();
try {
await worker.ffmpegConvertToMP4(inputTempFilePath, outputTempFilePath);
} catch (e) {
@@ -309,7 +309,7 @@ const handleGenerateHLSWrite = async (
}
}
- const worker = electronFFmpegWorkerNodeIfRunning();
+ const worker = ffmpegUtilityProcess();
const {
path: inputFilePath,