Skip to content

🎥 Make videos programmatically with React

onDone

Jonny Burgerf48f4c4805e9
diff --git a/packages/convert/app/components/FileAvailable.tsx b/packages/convert/app/components/FileAvailable.tsx
index 0811cb3fd5..1032b7ecf8 100644
--- a/packages/convert/app/components/FileAvailable.tsx
+++ b/packages/convert/app/components/FileAvailable.tsx
@@ -46,7 +46,7 @@ export const FileAvailable: React.FC<{
}, []);
const onDone = useCallback(() => {
- videoThumbnailRef.current?.reveal();
+ videoThumbnailRef.current?.onDone();
}, []);
const onProgress: ParseMediaOnProgress = useCallback(
diff --git a/packages/convert/app/components/VideoThumbnail.tsx b/packages/convert/app/components/VideoThumbnail.tsx
index 2c05539cd8..b9db8bff97 100644
--- a/packages/convert/app/components/VideoThumbnail.tsx
+++ b/packages/convert/app/components/VideoThumbnail.tsx
@@ -14,7 +14,7 @@ type Props = {
export type VideoThumbnailRef = {
draw: (videoFrame: VideoFrame) => void;
- reveal: () => void;
+ onDone: () => void;
};
const VideoThumbnailRefForward: React.ForwardRefRenderFunction<
@@ -35,6 +35,7 @@ const VideoThumbnailRefForward: React.ForwardRefRenderFunction<
const [color, setColor] = useState<string>('transparent');
const [dimensions, setDimensions] = useState<{width: number}>({width: 0});
const [reveal, setReveal] = useState(initialReveal);
+ const [drawn, setDrawn] = useState(false);
const drawThumbnail = useCallback((unrotatedVideoFrame: VideoFrame) => {
const scaleRatio = THUMBNAIL_HEIGHT / unrotatedVideoFrame.displayHeight;
@@ -59,13 +60,16 @@ const VideoThumbnailRefForward: React.ForwardRefRenderFunction<
return new FastAverageColor().getColor(canvas).hex;
});
+ setDrawn(true);
}, []);
useImperativeHandle(
forwardedRef,
() => ({
draw: drawThumbnail,
- reveal: () => setReveal(true),
+ onDone: () => {
+ setReveal(true);
+ },
}),
[drawThumbnail],
);
@@ -80,13 +84,13 @@ const VideoThumbnailRefForward: React.ForwardRefRenderFunction<
return (
<div
- className="border-b-2 border-black overflow-hidden"
+ className="border-b-2 border-black overflow-hidden bg-slate-100"
// +2 to account for border
style={{height: THUMBNAIL_HEIGHT * scale + 2}}
>
<div
className="flex justify-center transition-opacity"
- style={{backgroundColor: color, opacity: reveal ? 1 : 0}}
+ style={{backgroundColor: color, opacity: reveal && drawn ? 1 : 0}}
>
<canvas
ref={ref}
diff --git a/packages/convert/app/lib/use-thumbnail.ts b/packages/convert/app/lib/use-thumbnail.ts
index 0d5dad6453..bd2ca44270 100644
--- a/packages/convert/app/lib/use-thumbnail.ts
+++ b/packages/convert/app/lib/use-thumbnail.ts
@@ -24,12 +24,15 @@ export const useThumbnail = ({
reader: src.type === 'file' ? webFileReader : fetchReader,
src: src.type === 'file' ? src.file : src.url,
logLevel,
- onVideoTrack: async ({track}) => {
+ onVideoTrack: async ({track, container}) => {
if (typeof VideoDecoder === 'undefined') {
return null;
}
let frames = 0;
+ const onlyKeyframes =
+ container !== 'transport-stream' && container !== 'webm';
+ const framesToGet = onlyKeyframes ? 30 : 3;
const decoder = new VideoDecoder({
error: (error) => {
@@ -39,6 +42,13 @@ export const useThumbnail = ({
abortController.abort();
},
output(frame) {
+ if (frames >= framesToGet) {
+ abortController.abort();
+ onDone();
+ return;
+ }
+ frames++;
+
onVideoThumbnail(frame).then(() => {
frame.close();
});
@@ -54,42 +64,35 @@ export const useThumbnail = ({
decoder.configure(track);
return (sample) => {
- if (sample.type !== 'key') {
+ if (sample.type !== 'key' && onlyKeyframes) {
return;
}
- if (frames >= 3) {
- abortController.abort();
- return;
+ if (sample.type === 'key') {
+ decoder.flush();
}
- frames++;
- decoder.flush();
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;
- }
+ }).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);
- })
- .finally(() => {
- onDone();
- });
+ console.log(err);
+ setError(err as Error);
+ });
return abortController;
- }, [logLevel, onDone, onVideoThumbnail, src]);
+ }, [logLevel, onVideoThumbnail, src]);
useEffect(() => {
const task = execute();