Skip to content

🎥 Make videos programmatically with React

video thumbnail

Jonny Burgerdd7d1ce7af0b
diff --git a/packages/convert/app/components/Probe.tsx b/packages/convert/app/components/Probe.tsx
index a98ffa68b7..e0274a38da 100644
--- a/packages/convert/app/components/Probe.tsx
+++ b/packages/convert/app/components/Probe.tsx
@@ -11,6 +11,7 @@ import {AudioTrackOverview} from './AudioTrackOverview';
import {ContainerOverview} from './ContainerOverview';
import {SourceLabel} from './SourceLabel';
import {TrackSwitcher} from './TrackSwitcher';
+import {VideoThumbnail} from './VideoThumbnail';
import {VideoTrackOverview} from './VideoTrackOverview';
import {Button} from './ui/button';
import {
@@ -22,6 +23,7 @@ import {
CardTitle,
} from './ui/card';
import {ScrollArea} from './ui/scroll-area';
+import {Separator} from './ui/separator';
import {Skeleton} from './ui/skeleton';
export const Probe: React.FC<{
@@ -44,9 +46,38 @@ export const Probe: React.FC<{
const [size, setSize] = useState<number | null>(null);
const [tracks, setTracks] = useState<TracksField | null>(null);
const [container, setContainer] = useState<ParseMediaContainer | null>(null);
+ const [thumbnail, setThumbnail] = useState<VideoFrame | null>(null);
const getStart = useCallback(() => {
const controller = new AbortController();
+ let hasFps = false;
+ let hasDuration = false;
+ let hasDimensions = false;
+ let hasVideoCodec = false;
+ let hasAudioCodec = false;
+ let hasSize = false;
+ let hasName = false;
+ let hasFrame = false;
+ let hasContainer = false;
+ let hasTracks = false;
+
+ const cancelIfDone = () => {
+ if (
+ hasFps &&
+ hasDuration &&
+ hasDimensions &&
+ hasVideoCodec &&
+ hasAudioCodec &&
+ hasSize &&
+ hasName &&
+ hasFrame &&
+ hasContainer &&
+ hasTracks
+ ) {
+ controller.abort(new Error('Cancelled (all info)'));
+ }
+ };
+
parseMedia({
src,
fields: {
@@ -61,32 +92,96 @@ export const Probe: React.FC<{
container: true,
},
signal: controller.signal,
+ 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) {
+ frames++;
+ if (frames < 30) {
+ frame.close();
+ return;
+ }
+
+ setThumbnail((f) => {
+ if (f) {
+ frame.close();
+ return f;
+ }
+
+ return frame;
+ });
+ hasFrame = true;
+ cancelIfDone();
+ },
+ });
+
+ if (!(await VideoDecoder.isConfigSupported(track)).supported) {
+ return null;
+ }
+
+ // TODO: See if possible
+ decoder.configure(track);
+ return (sample) => {
+ if (hasFrame) {
+ return;
+ }
+
+ decoder.decode(new EncodedVideoChunk(sample));
+ };
+ },
onContainer(c) {
+ hasContainer = true;
setContainer(c);
+ cancelIfDone();
},
onAudioCodec: (codec) => {
+ hasAudioCodec = true;
setAudioCodec(codec);
+ cancelIfDone();
},
onFps: (f) => {
+ hasFps = true;
setFps(f);
+ cancelIfDone();
},
onDurationInSeconds: (d) => {
+ hasDuration = true;
setDurationInSeconds(d);
+ cancelIfDone();
},
onName: (n) => {
+ hasName = true;
setName(n);
+ cancelIfDone();
},
onDimensions(dim) {
+ hasDimensions = true;
setDimensions(dim);
+ cancelIfDone();
},
onVideoCodec: (codec) => {
+ hasVideoCodec = true;
setVideoCodec(codec);
+ cancelIfDone();
},
onTracks: (trx) => {
+ hasTracks = true;
setTracks(trx);
+ cancelIfDone();
},
onSize(s) {
+ hasSize = true;
setSize(s);
+ cancelIfDone();
},
})
.then(() => {})
@@ -137,9 +232,10 @@ export const Probe: React.FC<{
<Card
className={
(probeDetails ? 'w-[800px]' : 'w-[350px]') +
- ' h-5/6 max-h-[700px] flex flex-col max-w-[90vw]'
+ ' h-5/6 max-h-[700px] flex flex-col max-w-[90vw] overflow-hidden'
}
>
+ <VideoThumbnail thumbnail={thumbnail} />
<CardHeader>
<CardTitle title={name ?? undefined}>
{name ? name : <Skeleton className="h-5 w-[220px] inline-block" />}
@@ -178,7 +274,8 @@ export const Probe: React.FC<{
) : null}
</CardContent>
</ScrollArea>
- <CardFooter className="flex justify-between">
+ <Separator orientation="horizontal" />
+ <CardFooter className="flex flex-row items-center justify-center pb-3 pt-3">
<div className="flex-1" />
<Button disabled={!tracks} onClick={onClick} variant={'link'}>
{probeDetails ? 'Hide details' : 'Show details'}
diff --git a/packages/convert/app/components/VideoThumbnail.tsx b/packages/convert/app/components/VideoThumbnail.tsx
new file mode 100644
index 0000000000..cf9b053087
--- /dev/null
+++ b/packages/convert/app/components/VideoThumbnail.tsx
@@ -0,0 +1,42 @@
+import {useEffect, useRef} from 'react';
+
+const THUMBNAIL_WIDTH = 350;
+const THUMBNAIL_HEIGHT = Math.round((THUMBNAIL_WIDTH / 16) * 9);
+
+export const VideoThumbnail: React.FC<{
+ readonly thumbnail: VideoFrame | null;
+}> = ({thumbnail}) => {
+ const ref = useRef<HTMLCanvasElement>(null);
+
+ useEffect(() => {
+ if (!thumbnail) {
+ return;
+ }
+
+ createImageBitmap(thumbnail, {
+ resizeWidth: THUMBNAIL_WIDTH,
+ resizeHeight: THUMBNAIL_HEIGHT,
+ }).then((bitmap) => {
+ const twoDContext = ref.current?.getContext('2d');
+ if (!twoDContext) {
+ return;
+ }
+
+ twoDContext.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height);
+ thumbnail.close();
+ });
+ }, [thumbnail]);
+
+ return (
+ <canvas
+ ref={ref}
+ width={THUMBNAIL_WIDTH}
+ height={THUMBNAIL_HEIGHT}
+ style={{
+ maxHeight: THUMBNAIL_HEIGHT,
+ width: THUMBNAIL_WIDTH,
+ aspectRatio: '16 / 9',
+ }}
+ />
+ );
+};