Skip to content

armgit / ente

Public Imported from GitHub
0

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

Extract shared media conversion core

Anand Baburajan8bf55add83e0
diff --git a/web/apps/albums/src/public-album/media/processing/convert.ts b/web/apps/albums/src/public-album/media/processing/convert.ts
index e3aa53c85d..45fefe7512 100644
--- a/web/apps/albums/src/public-album/media/processing/convert.ts
+++ b/web/apps/albums/src/public-album/media/processing/convert.ts
@@ -1,235 +1,18 @@
-import { lowercaseExtension } from "ente-base/file-name";
-import log from "ente-base/log";
-import { detectFileTypeInfo } from "ente-gallery/utils/detect-type";
+import {
+ playableVideoURLWeb,
+ renderableImageBlobWeb,
+} from "ente-gallery/services/convert-core";
import type { EnteFile } from "ente-media/file";
-import { KnownFileTypeInfos } from "ente-media/file-type";
-import { isHEICExtension, needsJPEGConversion } from "ente-media/formats";
-import { heicToJPEG } from "ente-media/heic-convert";
import { convertToMP4 } from "./ffmpeg";
-/**
- * Return a new {@link Blob} containing an image's data in a format that the
- * browser (likely) knows how to render (in an img tag, or on the canvas).
- *
- * The type of the returned blob is set, whenever possible, to the MIME type of
- * the data that we're dealing with.
- *
- * @param imageBlob A {@link Blob} containing the contents of an image file.
- *
- * @param fileName The name of the file whose data {@link imageBlob} is.
- *
- * The logic used by this function is:
- *
- * 1. Try to detect the MIME type of the file from its contents and/or name.
- *
- * 2. If this detected type is one of the types that we know that the browser
- * likely cannot render, continue. Otherwise return the imageBlob that was
- * passed in (after setting its MIME type).
- *
- * 3. If this is an HEIC file and the browser does not have native HEIC support,
- * then use our (Wasm) HEIC converter and return the resultant JPEG blob.
- *
- * 4. Otherwise return the original (with the MIME type if we were able to
- * deduce one).
- *
- * In will catch all errors and return the original in those cases.
- */
export const renderableImageBlob = async (
imageBlob: Blob,
fileName: string,
-): Promise<Blob> => {
- try {
- const file = new File([imageBlob], fileName);
- const fileTypeInfo = await detectFileTypeInfo(file);
- const { extension, mimeType } = fileTypeInfo;
+): Promise<Blob> => renderableImageBlobWeb(imageBlob, fileName);
- if (needsJPEGConversion(extension)) {
- log.debug(() => [`Converting ${fileName} to JPEG`, fileTypeInfo]);
-
- if (isHEICExtension(extension)) {
- if (mimeType == "image/heic" && (await isHEICSupported())) {
- log.debug(
- () => `Using native HEIC support for ${fileName}`,
- );
- } else {
- return await heicToJPEG(imageBlob);
- }
- }
- }
-
- // Either it is something that the browser already knows how to render
- // (e.g. JPEG/PNG), or is a file extension that might be supported in
- // some browsers (e.g. JPEG 2000), or a file extension that we haven't
- // specifically whitelisted for conversion (any arbitrary extension not
- // part of `needsJPEGConversion`).
- //
- // Give it to the browser, attaching the mime type if possible.
-
- if (!mimeType) {
- log.info(
- "Attempting to convert a file without a MIME type",
- fileName,
- );
- return imageBlob;
- } else {
- return new Blob([imageBlob], { type: mimeType });
- }
- } catch (e) {
- log.error(`Failed to convert ${fileName}, will use the original`, e);
- return imageBlob;
- }
-};
-
-let _isHEICSupported: Promise<boolean> | undefined;
-
-/**
- * Return true if the browser can natively render HEIC files.
- *
- * For performance, the result of the check is cached. There shouldn't be a
- * reason for this cache to need be invalidated, the browser shouldn't suddenly
- * drop its HEIC support in the middle of it running (but I'm sure posterity
- * will prove this assumption wrong in a way I can't yet anticipate).
- *
- * Some more details:
- *
- * - The check works by trying to load a small HEIC file.
- *
- * - Currently (Spring 2025), the only browser with support for HEIC is Safari.
- */
-const isHEICSupported = () =>
- (_isHEICSupported ??= new Promise((resolve) => {
- const image = new Image();
- image.onload = () => resolve(true);
- image.onerror = () => resolve(false);
- image.src = testHEICDataURL;
- }));
-
-/**
- * A data URL encoding the smallest HEIC image (439 bytes).
- *
- * Source:
- * https://github.com/vvideo/detect-audio-video/blob/main/src/image/smallest/index.ts
- */
-const testHEICDataURL =
- "data:image/heic;base64,AAAAGGZ0eXBoZWljAAAAAG1pZjFoZWljAAABaW1ldGEAAAAAAAAAIWhkbHIAAAAAAAAAAHBpY3QAAAAAAAAAAAAAAAAAAAAADnBpdG0AAAAAAAEAAAAiaWxvYwAAAABEQAABAAEAAAAAAYkAAQAAAAAAAAAuAAAAI2lpbmYAAAAAAAEAAAAVaW5mZQIAAAAAAQAAaHZjMQAAAADpaXBycAAAAMppcGNvAAAAdmh2Y0MBA3AAAAAAAAAAAAAe8AD8/fj4AAAPAyAAAQAYQAEMAf//A3AAAAMAkAAAAwAAAwAeugJAIQABACpCAQEDcAAAAwCQAAADAAADAB6gIIEFluqumubgIaDAgAAAAwCAAAADAIQiAAEABkQBwXPBiQAAABRpc3BlAAAAAAAAAEAAAABAAAAAKGNsYXAAAAABAAAAAQAAAAEAAAAB////wQAAAAL////BAAAAAgAAABBwaXhpAAAAAAMICAgAAAAXaXBtYQAAAAAAAAABAAEEgQKDBAAAADZtZGF0AAAAKigBrwayEx2gkim3i/2Rd0CR/V6h6GbEyV3dheegYfLV9ZwraCH8nff+7w==";
-
-/**
- * Return a object URL containing a video's data in a format that the browser
- * (likely) knows how to play back using an video tag.
- *
- * Unlike {@link renderableImageBlob}, this uses a much simpler flowchart:
- *
- * 1. If the browser thinks it can play the video, then return the an object URL
- * created by directly using the provided {@link videoBlob}.
- *
- * 2. Otherwise try to convert using FFmpeg, but only for short videos since
- * the Wasm FFmpeg implementation is much slower.
- *
- * 3. On errors, return the original (as would've happened for step 1).
- *
- * @param file The {@link EnteFile} with which this video is associated.
- *
- * @param videoFileName The name of the video.
- *
- * @param videoBlob The contents of the video.
- *
- * @returns An object URL that can be used to playback the provided
- * {@link videoBlob}.
- */
export const playableVideoURL = async (
_file: EnteFile,
videoFileName: string,
videoBlob: Blob,
-): Promise<string> => {
- // Safari-only MIME correction for blob URLs to avoid NotSupportedError.
- // Prefer extension-based lookup first (fast, no sniff). Fallback to
- // content detection only if extension is unknown.
- let typedBlob: Blob = videoBlob;
- // Note: We use isHEICSupported() as a Safari proxy. As of 2025, Safari is
- // the only browser with native HEIC support, so gating here effectively
- // targets Safari engines where stricter blob MIME handling applies.
- if (await isHEICSupported()) {
- const ext = lowercaseExtension(videoFileName);
- const known = KnownFileTypeInfos.find((f) => f.extension === ext);
- const knownMIME = known?.mimeType;
- if (knownMIME && knownMIME !== videoBlob.type) {
- typedBlob = new Blob([videoBlob], { type: knownMIME });
- } else {
- try {
- const detected = await detectFileTypeInfo(
- new File([videoBlob], videoFileName),
- );
- if (detected.mimeType && detected.mimeType !== videoBlob.type) {
- typedBlob = new Blob([videoBlob], {
- type: detected.mimeType,
- });
- }
- } catch {
- // Best-effort only; fall back to original blob on detection failure.
- }
- }
- }
-
- const videoObjectURL = URL.createObjectURL(typedBlob);
- const isPlayable = await isPlaybackPossible(videoObjectURL);
-
- const shouldConvert =
- !isPlayable &&
- videoBlob.size < 100 * 1024 * 1024; /* 100 MB, arbitrary */
-
- if (shouldConvert) {
- try {
- log.info(`Converting ${videoFileName} to mp4`);
- const convertedBlob = await convertToMP4(videoBlob);
- return URL.createObjectURL(
- new Blob([convertedBlob], { type: "video/mp4" }),
- );
- } catch (e) {
- log.error(`Video conversion failed for ${videoFileName}`, e);
- }
- }
-
- return videoObjectURL;
-};
-
-/**
- * Try to see if the browser thinks it can play the video pointed to by the
- * given {@link url} by creating a <video> element and initiating playback.
- *
- * Note that this can sometimes cause false positives if the browser can play
- * some of the streams in the video, but not all. For example, the browser may
- * be able to play back the video stream, but not the audio stream (say due to
- * some codec issue): in such cases this function will return true, causing us
- * to skip conversion, but when the user actually plays the video there will be
- * no sound.
- *
- * To deal with such cases in an holistic manner, we have a streaming variant of
- * the video that gets transcoded into a (near) universal format.
- */
-const isPlaybackPossible = async (url: string) =>
- new Promise((resolve) => {
- const t = setTimeout(() => {
- video.remove();
- resolve(false);
- }, 1000);
-
- const video = document.createElement("video");
- video.addEventListener("canplay", () => {
- clearTimeout(t);
- // Clean up the video element.
- video.remove();
- // Check for duration > 0 to make sure it is not a broken video.
- if (video.duration > 0) {
- resolve(true);
- } else {
- resolve(false);
- }
- });
- video.addEventListener("error", () => {
- clearTimeout(t);
- video.remove();
- resolve(false);
- });
-
- video.src = url;
- });
+): Promise<string> =>
+ playableVideoURLWeb(videoFileName, videoBlob, { convertToMP4 });
diff --git a/web/packages/gallery/services/convert-core.ts b/web/packages/gallery/services/convert-core.ts
new file mode 100644
index 0000000000..dd38e2b495
--- /dev/null
+++ b/web/packages/gallery/services/convert-core.ts
@@ -0,0 +1,162 @@
+import { lowercaseExtension } from "ente-base/file-name";
+import log from "ente-base/log";
+import { KnownFileTypeInfos } from "ente-media/file-type";
+import { isHEICExtension, needsJPEGConversion } from "ente-media/formats";
+import { heicToJPEG } from "ente-media/heic-convert";
+import { detectFileTypeInfo } from "../utils/detect-type";
+
+type ConvertToMP4 = (blob: Blob) => Promise<Blob | Uint8Array<ArrayBuffer>>;
+
+export interface RenderableImageBlobWebOpts {
+ convertToJPEG?: (imageBlob: Blob) => Promise<Blob>;
+ onConvertToJPEGError?: (e: unknown) => void;
+}
+
+export const renderableImageBlobWeb = async (
+ imageBlob: Blob,
+ fileName: string,
+ opts?: RenderableImageBlobWebOpts,
+): Promise<Blob> => {
+ try {
+ const file = new File([imageBlob], fileName);
+ const fileTypeInfo = await detectFileTypeInfo(file);
+ const { extension, mimeType } = fileTypeInfo;
+
+ if (needsJPEGConversion(extension)) {
+ log.debug(() => [`Converting ${fileName} to JPEG`, fileTypeInfo]);
+
+ if (opts?.convertToJPEG) {
+ try {
+ return await opts.convertToJPEG(imageBlob);
+ } catch (e) {
+ opts.onConvertToJPEGError?.(e);
+ }
+ }
+
+ if (isHEICExtension(extension)) {
+ if (mimeType == "image/heic" && (await isHEICSupported())) {
+ log.debug(
+ () => `Using native HEIC support for ${fileName}`,
+ );
+ } else {
+ return await heicToJPEG(imageBlob);
+ }
+ }
+ }
+
+ if (!mimeType) {
+ log.info(
+ "Attempting to convert a file without a MIME type",
+ fileName,
+ );
+ return imageBlob;
+ }
+
+ return new Blob([imageBlob], { type: mimeType });
+ } catch (e) {
+ log.error(`Failed to convert ${fileName}, will use the original`, e);
+ return imageBlob;
+ }
+};
+
+let _isHEICSupported: Promise<boolean> | undefined;
+
+export const isHEICSupported = () =>
+ (_isHEICSupported ??= new Promise((resolve) => {
+ const image = new Image();
+ image.onload = () => resolve(true);
+ image.onerror = () => resolve(false);
+ image.src = testHEICDataURL;
+ }));
+
+const testHEICDataURL =
+ "data:image/heic;base64,AAAAGGZ0eXBoZWljAAAAAG1pZjFoZWljAAABaW1ldGEAAAAAAAAAIWhkbHIAAAAAAAAAAHBpY3QAAAAAAAAAAAAAAAAAAAAADnBpdG0AAAAAAAEAAAAiaWxvYwAAAABEQAABAAEAAAAAAYkAAQAAAAAAAAAuAAAAI2lpbmYAAAAAAAEAAAAVaW5mZQIAAAAAAQAAaHZjMQAAAADpaXBycAAAAMppcGNvAAAAdmh2Y0MBA3AAAAAAAAAAAAAe8AD8/fj4AAAPAyAAAQAYQAEMAf//A3AAAAMAkAAAAwAAAwAeugJAIQABACpCAQEDcAAAAwCQAAADAAADAB6gIIEFluqumubgIaDAgAAAAwCAAAADAIQiAAEABkQBwXPBiQAAABRpc3BlAAAAAAAAAEAAAABAAAAAKGNsYXAAAAABAAAAAQAAAAEAAAAB////wQAAAAL////BAAAAAgAAABBwaXhpAAAAAAMICAgAAAAXaXBtYQAAAAAAAAABAAEEgQKDBAAAADZtZGF0AAAAKigBrwayEx2gkim3i/2Rd0CR/V6h6GbEyV3dheegYfLV9ZwraCH8nff+7w==";
+
+export interface PlayableVideoURLWebOpts {
+ convertToMP4: ConvertToMP4;
+ shouldConvertPlayableVideo?: () => boolean;
+ shouldConvertUnplayableVideo?: (videoBlob: Blob) => boolean;
+}
+
+export const playableVideoURLWeb = async (
+ videoFileName: string,
+ videoBlob: Blob,
+ {
+ convertToMP4,
+ shouldConvertPlayableVideo = () => false,
+ shouldConvertUnplayableVideo = shouldConvertSmallVideo,
+ }: PlayableVideoURLWebOpts,
+): Promise<string> => {
+ const typedBlob = await videoBlobWithSafariMIME(videoFileName, videoBlob);
+ const videoObjectURL = URL.createObjectURL(typedBlob);
+ const isPlayable = await isPlaybackPossible(videoObjectURL);
+ const shouldConvert = isPlayable
+ ? shouldConvertPlayableVideo()
+ : shouldConvertUnplayableVideo(videoBlob);
+
+ if (shouldConvert) {
+ try {
+ log.info(`Converting ${videoFileName} to mp4`);
+ const convertedBlob = await convertToMP4(videoBlob);
+ return URL.createObjectURL(
+ new Blob([convertedBlob], { type: "video/mp4" }),
+ );
+ } catch (e) {
+ log.error(`Video conversion failed for ${videoFileName}`, e);
+ }
+ }
+
+ return videoObjectURL;
+};
+
+const shouldConvertSmallVideo = (videoBlob: Blob) =>
+ videoBlob.size < 100 * 1024 * 1024; /* 100 MB, arbitrary */
+
+const videoBlobWithSafariMIME = async (
+ videoFileName: string,
+ videoBlob: Blob,
+) => {
+ if (!(await isHEICSupported())) return videoBlob;
+
+ const ext = lowercaseExtension(videoFileName);
+ const known = KnownFileTypeInfos.find((f) => f.extension === ext);
+ const knownMIME = known?.mimeType;
+ if (knownMIME && knownMIME !== videoBlob.type) {
+ return new Blob([videoBlob], { type: knownMIME });
+ }
+
+ try {
+ const detected = await detectFileTypeInfo(
+ new File([videoBlob], videoFileName),
+ );
+ if (detected.mimeType && detected.mimeType !== videoBlob.type) {
+ return new Blob([videoBlob], { type: detected.mimeType });
+ }
+ } catch {
+ // Best-effort only; fall back to original blob on detection failure.
+ }
+
+ return videoBlob;
+};
+
+const isPlaybackPossible = async (url: string): Promise<boolean> =>
+ new Promise((resolve) => {
+ const t = setTimeout(() => {
+ video.remove();
+ resolve(false);
+ }, 1000);
+
+ const video = document.createElement("video");
+ video.addEventListener("canplay", () => {
+ clearTimeout(t);
+ video.remove();
+ resolve(video.duration > 0);
+ });
+ video.addEventListener("error", () => {
+ clearTimeout(t);
+ video.remove();
+ resolve(false);
+ });
+
+ video.src = url;
+ });
diff --git a/web/packages/gallery/services/convert.ts b/web/packages/gallery/services/convert.ts
index 4830098ac6..2013905f91 100644
--- a/web/packages/gallery/services/convert.ts
+++ b/web/packages/gallery/services/convert.ts
@@ -1,124 +1,31 @@
import { isDesktop } from "ente-base/app";
-import { lowercaseExtension } from "ente-base/file-name";
import log from "ente-base/log";
import { workerBridge } from "ente-base/worker/worker-bridge";
import type { EnteFile } from "ente-media/file";
-import { FileType, KnownFileTypeInfos } from "ente-media/file-type";
-import { isHEICExtension, needsJPEGConversion } from "ente-media/formats";
-import { heicToJPEG } from "ente-media/heic-convert";
-import { detectFileTypeInfo } from "../utils/detect-type";
+import { FileType } from "ente-media/file-type";
+import { playableVideoURLWeb, renderableImageBlobWeb } from "./convert-core";
import { convertToMP4 } from "./ffmpeg";
-/**
- * Return a new {@link Blob} containing an image's data in a format that the
- * browser (likely) knows how to render (in an img tag, or on the canvas).
- *
- * The type of the returned blob is set, whenever possible, to the MIME type of
- * the data that we're dealing with.
- *
- * @param imageBlob A {@link Blob} containing the contents of an image file.
- *
- * @param fileName The name of the file whose data {@link imageBlob} is.
- *
- * The logic used by this function is:
- *
- * 1. Try to detect the MIME type of the file from its contents and/or name.
- *
- * 2. If this detected type is one of the types that we know that the browser
- * likely cannot render, continue. Otherwise return the imageBlob that was
- * passed in (after setting its MIME type).
- *
- * 3. If we're running in our desktop app and this MIME type is something our
- * desktop app can natively convert to a JPEG (using ffmpeg), do that and
- * return the resultant JPEG blob.
- *
- * 4. If this is an HEIC file and the browser does not have native HEIC support,
- * then use our (Wasm) HEIC converter and return the resultant JPEG blob.
- *
- * 5. Otherwise return the original (with the MIME type if we were able to
- * deduce one).
- *
- * In will catch all errors and return the original in those cases.
- */
export const renderableImageBlob = async (
imageBlob: Blob,
fileName: string,
-): Promise<Blob> => {
- try {
- const file = new File([imageBlob], fileName);
- const fileTypeInfo = await detectFileTypeInfo(file);
- const { extension, mimeType } = fileTypeInfo;
+): Promise<Blob> =>
+ renderableImageBlobWeb(
+ imageBlob,
+ fileName,
+ isDesktop
+ ? {
+ convertToJPEG: nativeConvertToJPEG,
+ onConvertToJPEGError: (e) =>
+ log.error("Native conversion to JPEG failed", e),
+ }
+ : undefined,
+ );
- if (needsJPEGConversion(extension)) {
- log.debug(() => [`Converting ${fileName} to JPEG`, fileTypeInfo]);
-
- // If we're running in our desktop app, see if our Node.js layer can
- // convert this into a JPEG using native tools.
-
- if (isDesktop) {
- try {
- return await nativeConvertToJPEG(imageBlob);
- } catch (e) {
- log.error("Native conversion to JPEG failed", e);
- }
- }
-
- // If the previous step failed, or if native JPEG conversion is not
- // available on this platform, for HEIC/HEIF files we can fallback
- // to our web HEIC converter.
-
- if (isHEICExtension(extension)) {
- // But first, check if the browser already knows how to natively
- // render HEICs, e.g. Safari 17+. In such cases not only is the
- // Wasm conversion unnecessary, the native hardware accelerated
- // support will also be _much_ faster.
- if (mimeType == "image/heic" && (await isHEICSupported())) {
- log.debug(
- () => `Using native HEIC support for ${fileName}`,
- );
- } else {
- return await heicToJPEG(imageBlob);
- }
- }
- }
-
- // Either it is something that the browser already knows how to render
- // (e.g. JPEG/PNG), or is a file extension that might be supported in
- // some browsers (e.g. JPEG 2000), or a file extension that we haven't
- // specifically whitelisted for conversion (any arbitrary extension not
- // part of `needsJPEGConversion`).
- //
- // Give it to the browser, attaching the mime type if possible.
-
- if (!mimeType) {
- log.info(
- "Attempting to convert a file without a MIME type",
- fileName,
- );
- return imageBlob;
- } else {
- return new Blob([imageBlob], { type: mimeType });
- }
- } catch (e) {
- log.error(`Failed to convert ${fileName}, will use the original`, e);
- return imageBlob;
- }
-};
-
-/**
- * Convert {@link imageBlob} to a JPEG blob.
- *
- * The presumption is that method used by our desktop app for converting to JPEG
- * should be able to handle files with all extensions for which
- * {@link needsJPEGConversion} returns true.
- */
const nativeConvertToJPEG = async (imageBlob: Blob) => {
const startTime = Date.now();
const imageData = new Uint8Array(await imageBlob.arrayBuffer());
const electron = globalThis.electron;
- // If we're running in a worker, we need to reroute the request back to
- // the main thread since workers don't have access to the `window` (and
- // thus, to the `window.electron`) object.
const jpegData = electron
? await electron.convertToJPEG(imageData)
: await workerBridge!.convertToJPEG(imageData);
@@ -126,198 +33,17 @@ const nativeConvertToJPEG = async (imageBlob: Blob) => {
return new Blob([jpegData], { type: "image/jpeg" });
};
-let _isHEICSupported: Promise<boolean> | undefined;
-
-/**
- * Return true if the browser can natively render HEIC files.
- *
- * For performance, the result of the check is cached. There shouldn't be a
- * reason for this cache to need be invalidated, the browser shouldn't suddenly
- * drop its HEIC support in the middle of it running (but I'm sure posterity
- * will prove this assumption wrong in a way I can't yet anticipate).
- *
- * Some more details:
- *
- * - The check works by trying to load a small HEIC file.
- *
- * - Currently (Spring 2025), the only browser with support for HEIC is Safari.
- */
-const isHEICSupported = () =>
- (_isHEICSupported ??= new Promise((resolve) => {
- const image = new Image();
- image.onload = () => resolve(true);
- image.onerror = () => resolve(false);
- image.src = testHEICDataURL;
- }));
-
-/**
- * A data URL encoding the smallest HEIC image (439 bytes).
- *
- * Source:
- * https://github.com/vvideo/detect-audio-video/blob/main/src/image/smallest/index.ts
- */
-const testHEICDataURL =
- "data:image/heic;base64,AAAAGGZ0eXBoZWljAAAAAG1pZjFoZWljAAABaW1ldGEAAAAAAAAAIWhkbHIAAAAAAAAAAHBpY3QAAAAAAAAAAAAAAAAAAAAADnBpdG0AAAAAAAEAAAAiaWxvYwAAAABEQAABAAEAAAAAAYkAAQAAAAAAAAAuAAAAI2lpbmYAAAAAAAEAAAAVaW5mZQIAAAAAAQAAaHZjMQAAAADpaXBycAAAAMppcGNvAAAAdmh2Y0MBA3AAAAAAAAAAAAAe8AD8/fj4AAAPAyAAAQAYQAEMAf//A3AAAAMAkAAAAwAAAwAeugJAIQABACpCAQEDcAAAAwCQAAADAAADAB6gIIEFluqumubgIaDAgAAAAwCAAAADAIQiAAEABkQBwXPBiQAAABRpc3BlAAAAAAAAAEAAAABAAAAAKGNsYXAAAAABAAAAAQAAAAEAAAAB////wQAAAAL////BAAAAAgAAABBwaXhpAAAAAAMICAgAAAAXaXBtYQAAAAAAAAABAAEEgQKDBAAAADZtZGF0AAAAKigBrwayEx2gkim3i/2Rd0CR/V6h6GbEyV3dheegYfLV9ZwraCH8nff+7w==";
-
-/**
- * Return a object URL containing a video's data in a format that the browser
- * (likely) knows how to play back using an video tag.
- *
- * Unlike {@link renderableImageBlob}, this uses a much simpler flowchart:
- *
- * 1. If the browser thinks it can play the video, then return the an object URL
- * created by directly using the provided {@link videoBlob}.
- *
- * 2. Otherwise try to convert using FFmpeg. This conversion always happens on
- * the desktop app, but in the browser the conversion only happens for short
- * videos since the Wasm FFmpeg implementation is much slower.
- *
- * 3. On errors, return the original (as would've happened for step 1).
- *
- * A special case if for FileType.livePhoto on Linux in the desktop app, where
- * the conversion always happens to workaround the audio only playback in that
- * specific scenario.
- *
- * @param file The {@link EnteFile} with which this video is associated.
- *
- * @param videoFileName The name of the video.
- *
- * @param videoBlob The contents of the video.
- *
- * @returns An object URL that can be used to playback the provided
- * {@link videoBlob}.
- */
export const playableVideoURL = async (
file: EnteFile,
videoFileName: string,
videoBlob: Blob,
-): Promise<string> => {
- // Safari-only MIME correction for blob URLs to avoid NotSupportedError.
- // Prefer extension-based lookup first (fast, no sniff). Fallback to
- // content detection only if extension is unknown.
- let typedBlob: Blob = videoBlob;
- // Note: We use isHEICSupported() as a Safari proxy. As of 2025, Safari is
- // the only browser with native HEIC support, so gating here effectively
- // targets Safari engines where stricter blob MIME handling applies.
- if (await isHEICSupported()) {
- const ext = lowercaseExtension(videoFileName);
- const known = KnownFileTypeInfos.find((f) => f.extension === ext);
- const knownMIME = known?.mimeType;
- if (knownMIME && knownMIME !== videoBlob.type) {
- typedBlob = new Blob([videoBlob], { type: knownMIME });
- } else {
- try {
- const detected = await detectFileTypeInfo(
- new File([videoBlob], videoFileName),
- );
- if (detected.mimeType && detected.mimeType !== videoBlob.type) {
- typedBlob = new Blob([videoBlob], {
- type: detected.mimeType,
- });
- }
- } catch {
- // Best-effort only; fall back to original blob on detection failure.
- }
- }
- }
-
- const videoObjectURL = URL.createObjectURL(typedBlob);
- const isPlayable = await isPlaybackPossible(videoObjectURL);
-
- let shouldConvert = false;
-
- if (isPlayable) {
- // The browser thinks it can play this video.
- //
- // But it is not a guarantee. In particular, a problematic case is when
- // for a particular codec combination, browser can play the audio
- // stream, but not the video stream. `isPlaybackPossible` would return
- // true, but when the user will hear only audio and not see the video.
- //
- // For videos themselves, we solve this (and other issues) by providing
- // a streaming variant. However it can still happen for live photos.
- //
- // Unfortunately, I haven't found a way yet of detecting if this
- // scenario is going to arise (open a issue if you've found one).
- // Fortunately, this particular failure mode has only been reported on
- // Linux desktop app (which uses Chromium underneath). So we add a
- // special case - if (desktop && livePhoto && linux) then forceConvert.
- // Practically this is a reasonable fallback since the video component
- // of a live photo is going to be a few seconds only, and the video
- // conversion is fast in the desktop app.
- if (
+): Promise<string> =>
+ playableVideoURLWeb(videoFileName, videoBlob, {
+ convertToMP4,
+ shouldConvertPlayableVideo: () =>
isDesktop &&
file.metadata.fileType == FileType.livePhoto &&
- navigator.platform.startsWith("Linux")
- ) {
- shouldConvert = true;
- }
- } else {
- // The browser doesn't think it can play this video, try transcoding.
- if (isDesktop) {
- // Always on desktop.
- shouldConvert = true;
- } else {
- // Don't try to transcode on the web if the file is too big.
- if (videoBlob.size < 100 * 1024 * 1024 /* 100 MB, arbitrary */) {
- shouldConvert = true;
- }
- }
- }
-
- if (shouldConvert) {
- try {
- log.info(`Converting ${videoFileName} to mp4`);
- const convertedBlob = await convertToMP4(videoBlob);
- return URL.createObjectURL(
- new Blob([convertedBlob], { type: "video/mp4" }),
- );
- } catch (e) {
- log.error(`Video conversion failed for ${videoFileName}`, e);
- }
- }
-
- return videoObjectURL;
-};
-
-/**
- * Try to see if the browser thinks it can play the video pointed to by the
- * given {@link url} by creating a <video> element and initiating playback.
- *
- * Note that this can sometimes cause false positives if the browser can play
- * some of the streams in the video, but not all. For example, the browser may
- * be able to play back the video stream, but not the audio stream (say due to
- * some codec issue): in such cases this function will return true, causing us
- * to skip conversion, but when the user actually plays the video there will be
- * no sound.
- *
- * To deal with such cases in an holistic manner, we have a streaming variant of
- * the video that gets transcoded into a (near) universal format.
- */
-const isPlaybackPossible = async (url: string) =>
- new Promise((resolve) => {
- const t = setTimeout(() => {
- video.remove();
- resolve(false);
- }, 1000);
-
- const video = document.createElement("video");
- video.addEventListener("canplay", () => {
- clearTimeout(t);
- // Clean up the video element.
- video.remove();
- // Check for duration > 0 to make sure it is not a broken video.
- if (video.duration > 0) {
- resolve(true);
- } else {
- resolve(false);
- }
- });
- video.addEventListener("error", () => {
- clearTimeout(t);
- video.remove();
- resolve(false);
- });
-
- video.src = url;
+ navigator.platform.startsWith("Linux"),
+ shouldConvertUnplayableVideo: (videoBlob) =>
+ isDesktop || videoBlob.size < 100 * 1024 * 1024,
});