rotateAndResizeVideoFrame()v4.0.316
属于 @remotion/webcodecs 软件包的一部分.
🌐 Part of the @remotion/webcodecs package.
💼 重要许可免责声明
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.调整和/或旋转 VideoFrame 对象。
返回应用了变换的新 VideoFrame 对象,如果没有应用任何变换,则返回原始帧。
🌐 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 degreesimport {rotateAndResizeVideoFrame } from '@remotion/webcodecs'; // Assume you have a VideoFrame object declare 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 widthimport {rotateAndResizeVideoFrame } from '@remotion/webcodecs'; // Assume you have a VideoFrame object declare constframe :VideoFrame ; constresizedFrame =rotateAndResizeVideoFrame ({frame ,rotation : 0,resizeOperation : {mode : 'width',width : 640, }, });console .log ('Resized frame width:',resizedFrame .displayWidth );
Rotating and resizing togetherimport {rotateAndResizeVideoFrame } from '@remotion/webcodecs'; // Assume you have a VideoFrame object declare constframe :VideoFrame ; consttransformedFrame =rotateAndResizeVideoFrame ({frame ,rotation : 180,resizeOperation : {mode : 'height',height : 480, },needsToBeMultipleOfTwo : true, });
应用编程接口
🌐 API
frame
要转换的VideoFrame对象。
🌐 A VideoFrame object to be transformed.
rotation
旋转角度,以度为单位。仅支持90度的倍数(0、90、180、270等)。
🌐 The rotation angle in degrees. Only multiples of 90 degrees are supported (0, 90, 180, 270, etc.).
resizeOperation
要对帧应用的调整大小操作,如果不需要调整大小则为 null。
参见:调整大小模式 了解可用选项。
🌐 A resize operation to apply to the frame, or null if no resizing is needed.
See: Resize modes for available options.
needsToBeMultipleOfTwo?
是否希望得到的尺寸是2的倍数。默认值:false。
如果编码成像 H.264 这样的编解码器,通常需要这样做。
如果为 true,尺寸将向下取整到最接近的偶数。
🌐 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.
行为
🌐 Behavior
在以下情况下,该函数返回原始帧不变:
🌐 The function returns the original frame unchanged in these cases:
- 未指定旋转(0°)和调整大小操作
- 不旋转(0°)且调整大小操作会得到相同的尺寸
否则,它会返回一个 新的 VideoFrame 对象:
🌐 Otherwise, it returns a new VideoFrame object:
- 当应用旋转(90°、180°、270°等)时
- 调整大小时更改尺寸
- 当同时应用旋转和调整大小时
附加行为备注:
🌐 Additional behavior notes:
- 先应用旋转,然后调整大小
- 对于90°和270°的旋转,宽度和高度会互换
- 该函数使用
OffscreenCanvas进行转换来创建一个新的VideoFrame
内存管理
🌐 Memory Management
重要:你有责任关闭 VideoFrame 对象以防止内存泄漏。由于此函数可能返回原始帧或新帧,因此在关闭原始帧之前,你应检查是否创建了新帧:
Proper memory cleanupimport {rotateAndResizeVideoFrame } from '@remotion/webcodecs'; // Assume you have a VideoFrame object declare constoriginalFrame :VideoFrame ; consttransformedFrame =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();
另请参阅
🌐 See also