Skip to content

armgit / ente

Public Imported from GitHub
0

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

Relay

Manav Rathi2d3734bf143a
diff --git a/desktop/src/main/services/ffmpeg.ts b/desktop/src/main/services/ffmpeg.ts
index 4e025ed3d3..c1c8d1ab29 100644
--- a/desktop/src/main/services/ffmpeg.ts
+++ b/desktop/src/main/services/ffmpeg.ts
@@ -1,7 +1,7 @@
import pathToFfmpeg from "ffmpeg-static";
import { randomBytes } from "node:crypto";
import fs from "node:fs/promises";
-import path from "node:path";
+import path, { basename } from "node:path";
import type { ZipItem } from "../../types/ipc";
import log from "../log";
import { execAsync } from "../utils/electron";
@@ -166,11 +166,14 @@ export interface FFmpegGenerateHLSPlaylistAndSegmentsResult {
* @returns The paths to two files on the user's local file system - one
* containing the generated HLS playlist, and the other containing the
* transcoded and encrypted video segments that the HLS playlist refers to.
+ *
+ * If the video is such that it doesn't require stream generation, then this
+ * function returns `undefined`.
*/
export const ffmpegGenerateHLSPlaylistAndSegments = async (
inputFilePath: string,
outputPathPrefix: string,
-): Promise<FFmpegGenerateHLSPlaylistAndSegmentsResult> => {
+): Promise<FFmpegGenerateHLSPlaylistAndSegmentsResult | undefined> => {
// [Note: Tonemapping HDR to HD]
//
// BT.709 ("HD") is a standard that describes things like how color is
@@ -209,6 +212,8 @@ export const ffmpegGenerateHLSPlaylistAndSegments = async (
const { isH264, isBT709, bitrate } =
await detectVideoCharacteristics(inputFilePath);
+ log.debug(() => [basename(inputFilePath), { isH264, isBT709, bitrate }]);
+
// If the video is smaller than 10 MB, and already H.264 (the codec we are
// going to use for the conversion), then a streaming variant is not much
// use. Skip such cases.
@@ -234,8 +239,7 @@ export const ffmpegGenerateHLSPlaylistAndSegments = async (
.stat(inputFilePath)
.then((st) => st.size);
if (inputVideoSize <= 10 * 1024 * 1024 /* 10 MB */) {
- // TODO(HLS):
- console.log("Potential skip");
+ return undefined;
}
}
diff --git a/desktop/src/main/stream.ts b/desktop/src/main/stream.ts
index 2cc8d450d9..7c8d98547d 100644
--- a/desktop/src/main/stream.ts
+++ b/desktop/src/main/stream.ts
@@ -277,11 +277,15 @@ const handleVideoDone = async (token: string) => {
* See: [Note: Convert to MP4] for the general architecture of commands that do
* renderer <-> main I/O using streams.
*
- * The difference here is that we the conversion generates two streams - one for
- * the HLS playlist itself, and one for the file containing the encrypted and
- * transcoded video chunks. The video stream we write to the objectUploadURL
+ * The difference here is that we the conversion generates two streams^ - one
+ * for the HLS playlist itself, and one for the file containing the encrypted
+ * and transcoded video chunks. The video stream we write to the objectUploadURL
* (provided via {@link params}), and then we return a JSON object containing
* the token for the playlist, and other metadata for use by the renderer.
+ *
+ * ^ if the video doesn't require a stream to be generated (e.g. it is very
+ * small and already uses a compatible codec) then a HTT 204 is returned and
+ * no stream is generated.
*/
const handleGenerateHLSWrite = async (
request: Request,
@@ -313,7 +317,7 @@ const handleGenerateHLSWrite = async (
} = await makeFileForDataOrStreamOrPathOrZipItem(inputItem);
const outputFilePathPrefix = await makeTempFilePath();
- let result: FFmpegGenerateHLSPlaylistAndSegmentsResult;
+ let result: FFmpegGenerateHLSPlaylistAndSegmentsResult | undefined;
try {
await writeToTemporaryInputFile();
@@ -322,6 +326,11 @@ const handleGenerateHLSWrite = async (
outputFilePathPrefix,
);
+ if (!result) {
+ // This video doesn't require stream generation.
+ return new Response("", { status: 204 });
+ }
+
const { playlistPath, videoPath } = result;
try {
await uploadVideoSegments(videoPath, objectUploadURL);
diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.ts
index 4bef5280e9..f9b325a59b 100644
--- a/web/packages/gallery/services/video.ts
+++ b/web/packages/gallery/services/video.ts
@@ -420,12 +420,18 @@ const processQueueItem = async (
log.info(`Generate HLS for ${fileLogID(file)} | start`);
- const { playlistToken, dimensions, videoSize } = await initiateGenerateHLS(
+ const res = await initiateGenerateHLS(
electron,
sourceVideo!,
objectUploadURL,
);
+ if (!res) {
+ log.info(`Generate HLS for ${fileLogID(file)} | not-required`);
+ return;
+ }
+
+ const { playlistToken, dimensions, videoSize } = res;
try {
const playlist = await readVideoStream(electron, playlistToken).then(
(res) => res.text(),
diff --git a/web/packages/gallery/utils/native-stream.ts b/web/packages/gallery/utils/native-stream.ts
index 20f654155c..2bc566609e 100644
--- a/web/packages/gallery/utils/native-stream.ts
+++ b/web/packages/gallery/utils/native-stream.ts
@@ -190,13 +190,17 @@ export type GenerateHLSResult = z.infer<typeof GenerateHLSResult>;
* metadata about the generated video (its byte size and dimensions). See {@link
* GenerateHLSResult.
*
+ * In case the video is such that it doesn't require a separate stream to be
+ * generated (e.g. it is a small video using an already compatible codec), then
+ * this function will return `undefined`.
+ *
* See: [Note: Preview variant of videos].
*/
export const initiateGenerateHLS = async (
_: Electron,
video: UploadItem | ReadableStream,
objectUploadURL: string,
-): Promise<GenerateHLSResult> => {
+): Promise<GenerateHLSResult | undefined> => {
const params = new URLSearchParams({ op: "generate-hls", objectUploadURL });
let body: ReadableStream | null;
@@ -238,6 +242,8 @@ export const initiateGenerateHLS = async (
if (!res.ok)
throw new Error(`Failed to write stream to ${url}: HTTP ${res.status}`);
+ if (res.status == 204) return undefined;
+
return GenerateHLSResult.parse(await res.json());
};