Skip to content

🎥 Make videos programmatically with React

show keyframes info

Jonny Burger7019a978ea1f
diff --git a/packages/convert/app/components/KeyframesInfo.tsx b/packages/convert/app/components/KeyframesInfo.tsx
new file mode 100644
index 0000000000..bc5ba3e835
--- /dev/null
+++ b/packages/convert/app/components/KeyframesInfo.tsx
@@ -0,0 +1,84 @@
+import {MediaParserKeyframe} from '@remotion/media-parser';
+import React from 'react';
+import {TableCell, TableRow} from './ui/table';
+
+const KeyframesChart: React.FC<{
+ readonly keyframes: MediaParserKeyframe[];
+ readonly durationInSeconds: number;
+}> = ({keyframes, durationInSeconds}) => {
+ const averageEverySeconds = durationInSeconds / keyframes.length;
+
+ return (
+ <div>
+ <div className="rounded-lg border-2 border-black h-10 p-[2px] px-2">
+ <div className="relative w-full h-full rounded ">
+ {keyframes.map((k) => {
+ const left =
+ (k.presentationTimeInSeconds / durationInSeconds) * 100;
+ return (
+ <div
+ key={k.presentationTimeInSeconds}
+ className="absolute h-full bg-black"
+ style={{
+ left: `${left}%`,
+ width: keyframes.length > 50 ? '1px' : '2px',
+ }}
+ />
+ );
+ })}
+ </div>
+ </div>
+ <div className="flex flex-row mt-1">
+ <div className="text-muted-foreground text-xs pl-2">0s</div>
+ <div className="flex-1" />
+ <div className="text-muted-foreground text-xs pr-2">
+ {durationInSeconds.toFixed(0)}s
+ </div>
+ </div>
+ <div className="text-muted-foreground text-xs mt-2">
+ On average, a keyframe appears every {averageEverySeconds.toFixed(2)}{' '}
+ seconds.
+ </div>
+ </div>
+ );
+};
+
+export const KeyframesInfo: React.FC<{
+ readonly keyframes: MediaParserKeyframe[];
+ readonly trackId: number;
+ readonly durationInSeconds: number | null;
+}> = ({keyframes, trackId, durationInSeconds}) => {
+ const trackKeyframes = React.useMemo(
+ () => keyframes.filter((k) => k.trackId === trackId),
+ [keyframes, trackId],
+ );
+
+ if (trackKeyframes.length === 0) {
+ return null;
+ }
+
+ if (durationInSeconds === null) {
+ return null;
+ }
+
+ return (
+ <>
+ <TableRow
+ style={{
+ borderBottom: 0,
+ }}
+ >
+ <TableCell className="font-brand">Keyframes</TableCell>
+ <TableCell className="text-right">{trackKeyframes.length}</TableCell>
+ </TableRow>
+ <TableRow>
+ <TableCell colSpan={2} className="pt-0">
+ <KeyframesChart
+ keyframes={trackKeyframes}
+ durationInSeconds={durationInSeconds}
+ />
+ </TableCell>
+ </TableRow>
+ </>
+ );
+};
diff --git a/packages/convert/app/components/Probe.tsx b/packages/convert/app/components/Probe.tsx
index 5c352e343d..0f3a345601 100644
--- a/packages/convert/app/components/Probe.tsx
+++ b/packages/convert/app/components/Probe.tsx
@@ -51,6 +51,7 @@ export const Probe: React.FC<{
error,
metadata,
location,
+ keyframes,
} = probeResult;
const onClick = useCallback(() => {
@@ -146,6 +147,8 @@ export const Probe: React.FC<{
location={location}
metadata={metadata}
track={selectedTrack}
+ keyframes={keyframes}
+ durationInSeconds={durationInSeconds}
/>
) : selectedTrack.type === 'audio' ? (
<AudioTrackOverview
diff --git a/packages/convert/app/components/VideoTrackOverview.tsx b/packages/convert/app/components/VideoTrackOverview.tsx
index 989acea4e8..75c7e74533 100644
--- a/packages/convert/app/components/VideoTrackOverview.tsx
+++ b/packages/convert/app/components/VideoTrackOverview.tsx
@@ -1,18 +1,22 @@
import {Table, TableBody, TableCell, TableRow} from '@/components/ui/table';
import type {
+ MediaParserKeyframe,
MediaParserLocation,
MetadataEntry,
VideoTrack,
} from '@remotion/media-parser';
import React from 'react';
import {renderHumanReadableVideoCodec} from '~/lib/render-codec-label';
+import {KeyframesInfo} from './KeyframesInfo';
import {MetadataDisplay} from './MetadataTable';
export const VideoTrackOverview: React.FC<{
readonly track: VideoTrack;
readonly metadata: MetadataEntry[] | null;
readonly location: MediaParserLocation | null;
-}> = ({track, metadata, location}) => {
+ readonly keyframes: MediaParserKeyframe[] | null;
+ readonly durationInSeconds: number | null;
+}> = ({track, metadata, location, keyframes, durationInSeconds}) => {
return (
<Table className="table-fixed">
<TableBody>
@@ -47,6 +51,13 @@ export const VideoTrackOverview: React.FC<{
{track.sampleAspectRatio.denominator}
</TableCell>
</TableRow>
+ {keyframes !== null ? (
+ <KeyframesInfo
+ durationInSeconds={durationInSeconds}
+ keyframes={keyframes}
+ trackId={track.trackId}
+ />
+ ) : null}
<TableRow>
<TableCell className="font-brand">Rotation</TableCell>
<TableCell className="text-right">{track.rotation}°</TableCell>
diff --git a/packages/convert/app/components/use-probe.ts b/packages/convert/app/components/use-probe.ts
index 92723ba10c..58ddada80a 100644
--- a/packages/convert/app/components/use-probe.ts
+++ b/packages/convert/app/components/use-probe.ts
@@ -2,6 +2,7 @@ import {
Dimensions,
LogLevel,
MediaParserAudioCodec,
+ MediaParserKeyframe,
MediaParserLocation,
MediaParserVideoCodec,
MetadataEntry,
@@ -45,6 +46,9 @@ export const useProbe = ({
const [location, setLocation] = useState<MediaParserLocation | null>(null);
const [tracks, setTracks] = useState<TracksField | null>(null);
const [container, setContainer] = useState<ParseMediaContainer | null>(null);
+ const [keyframes, setKeyframes] = useState<MediaParserKeyframe[] | null>(
+ null,
+ );
const [done, setDone] = useState(false);
const [error, setError] = useState<Error | null>(null);
@@ -67,6 +71,7 @@ export const useProbe = ({
rotation: true,
metadata: true,
location: true,
+ keyframes: true,
},
onParseProgress: onProgress,
reader: src.type === 'file' ? webFileReader : fetchReader,
@@ -110,6 +115,9 @@ export const useProbe = ({
onSize: (s) => {
setSize(s);
},
+ onKeyframes: (k) => {
+ setKeyframes(k);
+ },
})
.then(() => {})
.catch((err) => {
@@ -159,6 +167,7 @@ export const useProbe = ({
rotation,
metadata,
location,
+ keyframes,
};
}, [
audioCodec,
@@ -176,5 +185,6 @@ export const useProbe = ({
rotation,
metadata,
location,
+ keyframes,
]);
};