packages/docs/docs/webcodecs/rotate-and-resize-video-frame.mdx
:::warning We are phasing out Remotion WebCodecs and are moving to Mediabunny! :::
Part of the @remotion/webcodecs package.
import {LicenseDisclaimer} from './LicenseDisclaimer';
<details> <summary>💼 Important License Disclaimer</summary> <LicenseDisclaimer /> </details>Resizes and/or rotates a VideoFrame object.
Returns a new VideoFrame object with the applied transformations, or the original frame if no transformations are applied.
import {rotateAndResizeVideoFrame} from '@remotion/webcodecs';
// Assume you have a VideoFrame object
declare const frame: VideoFrame;
const rotatedFrame = rotateAndResizeVideoFrame({
frame,
rotation: 90,
resizeOperation: null,
});
console.log('Original dimensions:', frame.displayWidth, 'x', frame.displayHeight);
console.log('Rotated dimensions:', rotatedFrame.displayWidth, 'x', rotatedFrame.displayHeight);
import {rotateAndResizeVideoFrame} from '@remotion/webcodecs';
// Assume you have a VideoFrame object
declare const frame: VideoFrame;
const resizedFrame = rotateAndResizeVideoFrame({
frame,
rotation: 0,
resizeOperation: {
mode: 'width',
width: 640,
},
});
console.log('Resized frame width:', resizedFrame.displayWidth);
import {rotateAndResizeVideoFrame} from '@remotion/webcodecs';
// Assume you have a VideoFrame object
declare const frame: VideoFrame;
const transformedFrame = rotateAndResizeVideoFrame({
frame,
rotation: 180,
resizeOperation: {
mode: 'height',
height: 480,
},
needsToBeMultipleOfTwo: true,
});
frameA VideoFrame object to be transformed.
rotationThe rotation angle in degrees. Only multiples of 90 degrees are supported (0, 90, 180, 270, etc.).
resizeOperationA resize operation to apply to the frame, or null if no resizing is needed.
See: Resize modes for available options.
needsToBeMultipleOfTwo?Whether the resulting dimensions should be multiples of 2. Default: false.
This is often required if encoding to a codec like H.264.
If true, the dimensions will be rounded down to the nearest even number.
The function returns the original frame unchanged in these cases:
Otherwise, it returns a new VideoFrame object:
Additional behavior notes:
VideoFrame using an OffscreenCanvas for the transformationImportant: You are responsible for closing VideoFrame objects to prevent memory leaks. Since this function may return either the original frame or a new frame, you should check if a new frame was created before closing the original:
import {rotateAndResizeVideoFrame} from '@remotion/webcodecs';
// Assume you have a VideoFrame object
declare const originalFrame: VideoFrame;
const transformedFrame = rotateAndResizeVideoFrame({
frame: originalFrame,
rotation: 90,
resizeOperation: null,
});
// Only close the original frame if a new one was created
if (transformedFrame !== originalFrame) {
originalFrame.close();
}
// Remember to also close the transformed frame when you're done with it
// transformedFrame.close();