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 degreestsximport {rotateAndResizeVideoFrame } from'@remotion/webcodecs';// Assume you have a VideoFrame objectdeclareconstframe :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 widthtsximport {rotateAndResizeVideoFrame } from'@remotion/webcodecs';// Assume you have a VideoFrame objectdeclareconstframe :VideoFrame ;constresizedFrame =rotateAndResizeVideoFrame ({frame ,rotation : 0,resizeOperation : {, width : 640,},});console .log ('Resized frame width:',resizedFrame .displayWidth );
Rotating and resizing togethertsximport {rotateAndResizeVideoFrame } from'@remotion/webcodecs';// Assume you have a VideoFrame objectdeclareconstframe :VideoFrame ;consttransformedFrame =rotateAndResizeVideoFrame ({frame ,rotation : 180,resizeOperation : {, 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
VideoFrameusing anOffscreenCanvasfor 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 cleanuptsximport {rotateAndResizeVideoFrame } from'@remotion/webcodecs';// Assume you have a VideoFrame objectdeclareconstoriginalFrame :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();