Skip to content

🎥 Make videos programmatically with React

awesome makes a lot of sense

Jonny Burger14ae0c43adb2
diff --git a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
index 17f4a7e40e..3ae962ee10 100644
--- a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
+++ b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
@@ -5,6 +5,7 @@ import {useVideoConfig} from 'remotion';
import type {FrameDatabaseKey} from '../../helpers/frame-database';
import {
frameDatabase,
+ getAspectRatioFromCache,
getTimestampFromFrameDatabaseKey,
makeFrameDatabaseKey,
} from '../../helpers/frame-database';
@@ -51,28 +52,24 @@ const calculateTimestampSlots = ({
return timestampTargets;
};
-const createSlots = ({
+const ensureSlots = ({
filledSlots,
visualizationWidth,
fromSeconds,
toSeconds,
+ aspectRatio,
}: {
filledSlots: Map<number, number | undefined>;
visualizationWidth: number;
fromSeconds: number;
toSeconds: number;
+ aspectRatio: number;
}) => {
if (filledSlots.size > 0) {
return;
}
- const anyFrame = frameDatabase.values().next().value;
- if (!anyFrame) {
- return;
- }
-
const segmentDuration = toSeconds - fromSeconds;
- const aspectRatio = anyFrame.displayWidth / anyFrame.displayHeight;
const timestampTargets = calculateTimestampSlots({
visualizationWidth,
@@ -92,12 +89,16 @@ const fillWithCachedFrames = ({
ctx,
visualizationWidth,
filledSlots,
+ src,
}: {
ctx: CanvasRenderingContext2D;
visualizationWidth: number;
filledSlots: Map<number, number | undefined>;
+ src: string;
}) => {
- const keys = Array.from(frameDatabase.keys());
+ const keys = Array.from(frameDatabase.keys()).filter((k) =>
+ k.startsWith(src),
+ );
const targets = Array.from(filledSlots.keys());
for (let i = 0; i < filledSlots.size; i++) {
@@ -167,7 +168,6 @@ const fillFrameWhereItFits = ({
continue;
}
- console.log('draw');
ctx.drawImage(frame, (i / filledSlots.size) * visualizationWidth, 0);
filledSlots.set(slot, frame.timestamp);
}
@@ -182,6 +182,7 @@ export const TimelineVideoInfo: React.FC<{
const {fps} = useVideoConfig();
const ref = useRef<HTMLDivElement>(null);
const [error, setError] = useState<Error | null>(null);
+ const aspectRatio = useRef<number | null>(getAspectRatioFromCache(src));
useEffect(() => {
if (error) {
@@ -193,8 +194,6 @@ export const TimelineVideoInfo: React.FC<{
return;
}
- console.log('TimelineVideoInfo', frameDatabase);
-
const canvas = document.createElement('canvas');
canvas.width = visualizationWidth;
canvas.height = HEIGHT;
@@ -209,45 +208,47 @@ export const TimelineVideoInfo: React.FC<{
const fromSeconds = startFrom / fps;
const toSeconds = (startFrom + durationInFrames) / fps;
- createSlots({
- filledSlots,
- visualizationWidth,
- fromSeconds,
- toSeconds,
- });
+ if (aspectRatio.current !== null) {
+ ensureSlots({
+ filledSlots,
+ visualizationWidth,
+ fromSeconds,
+ toSeconds,
+ aspectRatio: aspectRatio.current,
+ });
- fillWithCachedFrames({
- ctx,
- visualizationWidth,
- filledSlots,
- });
+ fillWithCachedFrames({
+ ctx,
+ visualizationWidth,
+ filledSlots,
+ src,
+ });
+ }
current.appendChild(canvas);
const controller = new AbortController();
- console.log('extracting', fromSeconds, toSeconds);
extractFrames({
acknowledgeRemotionLicense: true,
timestampsInSeconds: ({track}) => {
- const aspectRatio = track.width / track.height;
- const framesFitInWidth = Math.ceil(
- visualizationWidth / (HEIGHT * aspectRatio),
- );
- const timestampTargets: number[] = [];
- const segmentDuration = toSeconds - fromSeconds;
+ aspectRatio.current =
+ track.displayAspectWidth / track.displayAspectHeight;
- for (let i = 0; i < framesFitInWidth; i++) {
- timestampTargets.push(
- fromSeconds + (segmentDuration / framesFitInWidth) * (i + 0.5),
- );
- }
+ ensureSlots({
+ filledSlots,
+ fromSeconds,
+ toSeconds,
+ visualizationWidth,
+ aspectRatio: aspectRatio.current,
+ });
- return timestampTargets;
+ return Array.from(filledSlots.keys()).map(
+ (timestamp) => timestamp / WEBCODECS_TIMESCALE,
+ );
},
src,
onFrame: (frame) => {
- console.log('onFrame', frame.timestamp);
const scale = HEIGHT / frame.displayHeight;
const transformed = WebCodecsInternals.rotateAndResizeVideoFrame({
@@ -272,11 +273,16 @@ export const TimelineVideoInfo: React.FC<{
}
frameDatabase.set(databaseKey, transformed);
- createSlots({
+ if (aspectRatio.current === null) {
+ throw new Error('Aspect ratio is not set');
+ }
+
+ ensureSlots({
filledSlots,
fromSeconds,
toSeconds,
visualizationWidth,
+ aspectRatio: aspectRatio.current,
});
fillFrameWhereItFits({
ctx,
@@ -292,6 +298,7 @@ export const TimelineVideoInfo: React.FC<{
ctx,
visualizationWidth,
filledSlots,
+ src,
});
})
.catch((e) => {
diff --git a/packages/studio/src/helpers/frame-database.ts b/packages/studio/src/helpers/frame-database.ts
index a0efca20f8..eae9fa7bd3 100644
--- a/packages/studio/src/helpers/frame-database.ts
+++ b/packages/studio/src/helpers/frame-database.ts
@@ -10,6 +10,21 @@ export const makeFrameDatabaseKey = (
export const frameDatabase: Map<FrameDatabaseKey, VideoFrame> = new Map();
export const getTimestampFromFrameDatabaseKey = (key: FrameDatabaseKey) => {
- const [, timestamp] = key.split('-');
- return Number(timestamp);
+ const split = key.split('-');
+ return Number(split[split.length - 1]);
+};
+
+export const getAspectRatioFromCache = (src: string) => {
+ // find any key that has the same src and is before the timestamp
+ const keys = frameDatabase.keys();
+ for (const key of keys) {
+ if (key.startsWith(src)) {
+ const value = frameDatabase.get(key);
+ if (value) {
+ return value.displayWidth / value.displayHeight;
+ }
+ }
+ }
+
+ return null;
};