Skip to content

armgit / ente

Public Imported from GitHub
0

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

Sketch

Manav Rathicdb81c621d4d
diff --git a/desktop/src/main/services/ffmpeg.ts b/desktop/src/main/services/ffmpeg.ts
index 3b8e1d99f6..19e3f2a935 100644
--- a/desktop/src/main/services/ffmpeg.ts
+++ b/desktop/src/main/services/ffmpeg.ts
@@ -110,7 +110,8 @@ const ffmpegBinaryPath = () => {
* @param inputFilePath The path to a file on the user's local file system. This
* is the video we want to convert.
- * @param inputFilePath The path to a file on the user's local file system where
+ *
+ * @param outputFilePath The path to a file on the user's local file system where
* we should write the converted MP4 video.
*/
export const ffmpegConvertToMP4 = async (
@@ -130,3 +131,62 @@ export const ffmpegConvertToMP4 = async (
await execAsync(cmd);
};
+
+/**
+ * A bespoke variant of {@link ffmpegExec} for generation of HLS playlists for
+ * videos.
+ *
+ * See: [Note: Preview variant of videos]
+
+ * @param inputFilePath The path to a file on the user's local file system. This
+ * is the video we want to convert.
+ *
+ * @param outputPlaylistPath The path to a file on the user's local file system
+ * where we should write the generated HLS playlist.
+ *
+ * @param outputVideoPath The path to a file on the user's local file system
+ * where we should write the transcoded and encrypted video that the HLS
+ * playlist refers to.
+ */
+export const ffmpegGenerateHLSPlaylist = async (
+ inputFilePath: string,
+ outputFilePath: string,
+): Promise<void> => {
+ // Current parameters
+ //
+ // - H264
+ // - 720p width
+ // - 2000kbps bitrate
+ // - 30fps frame rate
+ const command = [
+ ffmpegPathPlaceholder,
+ // Input file. We don't need any extra options that apply to the input file.
+ "-i",
+ inputPathPlaceholder,
+ // The remaining options apply to the next file, `outputPathPlaceholder`.
+ // ---
+ // `-vf` creates a filter graph for the video stream.
+ "-vf",
+ // `-vf scale=720:-1` scales the video to 720p width, keeping aspect ratio.
+ "scale=720:-1",
+ // `-r 30` sets the frame rate to 30 fps.
+ "-r",
+ "30",
+ // `-c:v libx264` sets the codec for the video stream to H264.
+ "-c:v",
+ "libx264",
+ // `-b:v 2000k` sets the bitrate for the video stream.
+ "-b:v",
+ "2000k",
+ // `-c:a aac -b:a 128k` converts the audio stream to 128k bit AAC.
+ "-c:a",
+ "aac",
+ "-b:a",
+ "128k",
+ outputPathPlaceholder,
+ ];
+
+ const cmd = substitutePlaceholders(command, inputFilePath, outputFilePath);
+
+ await execAsync(cmd);
+};
diff --git a/web/packages/gallery/services/ffmpeg/index.ts b/web/packages/gallery/services/ffmpeg/index.ts
index fd80705aea..08a380566b 100644
--- a/web/packages/gallery/services/ffmpeg/index.ts
+++ b/web/packages/gallery/services/ffmpeg/index.ts
@@ -271,54 +271,3 @@ const convertToMP4Native = async (electron: Electron, blob: Blob) => {
await readConvertToMP4Done(electron, token);
return mp4Blob;
};
-
-/**
- * Generate a preview variant of for the given video using a Wasm FFmpeg running
- * in a web worker.
- *
- * See: [Note: Preview variant of videos].
- *
- * @param blob The input video blob.
- *
- * @returns The output video blob containing the generated preview variant.
- */
-export const generateVideoPreviewVariantWeb = async (blob: Blob) =>
- ffmpegExecWeb(transcodeAndGenerateHLSPlaylistCommand, blob, "hls");
-
-/**
- * The FFmpeg command to use to create a preview variant of videos.
- *
- * Current parameters
- *
- * - H264
- * - 720p width
- * - 2000kbps bitrate
- * - 30fps frame rate
- */
-const transcodeAndGenerateHLSPlaylistCommand = [
- ffmpegPathPlaceholder,
- // Input file. We don't need any extra options that apply to the input file.
- "-i",
- inputPathPlaceholder,
- // The remaining options apply to the next file, `outputPathPlaceholder`.
- // ---
- // `-vf` creates a filter graph for the video stream.
- "-vf",
- // `-vf scale=720:-1` scales the video to 720p width, keeping aspect ratio.
- "scale=720:-1",
- // `-r 30` sets the frame rate to 30 fps.
- "-r",
- "30",
- // `-c:v libx264` sets the codec for the video stream to H264.
- "-c:v",
- "libx264",
- // `-b:v 2000k` sets the bitrate for the video stream.
- "-b:v",
- "2000k",
- // `-c:a aac -b:a 128k` converts the audio stream to 128k bit AAC.
- "-c:a",
- "aac",
- "-b:a",
- "128k",
- outputPathPlaceholder,
-];
diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.ts
index ec1cb4d419..8f1fd9babd 100644
--- a/web/packages/gallery/services/video.ts
+++ b/web/packages/gallery/services/video.ts
@@ -1,9 +1,8 @@
-import { isDesktop } from "ente-base/app";
import { decryptBlob } from "ente-base/crypto";
import type { EncryptedBlob } from "ente-base/crypto/types";
-import { isDevBuild } from "ente-base/env";
import type { PublicAlbumsCredentials } from "ente-base/http";
import log from "ente-base/log";
+import type { Electron } from "ente-base/types/ipc";
import type { EnteFile } from "ente-media/file";
import { FileType } from "ente-media/file-type";
import { settingsSnapshot } from "ente-new/photos/services/settings";
@@ -307,21 +306,17 @@ export const processVideoNewUpload = (
// TODO(HLS):
if (!isVideoProcessingEnabled()) return;
if (file.metadata.fileType !== FileType.video) return;
- if (!isDesktop) {
+ if (!electron) {
// Processing very large videos with the current ffmpeg Wasm
// implementation can cause the app to crash, esp. on mobile devices
// (e.g. https://github.com/ffmpegwasm/ffmpeg.wasm/issues/851).
//
- // So the video processing only happpens in the desktop app (which uses
- // the much more efficient native ffmpeg integration).
- if (process.env.NEXT_PUBLIC_ENTE_WIP_VIDEO_STREAMING && isDevBuild) {
- // TODO(HLS): Temporary dev convenience
- } else {
- return;
- }
+ // So the video processing only happpens in the desktop app, which uses
+ // the much more efficient native ffmpeg integration.
+ return;
}
- if (_state.videoProcessingQueue.length > 1) {
+ if (_state.videoProcessingQueue.length > 50) {
// Drop new requests if the queue can't keep up to avoid the app running
// out of memory by keeping hold of too many (potentially huge) video
// blobs. These items will later get processed as part of a backfill.
@@ -333,17 +328,20 @@ export const processVideoNewUpload = (
_state.videoProcessingQueue.push({ file, uploadItem });
// Tickle the processor if it isn't already running.
- _state.queueProcessor ??= processQueue();
+ _state.queueProcessor ??= processQueue(electron);
};
export const isVideoProcessingEnabled = () =>
process.env.NEXT_PUBLIC_ENTE_WIP_VIDEO_STREAMING &&
settingsSnapshot().isInternalUser;
-const processQueue = async () => {
+const processQueue = async (electron: Electron) => {
while (_state.videoProcessingQueue.length) {
try {
- await processQueueItem(_state.videoProcessingQueue.shift()!);
+ await processQueueItem(
+ _state.videoProcessingQueue.shift()!,
+ electron,
+ );
} catch (e) {
log.error("Video processing failed", e);
// Ignore this unprocessable item. Currently this function only runs
@@ -376,15 +374,22 @@ const processQueue = async () => {
*
* 3. Both the generated video and the HLS playlist are then uploaded, E2EE.
*/
-const processQueueItem = async ({
- file,
- uploadItem,
-}: VideoProcessingQueueItem) => {
+const processQueueItem = async (
+ { file, uploadItem }: VideoProcessingQueueItem,
+ electron: Electron,
+) => {
log.debug(() => ["gen-hls", { file, uploadItem }]);
const fileBlob = await fetchOriginalVideoBlob(file, uploadItem);
const previewFileData = await generateVideoPreviewVariantWeb(fileBlob);
+ const convertToMP4Native = async (electron: Electron, blob: Blob) => {
+ const token = await writeConvertToMP4Stream(electron, blob);
+ const mp4Blob = await readConvertToMP4Stream(electron, token);
+ await readConvertToMP4Done(electron, token);
+ return mp4Blob;
+ };
+
console.log(previewFileData);
await Promise.resolve(0);