Skip to content

🎥 Make videos programmatically with React

favicon

Jonny Burgere8b3d972b242
diff --git a/packages/convert/app/components/Probe.tsx b/packages/convert/app/components/Probe.tsx
index 8378b14a7c..198df05944 100644
--- a/packages/convert/app/components/Probe.tsx
+++ b/packages/convert/app/components/Probe.tsx
@@ -3,7 +3,10 @@ import clsx from 'clsx';
import React, {useCallback, useMemo, useState} from 'react';
import {Source} from '~/lib/convert-state';
import {useIsNarrow} from '~/lib/is-narrow';
-import {useAddFilenameToTitle} from '~/lib/title-context';
+import {
+ useAddFilenameToTitle,
+ useCopyThumbnailToFavicon,
+} from '~/lib/title-context';
import {AudioTrackOverview} from './AudioTrackOverview';
import {ContainerOverview} from './ContainerOverview';
import {SourceLabel} from './SourceLabel';
@@ -84,6 +87,7 @@ export const Probe: React.FC<{
}, [probeDetails, sortedTracks, trackDetails]);
useAddFilenameToTitle(name);
+ useCopyThumbnailToFavicon(videoThumbnailRef);
return (
<div className="w-full lg:w-[350px]">
diff --git a/packages/convert/app/components/ResizeThumbnail.tsx b/packages/convert/app/components/ResizeThumbnail.tsx
index a537c2c925..7a13f66e58 100644
--- a/packages/convert/app/components/ResizeThumbnail.tsx
+++ b/packages/convert/app/components/ResizeThumbnail.tsx
@@ -1,6 +1,7 @@
import {Dimensions} from '@remotion/media-parser';
import {ResizeOperation} from '@remotion/webcodecs';
-import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
+import React, {useCallback, useMemo, useRef, useState} from 'react';
+import {useThumbnailCopy} from '~/lib/use-thumbnail-copy';
import {ResizeCorner} from './ResizeCorner';
import {VideoThumbnailRef} from './VideoThumbnail';
@@ -52,10 +53,6 @@ export const ResizeThumbnail: React.FC<{
return getThumbnailDimensions(unrotatedDimensions);
}, [unrotatedDimensions]);
- const [drawn, setDrawn] = useState(
- () => thumbnailRef.current?.hasBitmap ?? false,
- );
-
const inner = useMemo(() => {
return {
height: thumbnailDimensions.height * scale,
@@ -63,45 +60,6 @@ export const ResizeThumbnail: React.FC<{
};
}, [scale, thumbnailDimensions]);
- const draw = useCallback(() => {
- thumbnailRef.current?.copy().then((map) => {
- const ctx = ref.current?.getContext('2d');
- if (!ctx) {
- return;
- }
-
- ctx.drawImage(
- map,
- 0,
- 0,
- unrotatedThumbnailDimensions.width,
- unrotatedThumbnailDimensions.height,
- );
-
- setDrawn(true);
- });
- }, [
- thumbnailRef,
- unrotatedThumbnailDimensions.height,
- unrotatedThumbnailDimensions.width,
- ]);
-
- useEffect(() => {
- const {current} = thumbnailRef;
- if (!current) {
- return;
- }
-
- current.addOnChangeListener(draw);
- return () => {
- current.removeOnChangeListener(draw);
- };
- }, [draw, thumbnailRef]);
-
- useEffect(() => {
- draw();
- }, [draw]);
-
const [dragging, setDragging] = useState(false);
const onEnd = useCallback(() => {
@@ -114,6 +72,12 @@ export const ResizeThumbnail: React.FC<{
const animate = !dragging && !inputFocused;
+ const drawn = useThumbnailCopy({
+ sourceRef: thumbnailRef,
+ targetRef: ref,
+ dimensions: unrotatedThumbnailDimensions,
+ });
+
return (
<div className="rounded transition-transform">
<div
diff --git a/packages/convert/app/lib/default-favicon.ts b/packages/convert/app/lib/default-favicon.ts
new file mode 100644
index 0000000000..9473a15dba
--- /dev/null
+++ b/packages/convert/app/lib/default-favicon.ts
@@ -0,0 +1 @@
+export const DEFAULT_FAVICON = `${import.meta.env.BASE_URL}pwa-icon-192.png`;
diff --git a/packages/convert/app/lib/title-context.tsx b/packages/convert/app/lib/title-context.tsx
index e3f40c220c..64993d189e 100644
--- a/packages/convert/app/lib/title-context.tsx
+++ b/packages/convert/app/lib/title-context.tsx
@@ -1,11 +1,15 @@
import React, {useEffect, useLayoutEffect, useMemo} from 'react';
+import {VideoThumbnailRef} from '~/components/VideoThumbnail';
import {getPageTitle, RouteAction} from '~/seo';
+import {DEFAULT_FAVICON} from './default-favicon';
+import {useThumbnailAsFavicon} from './use-thumbnail-as-favicon';
export type TitleContextT = {
filename: string | null;
setFilename: React.Dispatch<React.SetStateAction<string | null>>;
progress: number | null;
setProgress: React.Dispatch<React.SetStateAction<number | null>>;
+ favicon: OffscreenCanvas | null;
};
const TitleContext = React.createContext<TitleContextT | null>(null);
@@ -16,10 +20,20 @@ export const TitleProvider: React.FC<{
}> = ({children, routeAction}) => {
const [filename, setFilename] = React.useState<string | null>(null);
const [progress, setProgress] = React.useState<number | null>(null);
+ const [favicon] = React.useState<OffscreenCanvas | null>(() => {
+ if (typeof OffscreenCanvas === 'undefined') {
+ return null;
+ }
+
+ const canvas = new OffscreenCanvas(32, 32);
+ canvas.width = 32;
+ canvas.height = 32;
+ return canvas;
+ });
const value: TitleContextT = useMemo(() => {
- return {filename, setFilename, progress, setProgress};
- }, [filename, progress]);
+ return {filename, setFilename, progress, setProgress, favicon};
+ }, [filename, progress, favicon]);
useLayoutEffect(() => {
document.title = [
@@ -78,3 +92,30 @@ export const useAddProgressToTitle = (progress: number | null) => {
};
}, [progress, setProgress]);
};
+
+export const useCopyThumbnailToFavicon = (
+ sourceRef: React.RefObject<VideoThumbnailRef | null>,
+) => {
+ const {favicon} = useTitle();
+
+ const thumbnail = useThumbnailAsFavicon({
+ sourceRef,
+ targetRef: favicon,
+ });
+
+ useEffect(() => {
+ const favicon = document.querySelector('link[rel="icon"]');
+ if (!favicon) {
+ throw new Error('No favicon found');
+ }
+
+ if (thumbnail) {
+ favicon.setAttribute('href', thumbnail);
+ } else {
+ favicon.setAttribute('href', DEFAULT_FAVICON);
+ }
+ return () => {
+ favicon.setAttribute('href', DEFAULT_FAVICON);
+ };
+ }, [thumbnail]);
+};
diff --git a/packages/convert/app/lib/use-thumbnail-as-favicon.ts b/packages/convert/app/lib/use-thumbnail-as-favicon.ts
new file mode 100644
index 0000000000..e4803bb3cc
--- /dev/null
+++ b/packages/convert/app/lib/use-thumbnail-as-favicon.ts
@@ -0,0 +1,79 @@
+import {useCallback, useEffect, useState} from 'react';
+import {VideoThumbnailRef} from '~/components/VideoThumbnail';
+
+const blobToDataURL = (blob: Blob) => {
+ return new Promise<string>((resolve, reject) => {
+ const reader = new FileReader();
+ reader.onload = () => resolve(reader.result as string);
+ reader.onerror = reject;
+ reader.readAsDataURL(blob);
+ });
+};
+
+export const useThumbnailAsFavicon = ({
+ sourceRef,
+ targetRef,
+}: {
+ sourceRef: React.RefObject<VideoThumbnailRef | null>;
+ targetRef: OffscreenCanvas | null;
+}) => {
+ const [thumbnail, setThumbnail] = useState<string | null>(null);
+ const [mounted, setMounted] = useState(false);
+
+ const draw = useCallback(() => {
+ if (!targetRef) {
+ return;
+ }
+
+ if (!sourceRef.current) {
+ return;
+ }
+ if (!sourceRef.current.hasBitmap) {
+ return;
+ }
+
+ sourceRef.current.copy().then((map) => {
+ const ctx = targetRef.getContext('2d');
+ if (!ctx) {
+ return;
+ }
+
+ const scale = Math.min(32 / map.width, 32 / map.height);
+ const x = (32 - map.width * scale) / 2;
+ const y = (32 - map.height * scale) / 2;
+
+ ctx.clearRect(0, 0, 32, 32);
+ ctx.drawImage(map, x, y, map.width * scale, map.height * scale);
+
+ targetRef
+ .convertToBlob()
+ .then((blob) => blobToDataURL(blob))
+ .then((url) => setThumbnail(url));
+ });
+ }, [sourceRef, targetRef]);
+
+ useEffect(() => {
+ const {current} = sourceRef;
+ if (!current) {
+ return;
+ }
+
+ current.addOnChangeListener(draw);
+ return () => {
+ current.removeOnChangeListener(draw);
+ };
+ }, [draw, sourceRef]);
+
+ useEffect(() => {
+ setMounted(true);
+ return () => {
+ setMounted(false);
+ };
+ }, []);
+
+ useEffect(() => {
+ draw();
+ }, [draw]);
+
+ return mounted ? thumbnail : null;
+};
diff --git a/packages/convert/app/lib/use-thumbnail-copy.ts b/packages/convert/app/lib/use-thumbnail-copy.ts
new file mode 100644
index 0000000000..4cf8c7295e
--- /dev/null
+++ b/packages/convert/app/lib/use-thumbnail-copy.ts
@@ -0,0 +1,52 @@
+import {Dimensions} from '@remotion/media-parser';
+import {useCallback, useEffect, useState} from 'react';
+import {VideoThumbnailRef} from '~/components/VideoThumbnail';
+
+export const useThumbnailCopy = ({
+ sourceRef,
+ targetRef,
+ dimensions,
+}: {
+ sourceRef: React.RefObject<VideoThumbnailRef | null>;
+ targetRef: React.RefObject<HTMLCanvasElement | null> | null;
+ dimensions: Dimensions;
+}) => {
+ const [drawn, setDrawn] = useState(
+ () => sourceRef.current?.hasBitmap ?? false,
+ );
+
+ const draw = useCallback(() => {
+ if (!targetRef) {
+ return;
+ }
+
+ sourceRef.current?.copy().then((map) => {
+ const ctx = targetRef.current?.getContext('2d');
+ if (!ctx) {
+ return;
+ }
+
+ ctx.drawImage(map, 0, 0, dimensions.width, dimensions.height);
+
+ setDrawn(true);
+ });
+ }, [sourceRef, targetRef, dimensions.width, dimensions.height]);
+
+ useEffect(() => {
+ const {current} = sourceRef;
+ if (!current) {
+ return;
+ }
+
+ current.addOnChangeListener(draw);
+ return () => {
+ current.removeOnChangeListener(draw);
+ };
+ }, [draw, sourceRef]);
+
+ useEffect(() => {
+ draw();
+ }, [draw]);
+
+ return drawn;
+};
diff --git a/packages/convert/app/root.tsx b/packages/convert/app/root.tsx
index 3f8f5ceb63..6fa04261cb 100644
--- a/packages/convert/app/root.tsx
+++ b/packages/convert/app/root.tsx
@@ -1,3 +1,4 @@
+import {DEFAULT_FAVICON} from './lib/default-favicon';
import './tailwind.css';
import {Links, Outlet, Scripts, ScrollRestoration} from '@remix-run/react';
@@ -10,7 +11,7 @@ export function Layout({children}: {readonly children: React.ReactNode}) {
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Remotion Convert</title>
<meta name="description" content="Remotion Convert" />
- <link rel="icon" href={`${import.meta.env.BASE_URL}pwa-icon-192.png`} />
+ <link rel="icon" href={DEFAULT_FAVICON} />
<link
rel="manifest"
href={`${import.meta.env.BASE_URL}manifest.json`}