Skip to content

armgit / ente

Public Imported from GitHub
0

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

node 2

Manav Rathi72acefadd4b1
diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts
index 3782033bb8..b4e7eff0ca 100644
--- a/desktop/src/main/ipc.ts
+++ b/desktop/src/main/ipc.ts
@@ -32,7 +32,7 @@ import {
openLogDirectory,
selectDirectory,
} from "./services/dir";
-import { ffmpegExec } from "./services/ffmpeg";
+import { ffmpegDetermineVideoDuration, ffmpegExec } from "./services/ffmpeg";
import {
fsExists,
fsFindFiles,
@@ -198,6 +198,12 @@ export const attachIPCHandlers = () => {
) => ffmpegExec(command, pathOrZipItem, outputFileExtension),
);
+ ipcMain.handle(
+ "ffmpegDetermineVideoDuration",
+ (_, pathOrZipItem: string | ZipItem) =>
+ ffmpegDetermineVideoDuration(pathOrZipItem),
+ );
+
// - Upload
ipcMain.handle("listZipItems", (_, zipPath: string) =>
diff --git a/desktop/src/main/services/ffmpeg.ts b/desktop/src/main/services/ffmpeg.ts
index 69edefafe0..ead6c4c63e 100644
--- a/desktop/src/main/services/ffmpeg.ts
+++ b/desktop/src/main/services/ffmpeg.ts
@@ -31,7 +31,21 @@ export const ffmpegExec = async (
command: FFmpegCommand,
pathOrZipItem: string | ZipItem,
outputFileExtension: string,
-): Promise<Uint8Array> => {
+): Promise<Uint8Array> =>
+ withInputFile(pathOrZipItem, async (worker, inputFilePath) => {
+ const outputFilePath = await makeTempFilePath(outputFileExtension);
+ try {
+ await worker.ffmpegExec(command, inputFilePath, outputFilePath);
+ return await fs.readFile(outputFilePath);
+ } finally {
+ await deleteTempFileIgnoringErrors(outputFilePath);
+ }
+ });
+
+export const withInputFile = async <T>(
+ pathOrZipItem: string | ZipItem,
+ f: (worker: FFmpegUtilityProcess, inputFilePath: string) => Promise<T>,
+): Promise<T> => {
const worker = await ffmpegUtilityProcess();
const {
@@ -40,16 +54,23 @@ export const ffmpegExec = async (
writeToTemporaryFile: writeToTemporaryInputFile,
} = await makeFileForStreamOrPathOrZipItem(pathOrZipItem);
- const outputFilePath = await makeTempFilePath(outputFileExtension);
try {
await writeToTemporaryInputFile();
- await worker.ffmpegExec(command, inputFilePath, outputFilePath);
-
- return await fs.readFile(outputFilePath);
+ return await f(worker, inputFilePath);
} finally {
if (isInputFileTemporary)
await deleteTempFileIgnoringErrors(inputFilePath);
- await deleteTempFileIgnoringErrors(outputFilePath);
}
};
+
+/**
+ * Implement the IPC "ffmpegDetermineVideoDuration" contract, writing the input
+ * to temporary files as needed, and then forward to the
+ * {@link ffmpegDetermineVideoDuration} running in the utility process.
+ */
+export const ffmpegDetermineVideoDuration = async (
+ pathOrZipItem: string | ZipItem,
+): Promise<number> => {
+ throw new Error("todo");
+};
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index d923a72613..e983855d5f 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -216,6 +216,9 @@ const ffmpegExec = (
outputFileExtension,
);
+const ffmpegDetermineVideoDuration = (pathOrZipItem: string | ZipItem) =>
+ ipcRenderer.invoke("ffmpegDetermineVideoDuration", pathOrZipItem);
+
// - Utility processes
const triggerCreateUtilityProcess = (type: UtilityProcessType) => {
@@ -392,6 +395,7 @@ contextBridge.exposeInMainWorld("electron", {
convertToJPEG,
generateImageThumbnail,
ffmpegExec,
+ ffmpegDetermineVideoDuration,
// - ML
diff --git a/web/packages/gallery/services/ffmpeg/web.ts b/web/packages/gallery/services/ffmpeg/web.ts
index d290010562..0074cc70ea 100644
--- a/web/packages/gallery/services/ffmpeg/web.ts
+++ b/web/packages/gallery/services/ffmpeg/web.ts
@@ -154,7 +154,7 @@ const withInputMount = async <T>(
ffmpeg: FFmpeg,
blob: Blob,
f: (inputPath: string) => Promise<T>,
-) => {
+): Promise<T> => {
const mountDir = "/mount";
const inputFileName = newID("in_");
const inputPath = joinPath(mountDir, inputFileName);