🎥 Make videos programmatically with React
make it work with hardcoded rotation
Jonny Burger46a8bc657bde
diff --git a/packages/webcodecs/src/on-frame.ts b/packages/webcodecs/src/on-frame.tsindex 27e5289404..28471abff9 100644--- a/packages/webcodecs/src/on-frame.ts+++ b/packages/webcodecs/src/on-frame.ts@@ -2,10 +2,12 @@ import type {VideoTrack} from '@remotion/media-parser';import type {ConvertMediaOnVideoFrame} from './convert-media';import {convertToCorrectVideoFrame} from './convert-to-correct-videoframe';import type {ConvertMediaVideoCodec} from './get-available-video-codecs';+import {rotateVideo} from './rotate-video';+import {HARDCODED_ROTATE} from './rotation';import type {WebCodecsVideoEncoder} from './video-encoder';export const onFrame = async ({- frame,+ frame: unrotatedFrame,onVideoFrame,videoEncoder,track,@@ -17,45 +19,55 @@ export const onFrame = async ({track: VideoTrack;outputCodec: ConvertMediaVideoCodec;}) => {- const newFrame = onVideoFrame ? await onVideoFrame({frame, track}) : frame;+ const rotated = rotateVideo({+ rotation: HARDCODED_ROTATE,+ frame: unrotatedFrame,+ });+ if (unrotatedFrame !== rotated) {+ unrotatedFrame.close();+ }++ const userProcessedFrame = onVideoFrame+ ? await onVideoFrame({frame: rotated, track})+ : rotated;- if (newFrame.displayWidth !== frame.displayWidth) {+ if (userProcessedFrame.displayWidth !== rotated.displayWidth) {throw new Error(- `Returned VideoFrame of track ${track.trackId} has different displayWidth (${newFrame.displayWidth}) than the input frame (${newFrame.displayHeight})`,+ `Returned VideoFrame of track ${track.trackId} has different displayWidth (${userProcessedFrame.displayWidth}) than the input frame (${userProcessedFrame.displayHeight})`,);}- if (newFrame.displayHeight !== frame.displayHeight) {+ if (userProcessedFrame.displayHeight !== rotated.displayHeight) {throw new Error(- `Returned VideoFrame of track ${track.trackId} has different displayHeight (${newFrame.displayHeight}) than the input frame (${newFrame.displayHeight})`,+ `Returned VideoFrame of track ${track.trackId} has different displayHeight (${userProcessedFrame.displayHeight}) than the input frame (${userProcessedFrame.displayHeight})`,);}- if (newFrame.timestamp !== frame.timestamp) {+ if (userProcessedFrame.timestamp !== rotated.timestamp) {throw new Error(- `Returned VideoFrame of track ${track.trackId} has different timestamp (${newFrame.timestamp}) than the input frame (${newFrame.timestamp}). When calling new VideoFrame(), pass {timestamp: frame.timestamp} as second argument`,+ `Returned VideoFrame of track ${track.trackId} has different timestamp (${userProcessedFrame.timestamp}) than the input frame (${userProcessedFrame.timestamp}). When calling new VideoFrame(), pass {timestamp: frame.timestamp} as second argument`,);}- if (newFrame.duration !== frame.duration) {+ if (userProcessedFrame.duration !== rotated.duration) {throw new Error(- `Returned VideoFrame of track ${track.trackId} has different duration (${newFrame.duration}) than the input frame (${newFrame.duration}). When calling new VideoFrame(), pass {duration: frame.duration} as second argument`,+ `Returned VideoFrame of track ${track.trackId} has different duration (${userProcessedFrame.duration}) than the input frame (${userProcessedFrame.duration}). When calling new VideoFrame(), pass {duration: frame.duration} as second argument`,);}const fixedFrame = convertToCorrectVideoFrame({- videoFrame: newFrame,+ videoFrame: userProcessedFrame,outputCodec,});await videoEncoder.encodeFrame(fixedFrame, fixedFrame.timestamp);fixedFrame.close();- if (frame !== newFrame) {- frame.close();+ if (rotated !== userProcessedFrame) {+ rotated.close();}- if (fixedFrame !== newFrame) {+ if (fixedFrame !== userProcessedFrame) {fixedFrame.close();}};diff --git a/packages/webcodecs/src/on-video-track.ts b/packages/webcodecs/src/on-video-track.tsindex 5e8cbd64cf..971cc9c1dd 100644--- a/packages/webcodecs/src/on-video-track.ts+++ b/packages/webcodecs/src/on-video-track.ts@@ -9,6 +9,10 @@ import type {ConvertMediaVideoCodec} from './get-available-video-codecs';import {Log} from './log';import {onFrame} from './on-frame';import type {ConvertMediaOnVideoTrackHandler} from './on-video-track-handler';+import {+ calculateNewDimensionsFromDimensions,+ HARDCODED_ROTATE,+} from './rotation';import type {ConvertMediaProgressFn} from './throttled-state-update';import {createVideoDecoder} from './video-decoder';import {getVideoDecoderConfigWithHardwareAcceleration} from './video-decoder-config';@@ -91,10 +95,17 @@ export const makeVideoTrackHandler =};}+ const {height: newHeight, width: newWidth} =+ calculateNewDimensionsFromDimensions({+ width: track.codedWidth,+ height: track.codedHeight,+ rotation: HARDCODED_ROTATE,+ });+const videoEncoderConfig = await getVideoEncoderConfig({codec: videoOperation.videoCodec,- height: track.displayAspectHeight,- width: track.displayAspectWidth,+ height: newHeight,+ width: newWidth,fps: track.fps,});const videoDecoderConfig =@@ -121,8 +132,8 @@ export const makeVideoTrackHandler =const {trackNumber} = await state.addTrack({type: 'video',color: track.color,- width: track.codedWidth,- height: track.codedHeight,+ width: newWidth,+ height: newHeight,codec: videoOperation.videoCodec,codecPrivate: null,timescale: track.timescale,diff --git a/packages/webcodecs/src/rotate-video.ts b/packages/webcodecs/src/rotate-video.tsnew file mode 100644index 0000000000..8210cadea4--- /dev/null+++ b/packages/webcodecs/src/rotate-video.ts@@ -0,0 +1,52 @@+import {calculateNewDimensionsFromDimensions} from './rotation';++export const rotateVideo = ({+ frame,+ rotation,+}: {+ frame: VideoFrame;+ rotation: number;+}) => {+ if (rotation % 360 === 0) {+ return frame;+ }++ if (rotation % 90 !== 0) {+ throw new Error('Only 90 degree rotations are supported');+ }++ const normalized = ((rotation % 360) + 360) % 360;++ const {height, width} = calculateNewDimensionsFromDimensions({+ height: frame.displayHeight,+ width: frame.displayWidth,+ rotation,+ });++ 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 (normalized === 90) {+ ctx.translate(width, 0);+ } else if (normalized === 180) {+ ctx.translate(width, height);+ } else if (normalized === 270) {+ ctx.translate(0, height);+ }++ ctx.rotate(normalized * (Math.PI / 180));+ ctx.drawImage(frame, 0, 0);++ return new VideoFrame(canvas, {+ displayHeight: height,+ displayWidth: width,+ duration: frame.duration as number,+ timestamp: frame.timestamp,+ });+};diff --git a/packages/webcodecs/src/rotation.ts b/packages/webcodecs/src/rotation.tsnew file mode 100644index 0000000000..9fee095f48--- /dev/null+++ b/packages/webcodecs/src/rotation.ts@@ -0,0 +1,18 @@+export const HARDCODED_ROTATE = -270;++export const calculateNewDimensionsFromDimensions = ({+ width,+ height,+ rotation,+}: {+ width: number;+ height: number;+ rotation: number;+}) => {+ const switchDimensions = rotation % 90 === 0 && rotation % 180 !== 0;++ const newHeight = switchDimensions ? width : height;+ const newWidth = switchDimensions ? height : width;++ return {height: newHeight, width: newWidth};+};