Skip to content

🎥 Make videos programmatically with React

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

JonnyBurger49eaaf59fc2e
diff --git a/packages/media/src/extract-frame-and-audio.ts b/packages/media/src/extract-frame-and-audio.ts
index b7eb8b0997..c33b48a892 100644
--- a/packages/media/src/extract-frame-and-audio.ts
+++ b/packages/media/src/extract-frame-and-audio.ts
@@ -3,7 +3,6 @@ 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,
@@ -100,21 +99,9 @@ export const extractFrameAndAudio = async ({
};
}
- if (!frame?.frame) {
- return {
- type: 'success',
- frame: null,
- audio: audio?.data ?? null,
- durationInSeconds: audio?.durationInSeconds ?? null,
- };
- }
-
return {
type: 'success',
- frame: await rotateFrame({
- frame: frame.frame.toVideoFrame(),
- rotation: frame.frame.rotation,
- }),
+ frame: frame?.frame?.toVideoFrame() ?? null,
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 fc5dd230c6..30617e6cdc 100644
--- a/packages/media/src/test/browser.test.ts
+++ b/packages/media/src/test/browser.test.ts
@@ -41,6 +41,7 @@ test('Should be able to extract a frame', async () => {
assert(audio);
assert(frame);
+ expect((frame as VideoFrame).timestamp).toBe(1_000_000);
assert(audio);
@@ -93,6 +94,7 @@ 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);
@@ -214,5 +216,5 @@ test('Should be able to loop', async () => {
const {frame} = result;
- assert(frame);
+ expect((frame as VideoFrame)?.timestamp).toBe(41_000_000);
});
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 630a527d69..0ee99b8f68 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,9 +126,12 @@ if (
const {frame, audio, durationInSeconds} = result;
- const imageBitmap = frame ? await createImageBitmap(frame) : null;
- if (frame) {
- frame.close();
+ const videoFrame = frame;
+ const imageBitmap = videoFrame
+ ? await createImageBitmap(videoFrame)
+ : null;
+ if (videoFrame) {
+ videoFrame.close();
}
const response: ExtractFrameResponse = {
@@ -140,6 +143,7 @@ if (
};
window.remotion_broadcastChannel!.postMessage(response);
+ videoFrame?.close();
} catch (error) {
const response: ExtractFrameResponse = {
type: 'response-error',
@@ -159,7 +163,7 @@ if (
export type ExtractFrameViaBroadcastChannelResult =
| {
type: 'success';
- frame: ImageBitmap | null;
+ frame: ImageBitmap | VideoFrame | 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
deleted file mode 100644
index a53da2671c..0000000000
--- a/packages/media/src/video-extraction/rotate-frame.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-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 03bca94784..37ce50cab2 100644
--- a/packages/media/src/video/video-for-rendering.tsx
+++ b/packages/media/src/video/video-for-rendering.tsx
@@ -259,8 +259,14 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({
return;
}
- context.canvas.width = imageBitmap.width;
- context.canvas.height = imageBitmap.height;
+ context.canvas.width =
+ imageBitmap instanceof ImageBitmap
+ ? imageBitmap.width
+ : imageBitmap.displayWidth;
+ context.canvas.height =
+ imageBitmap instanceof ImageBitmap
+ ? imageBitmap.height
+ : imageBitmap.displayHeight;
context.canvas.style.aspectRatio = `${context.canvas.width} / ${context.canvas.height}`;
context.drawImage(imageBitmap, 0, 0);