Skip to content

🎥 Make videos programmatically with React

use thumbnail

Jonny Burgerc06efbb802f6
diff --git a/packages/convert/app/components/FileAvailable.tsx b/packages/convert/app/components/FileAvailable.tsx
index 4dbb476991..b46b2f988a 100644
--- a/packages/convert/app/components/FileAvailable.tsx
+++ b/packages/convert/app/components/FileAvailable.tsx
@@ -4,13 +4,14 @@ import React, {useCallback, useMemo, useRef, useState} from 'react';
import {Source} from '~/lib/convert-state';
import {defaultRotateOrMirorState, RotateOrMirrorState} from '~/lib/default-ui';
import {formatBytes} from '~/lib/format-bytes';
+import {useThumbnail} from '~/lib/use-thumbnail';
import {RouteAction} from '~/seo';
import ConvertUI from './ConvertUi';
import {Footer} from './Footer';
import {Probe} from './Probe';
import {VideoThumbnailRef} from './VideoThumbnail';
import {Button} from './ui/button';
-import {useProbe, useThumbnail} from './use-probe';
+import {useProbe} from './use-probe';
export const FileAvailable: React.FC<{
readonly src: Source;
@@ -53,7 +54,7 @@ export const FileAvailable: React.FC<{
const [enableRotateOrMirrow, setEnableRotateOrMirror] =
useState<RotateOrMirrorState>(() => defaultRotateOrMirorState(routeAction));
- useThumbnail({src, logLevel: 'verbose', onVideoThumbnail});
+ const {err} = useThumbnail({src, logLevel: 'verbose', onVideoThumbnail});
const probeResult = useProbe({
src,
logLevel: 'verbose',
@@ -94,6 +95,7 @@ export const FileAvailable: React.FC<{
</div>
<div className="lg:inline-flex lg:flex-row">
<Probe
+ thumbnailError={err}
src={src}
probeDetails={probeDetails}
setProbeDetails={setProbeDetails}
diff --git a/packages/convert/app/components/Probe.tsx b/packages/convert/app/components/Probe.tsx
index a78cdaf4b5..bd310a9263 100644
--- a/packages/convert/app/components/Probe.tsx
+++ b/packages/convert/app/components/Probe.tsx
@@ -23,6 +23,7 @@ export const Probe: React.FC<{
readonly userRotation: number;
readonly mirrorHorizontal: boolean;
readonly mirrorVertical: boolean;
+ readonly thumbnailError: Error | null;
}> = ({
src,
probeDetails,
@@ -32,6 +33,7 @@ export const Probe: React.FC<{
userRotation,
mirrorHorizontal,
mirrorVertical,
+ thumbnailError,
}) => {
const {
audioCodec,
@@ -80,7 +82,7 @@ export const Probe: React.FC<{
return (
<Card className="w-full lg:w-[350px] overflow-hidden">
<div className="flex flex-row lg:flex-col w-full border-b-2 border-black">
- {error ? null : (
+ {error ? null : thumbnailError ? null : (
<VideoThumbnail
ref={videoThumbnailRef}
smallThumbOnMobile
diff --git a/packages/convert/app/components/use-probe.ts b/packages/convert/app/components/use-probe.ts
index bd5773e496..79480e5ffa 100644
--- a/packages/convert/app/components/use-probe.ts
+++ b/packages/convert/app/components/use-probe.ts
@@ -16,71 +16,6 @@ import {Source} from '~/lib/convert-state';
export type ProbeResult = ReturnType<typeof useProbe>;
-export const useThumbnail = ({
- src,
- logLevel,
- onVideoThumbnail,
-}: {
- src: Source;
- logLevel: LogLevel;
- onVideoThumbnail: (videoFrame: VideoFrame) => void;
-}) => {
- const abortController = new AbortController();
- parseMedia({
- signal: abortController.signal,
- reader: src.type === 'file' ? webFileReader : fetchReader,
- src: src.type === 'file' ? src.file : src.url,
- logLevel,
- onVideoTrack: async (track) => {
- if (typeof VideoDecoder === 'undefined') {
- return null;
- }
-
- let frames = 0;
-
- const decoder = new VideoDecoder({
- error: (error) => {
- // eslint-disable-next-line no-console
- console.log(error);
- },
- output(frame) {
- onVideoThumbnail(frame);
- frame.close();
- },
- });
-
- if (!(await VideoDecoder.isConfigSupported(track)).supported) {
- return null;
- }
-
- // TODO: See if possible
- decoder.configure(track);
- return (sample) => {
- frames++;
- if (frames >= 30) {
- abortController.abort();
- }
-
- decoder.decode(new EncodedVideoChunk(sample));
- };
- },
- }).catch((err) => {
- if ((err as Error).stack?.includes('Cancelled')) {
- return;
- }
- if ((err as Error).stack?.toLowerCase()?.includes('aborted')) {
- return;
- }
- // firefox
- if ((err as Error).message?.toLowerCase()?.includes('aborted')) {
- return;
- }
-
- // eslint-disable-next-line no-console
- console.log(err);
- });
-};
-
export const useProbe = ({
src,
logLevel,
diff --git a/packages/convert/app/lib/use-thumbnail.ts b/packages/convert/app/lib/use-thumbnail.ts
new file mode 100644
index 0000000000..5be252d9d8
--- /dev/null
+++ b/packages/convert/app/lib/use-thumbnail.ts
@@ -0,0 +1,96 @@
+import {AudioOrVideoSample, LogLevel, parseMedia} from '@remotion/media-parser';
+import {fetchReader} from '@remotion/media-parser/fetch';
+import {webFileReader} from '@remotion/media-parser/web-file';
+import {useCallback, useEffect, useMemo, useState} from 'react';
+import {Source} from './convert-state';
+
+export const useThumbnail = ({
+ src,
+ logLevel,
+ onVideoThumbnail,
+}: {
+ src: Source;
+ logLevel: LogLevel;
+ onVideoThumbnail: (videoFrame: VideoFrame) => void;
+}) => {
+ const [err, setError] = useState<Error | null>(null);
+
+ const execute = useCallback(() => {
+ const abortController = new AbortController();
+ parseMedia({
+ signal: abortController.signal,
+ reader: src.type === 'file' ? webFileReader : fetchReader,
+ src: src.type === 'file' ? src.file : src.url,
+ logLevel,
+ onVideoTrack: async (track) => {
+ if (typeof VideoDecoder === 'undefined') {
+ return null;
+ }
+
+ let frames = 0;
+
+ const decoder = new VideoDecoder({
+ error: (error) => {
+ // eslint-disable-next-line no-console
+ console.log(error);
+ setError(error);
+ abortController.abort();
+ },
+ output(frame) {
+ onVideoThumbnail(frame);
+ frame.close();
+ },
+ });
+
+ if (!(await VideoDecoder.isConfigSupported(track)).supported) {
+ abortController.abort();
+ setError(new Error('Video configuration not supported'));
+ return null;
+ }
+
+ decoder.configure(track);
+
+ return (sample) => {
+ const remappedSample: AudioOrVideoSample = {
+ ...sample,
+ duration: 1500,
+ };
+ frames++;
+ if (frames >= 10) {
+ abortController.abort();
+ }
+
+ decoder.decode(new EncodedVideoChunk(remappedSample));
+ };
+ },
+ }).catch((err) => {
+ if ((err as Error).stack?.includes('Cancelled')) {
+ return;
+ }
+ if ((err as Error).stack?.toLowerCase()?.includes('aborted')) {
+ return;
+ }
+ // firefox
+ if ((err as Error).message?.toLowerCase()?.includes('aborted')) {
+ return;
+ }
+
+ console.log(err);
+ setError(err as Error);
+ });
+
+ return abortController;
+ }, [logLevel, onVideoThumbnail, src]);
+
+ useEffect(() => {
+ const task = execute();
+
+ return () => {
+ task.abort();
+ };
+ }, [execute]);
+
+ return useMemo(() => {
+ return {err};
+ }, [err]);
+};