rotateAndResizeVideoFrame()v4.0.316
Part of the @remotion/webcodecs
package.
💼 Important License Disclaimer
We consider a team of 4 or more people a "company".
In a future version of
@remotion/webcodecs
, this package will also require the purchase of a newly created "WebCodecs Conversion Seat". Get in touch with us if you are planning to use this package.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.
Rotating a video frame by 90 degreestsx
import {rotateAndResizeVideoFrame } from '@remotion/webcodecs';Â// Assume you have a VideoFrame objectdeclare constframe :VideoFrame ;ÂconstrotatedFrame =rotateAndResizeVideoFrame ({frame ,rotation : 90,resizeOperation : null,});Âconsole .log ('Original dimensions:',frame .displayWidth , 'x',frame .displayHeight );console .log ('Rotated dimensions:',rotatedFrame .displayWidth , 'x',rotatedFrame .displayHeight );
Resizing a video frame by widthtsx
import {rotateAndResizeVideoFrame } from '@remotion/webcodecs';Â// Assume you have a VideoFrame objectdeclare constframe :VideoFrame ;ÂconstresizedFrame =rotateAndResizeVideoFrame ({frame ,rotation : 0,resizeOperation : {mode : 'width',width : 640,},});Âconsole .log ('Resized frame width:',resizedFrame .displayWidth );
Rotating and resizing togethertsx
import {rotateAndResizeVideoFrame } from '@remotion/webcodecs';Â// Assume you have a VideoFrame objectdeclare constframe :VideoFrame ;ÂconsttransformedFrame =rotateAndResizeVideoFrame ({frame ,rotation : 180,resizeOperation : {mode : 'height',height : 480,},needsToBeMultipleOfTwo : true,});
API​
frame
​
A VideoFrame
object to be transformed.
rotation
​
The rotation angle in degrees. Only multiples of 90 degrees are supported (0, 90, 180, 270, etc.).
resizeOperation
​
A resize operation to apply to the frame, or null
if no resizing is needed.
See: Resize modes for available options.
needsToBeMultipleOfTwo?
​
optional, default: false
Whether the resulting dimensions should be multiples of 2.
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.
Behavior​
The function returns the original frame unchanged in these cases:
- No rotation (0°) and no resize operation is specified
- No rotation (0°) and resize operation results in the same dimensions
Otherwise, it returns a new VideoFrame
object:
- When rotation is applied (90°, 180°, 270°, etc.)
- When resizing changes the dimensions
- When both rotation and resizing are applied
Additional behavior notes:
- Rotation is applied first, then resizing
- For 90° and 270° rotations, the width and height are swapped
- The function creates a new
VideoFrame
using anOffscreenCanvas
for the transformation
Memory Management​
Important: 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:
Proper memory cleanuptsx
import {rotateAndResizeVideoFrame } from '@remotion/webcodecs';Â// Assume you have a VideoFrame objectdeclare constoriginalFrame :VideoFrame ;ÂconsttransformedFrame =rotateAndResizeVideoFrame ({frame :originalFrame ,rotation : 90,resizeOperation : null,});Â// Only close the original frame if a new one was createdif (transformedFrame !==originalFrame ) {originalFrame .close ();}Â// Remember to also close the transformed frame when you're done with it// transformedFrame.close();