Skip to content

🎥 Make videos programmatically with React

Reapply "`@remotion/media`: Handle frames with rotation"

JonnyBurger630f32d8a608
diff --git a/packages/media/src/extract-frame-and-audio.ts b/packages/media/src/extract-frame-and-audio.ts
index c33b48a892..b7eb8b0997 100644
--- a/packages/media/src/extract-frame-and-audio.ts
+++ b/packages/media/src/extract-frame-and-audio.ts
@@ -3,6 +3,7 @@ import {extractAudio} from './audio-extraction/extract-audio';
import {isNetworkError} from './is-network-error';
import {extractFrame} from './video-extraction/extract-frame';
import type {ExtractFrameViaBroadcastChannelResult} from './video-extraction/extract-frame-via-broadcast-channel';
+import {rotateFrame} from './video-extraction/rotate-frame';
export const extractFrameAndAudio = async ({
src,
@@ -99,9 +100,21 @@ export const extractFrameAndAudio = async ({
};
}
+ if (!frame?.frame) {
+ return {
+ type: 'success',
+ frame: null,
+ audio: audio?.data ?? null,
+ durationInSeconds: audio?.durationInSeconds ?? null,
+ };
+ }
+
return {
type: 'success',
- frame: frame?.frame?.toVideoFrame() ?? null,
+ frame: await rotateFrame({
+ frame: frame.frame.toVideoFrame(),
+ rotation: frame.frame.rotation,
+ }),
audio: audio?.data ?? null,
durationInSeconds: audio?.durationInSeconds ?? null,
};
diff --git a/packages/media/src/test/browser.test.ts b/packages/media/src/test/browser.test.ts
index 30617e6cdc..fc5dd230c6 100644
--- a/packages/media/src/test/browser.test.ts
+++ b/packages/media/src/test/browser.test.ts
@@ -41,7 +41,6 @@ test('Should be able to extract a frame', async () => {
assert(audio);
assert(frame);
- expect((frame as VideoFrame).timestamp).toBe(1_000_000);
assert(audio);
@@ -94,7 +93,6 @@ test('Should be able to extract the last frame', async () => {
const {audio, frame} = result;
assert(frame);
- expect((frame as VideoFrame).timestamp).toBe(59_958_333);
assert(!audio);
@@ -216,5 +214,5 @@ test('Should be able to loop', async () => {
const {frame} = result;
- expect((frame as VideoFrame)?.timestamp).toBe(41_000_000);
+ assert(frame);
});
diff --git a/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts b/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
index 0ee99b8f68..630a527d69 100644
--- a/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
+++ b/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
@@ -126,12 +126,9 @@ if (
const {frame, audio, durationInSeconds} = result;
- const videoFrame = frame;
- const imageBitmap = videoFrame
- ? await createImageBitmap(videoFrame)
- : null;
- if (videoFrame) {
- videoFrame.close();
+ const imageBitmap = frame ? await createImageBitmap(frame) : null;
+ if (frame) {
+ frame.close();
}
const response: ExtractFrameResponse = {
@@ -143,7 +140,6 @@ if (
};
window.remotion_broadcastChannel!.postMessage(response);
- videoFrame?.close();
} catch (error) {
const response: ExtractFrameResponse = {
type: 'response-error',
@@ -163,7 +159,7 @@ if (
export type ExtractFrameViaBroadcastChannelResult =
| {
type: 'success';
- frame: ImageBitmap | VideoFrame | null;
+ frame: ImageBitmap | null;
audio: PcmS16AudioData | null;
durationInSeconds: number | null;
}
diff --git a/packages/media/src/video-extraction/rotate-frame.ts b/packages/media/src/video-extraction/rotate-frame.ts
new file mode 100644
index 0000000000..a53da2671c
--- /dev/null
+++ b/packages/media/src/video-extraction/rotate-frame.ts
@@ -0,0 +1,42 @@
+export const rotateFrame = ({
+ frame,
+ rotation,
+}: {
+ frame: VideoFrame;
+ rotation: number;
+}): Promise<ImageBitmap> => {
+ if (rotation === 0) {
+ return createImageBitmap(frame);
+ }
+
+ const width =
+ rotation === 90 || rotation === 270
+ ? frame.displayHeight
+ : frame.displayWidth;
+ const height =
+ rotation === 90 || rotation === 270
+ ? frame.displayWidth
+ : frame.displayHeight;
+
+ const canvas = new OffscreenCanvas(width, height);
+ const ctx = canvas.getContext('2d');
+ if (!ctx) {
+ throw new Error('Could not get 2d context');
+ }
+
+ canvas.width = width;
+ canvas.height = height;
+
+ if (rotation === 90) {
+ ctx.translate(width, 0);
+ } else if (rotation === 180) {
+ ctx.translate(width, height);
+ } else if (rotation === 270) {
+ ctx.translate(0, height);
+ }
+
+ ctx.rotate(rotation * (Math.PI / 180));
+ ctx.drawImage(frame, 0, 0);
+
+ return createImageBitmap(canvas);
+};
diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsx
index 37ce50cab2..03bca94784 100644
--- a/packages/media/src/video/video-for-rendering.tsx
+++ b/packages/media/src/video/video-for-rendering.tsx
@@ -259,14 +259,8 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({
return;
}
- context.canvas.width =
- imageBitmap instanceof ImageBitmap
- ? imageBitmap.width
- : imageBitmap.displayWidth;
- context.canvas.height =
- imageBitmap instanceof ImageBitmap
- ? imageBitmap.height
- : imageBitmap.displayHeight;
+ context.canvas.width = imageBitmap.width;
+ context.canvas.height = imageBitmap.height;
context.canvas.style.aspectRatio = `${context.canvas.width} / ${context.canvas.height}`;
context.drawImage(imageBitmap, 0, 0);