Skip to content

🎥 Make videos programmatically with React

remotion.dev/convert: Ensure that crop is divisible by 2

Jonny Burger0867ef9f940a
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsx
index 4554e491d8..1700b43ec1 100644
--- a/packages/convert/app/components/ConvertUi.tsx
+++ b/packages/convert/app/components/ConvertUi.tsx
@@ -43,6 +43,7 @@ import {ConvertProgress, convertProgressRef} from './ConvertProgress';
import {ConvertUiSection} from './ConvertUiSection';
import {ErrorState} from './ErrorState';
import {flipVideoFrame} from './flip-video';
+import {makeCrop} from './make-crop';
import {MirrorComponents} from './MirrorComponents';
import {ResampleUi} from './ResampleUi';
import {ResizeUi} from './ResizeUi';
@@ -296,16 +297,11 @@ const ConvertUI = ({
return flipped;
},
crop: crop
- ? {
- width: Number.isFinite(cropRect.width)
- ? cropRect.width
- : dimensions!.width - cropRect.left,
- height: Number.isFinite(cropRect.height)
- ? cropRect.height
- : dimensions!.height - cropRect.top,
- left: cropRect.left,
- top: cropRect.top,
- }
+ ? makeCrop({
+ cropRect,
+ dimensions: dimensions!,
+ videoCodec: operation.videoCodec,
+ })
: undefined,
rotate: userRotation as Rotation,
forceTranscode: true,
diff --git a/packages/convert/app/components/Probe.tsx b/packages/convert/app/components/Probe.tsx
index e35e83b872..a9584f0b78 100644
--- a/packages/convert/app/components/Probe.tsx
+++ b/packages/convert/app/components/Probe.tsx
@@ -62,6 +62,8 @@ export const Probe: React.FC<{
bestBrightness.current = differenceToIdeal;
videoThumbnailRef.current?.draw(frame);
}
+
+ frame.close();
},
[videoThumbnailRef],
);
diff --git a/packages/convert/app/components/make-crop.tsx b/packages/convert/app/components/make-crop.tsx
new file mode 100644
index 0000000000..b7e7a02093
--- /dev/null
+++ b/packages/convert/app/components/make-crop.tsx
@@ -0,0 +1,32 @@
+import type {CropRectangle, VideoCodec} from 'mediabunny';
+import type {Dimensions} from '~/lib/calculate-new-dimensions-from-dimensions';
+
+export const makeCrop = ({
+ cropRect,
+ dimensions,
+ videoCodec,
+}: {
+ cropRect: CropRectangle;
+ dimensions: Dimensions;
+ videoCodec: VideoCodec;
+}) => {
+ let width = Number.isFinite(cropRect.width)
+ ? cropRect.width
+ : dimensions!.width - cropRect.left;
+ let height = Number.isFinite(cropRect.height)
+ ? cropRect.height
+ : dimensions!.height - cropRect.top;
+
+ // Round down to nearest even number if codec is avc or hevc
+ if (videoCodec === 'avc' || videoCodec === 'hevc') {
+ width = Math.floor(width / 2) * 2;
+ height = Math.floor(height / 2) * 2;
+ }
+
+ return {
+ width,
+ height,
+ left: cropRect.left,
+ top: cropRect.top,
+ };
+};
diff --git a/packages/convert/app/lib/use-thumbnail.ts b/packages/convert/app/lib/use-thumbnail.ts
index 6e1db276aa..3ebc3aaf4f 100644
--- a/packages/convert/app/lib/use-thumbnail.ts
+++ b/packages/convert/app/lib/use-thumbnail.ts
@@ -38,6 +38,7 @@ export const useThumbnailAndWaveform = ({
for await (const sample of iterator) {
samples++;
onVideoThumbnail(sample.toVideoFrame());
+ sample.close();
if (samples === 60) {
iterator.return().catch(() => undefined);