Skip to content

🎥 Make videos programmatically with React

alright

JonnyBurgerd32bda4ac362
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsx
index 58a5f5727e..e3d0fea9ef 100644
--- a/packages/convert/app/components/ConvertUi.tsx
+++ b/packages/convert/app/components/ConvertUi.tsx
@@ -275,6 +275,10 @@ const ConvertUI = ({
}
progress.hasVideo = true;
+
+ const dimensionsAfterCrop =
+ dimensions && crop ? applyCrop(dimensions, cropRect) : dimensions;
+
return {
height: Math.min(videoTrack.displayHeight, 1080),
process(sample) {
@@ -298,7 +302,7 @@ const ConvertUI = ({
forceTranscode: true,
...calculateMediabunnyResizeOption(
resizeOperation,
- dimensions ?? null,
+ dimensionsAfterCrop ?? null,
),
codec: operation.videoCodec,
};
@@ -469,8 +473,12 @@ const ConvertUI = ({
return null;
}
+ if (enableRotateOrMirror !== 'crop') {
+ return dimensions;
+ }
+
return applyCrop(dimensions, cropRect);
- }, [dimensions, cropRect]);
+ }, [dimensions, cropRect, enableRotateOrMirror]);
const newDimensions = useMemo(() => {
if (unrotatedDimensions === null) {
@@ -722,6 +730,9 @@ const ConvertUI = ({
rotation={userRotation - (rotation ?? 0)}
setResizeMode={setResizeOperation}
requireTwoStep={Boolean(isH264Reencode)}
+ crop={enableRotateOrMirror === 'crop'}
+ cropRect={cropRect}
+ dimensionsBeforeCrop={dimensions}
/>
</>
) : null}
diff --git a/packages/convert/app/components/ResizeThumbnail.tsx b/packages/convert/app/components/ResizeThumbnail.tsx
index 8699bb2eaa..59e1910582 100644
--- a/packages/convert/app/components/ResizeThumbnail.tsx
+++ b/packages/convert/app/components/ResizeThumbnail.tsx
@@ -1,3 +1,4 @@
+import type {CropRectangle} from 'mediabunny';
import React, {useCallback, useMemo, useRef, useState} from 'react';
import type {Dimensions} from '~/lib/calculate-new-dimensions-from-dimensions';
import type {MediabunnyResize} from '~/lib/mediabunny-calculate-resize-option';
@@ -28,6 +29,7 @@ export const getThumbnailDimensions = (dimensions: Dimensions) => {
export const ResizeThumbnail: React.FC<{
readonly dimensions: Dimensions;
readonly unrotatedDimensions: Dimensions;
+ readonly dimensionsBeforeCrop: Dimensions;
readonly thumbnailRef: React.RefObject<VideoThumbnailRef | null>;
readonly rotation: number;
readonly scale: number;
@@ -35,6 +37,8 @@ export const ResizeThumbnail: React.FC<{
React.SetStateAction<MediabunnyResize | null>
>;
readonly inputFocused: boolean;
+ readonly cropRect: CropRectangle;
+ readonly crop: boolean;
}> = ({
thumbnailRef,
dimensions,
@@ -43,6 +47,9 @@ export const ResizeThumbnail: React.FC<{
rotation,
unrotatedDimensions,
inputFocused,
+ cropRect,
+ crop,
+ dimensionsBeforeCrop,
}) => {
const ref = useRef<HTMLCanvasElement>(null);
const thumbnailDimensions = useMemo(() => {
@@ -70,12 +77,15 @@ export const ResizeThumbnail: React.FC<{
setDragging(true);
}, []);
- const animate = !dragging && !inputFocused;
+ const animate = !dragging && !inputFocused && !crop;
const drawn = useThumbnailCopy({
sourceRef: thumbnailRef,
targetRef: ref,
dimensions: unrotatedThumbnailDimensions,
+ cropRect,
+ crop,
+ fullDimensionsBeforeCrop: dimensionsBeforeCrop,
});
return (
diff --git a/packages/convert/app/components/ResizeUi.tsx b/packages/convert/app/components/ResizeUi.tsx
index ef94f96ef7..e0814e469a 100644
--- a/packages/convert/app/components/ResizeUi.tsx
+++ b/packages/convert/app/components/ResizeUi.tsx
@@ -1,3 +1,4 @@
+import type {CropRectangle} from 'mediabunny';
import React, {useCallback, useMemo} from 'react';
import type {Dimensions} from '~/lib/calculate-new-dimensions-from-dimensions';
import type {MediabunnyResize} from '~/lib/mediabunny-calculate-resize-option';
@@ -38,12 +39,15 @@ const NumberInput: React.FC<{
export const ResizeUi: React.FC<{
readonly dimensions: Dimensions;
readonly originalDimensions: Dimensions;
+ readonly dimensionsBeforeCrop: Dimensions;
readonly thumbnailRef: React.RefObject<VideoThumbnailRef | null>;
readonly rotation: number;
readonly setResizeMode: React.Dispatch<
React.SetStateAction<MediabunnyResize | null>
>;
readonly requireTwoStep: boolean;
+ readonly cropRect: CropRectangle;
+ readonly crop: boolean;
}> = ({
dimensions,
thumbnailRef,
@@ -51,6 +55,9 @@ export const ResizeUi: React.FC<{
rotation,
setResizeMode,
requireTwoStep,
+ cropRect,
+ crop,
+ dimensionsBeforeCrop,
}) => {
const outer: React.CSSProperties = useMemo(() => {
return {...getThumbnailDimensions(dimensions), outlineStyle: 'solid'};
@@ -109,7 +116,7 @@ export const ResizeUi: React.FC<{
<div className="flex-1" />
<div
style={outer}
- className="rounded bg-white transition-all flex justify-center items-center outline-2 outline-slate-300"
+ className="rounded bg-white flex justify-center items-center outline-2 outline-slate-300"
>
<ResizeThumbnail
dimensions={rotatedDimensions}
@@ -119,6 +126,9 @@ export const ResizeUi: React.FC<{
setResizeMode={setResizeMode}
unrotatedDimensions={originalDimensions}
inputFocused={widthFocused || heightFocused}
+ cropRect={cropRect}
+ crop={crop}
+ dimensionsBeforeCrop={dimensionsBeforeCrop}
/>
</div>
<div className="flex-1 flex flex-row items-center ml-[2px]">
diff --git a/packages/convert/app/lib/use-thumbnail-copy.ts b/packages/convert/app/lib/use-thumbnail-copy.ts
index ab474f3d3b..19ebf4d719 100644
--- a/packages/convert/app/lib/use-thumbnail-copy.ts
+++ b/packages/convert/app/lib/use-thumbnail-copy.ts
@@ -1,3 +1,4 @@
+import type {CropRectangle} from 'mediabunny';
import {useCallback, useEffect, useState} from 'react';
import type {VideoThumbnailRef} from '~/components/VideoThumbnail';
import type {Dimensions} from './calculate-new-dimensions-from-dimensions';
@@ -6,10 +7,16 @@ export const useThumbnailCopy = ({
sourceRef,
targetRef,
dimensions,
+ cropRect,
+ crop,
+ fullDimensionsBeforeCrop,
}: {
sourceRef: React.RefObject<VideoThumbnailRef | null>;
targetRef: React.RefObject<HTMLCanvasElement | null> | null;
dimensions: Dimensions;
+ cropRect: CropRectangle;
+ crop: boolean;
+ fullDimensionsBeforeCrop: Dimensions;
}) => {
const [drawn, setDrawn] = useState(
() => sourceRef.current?.hasBitmap ?? false,
@@ -26,11 +33,32 @@ export const useThumbnailCopy = ({
return;
}
- ctx.drawImage(map, 0, 0, dimensions.width, dimensions.height);
+ if (!crop) {
+ ctx.drawImage(map, 0, 0, dimensions.width, dimensions.height);
+ } else {
+ ctx.drawImage(
+ map,
+ (map.width / fullDimensionsBeforeCrop.width) * cropRect.left,
+ (map.height / fullDimensionsBeforeCrop.height) * cropRect.top,
+ (map.width / fullDimensionsBeforeCrop.width) * cropRect.width,
+ (map.height / fullDimensionsBeforeCrop.height) * cropRect.height,
+ 0,
+ 0,
+ dimensions.width,
+ dimensions.height,
+ );
+ }
setDrawn(true);
});
- }, [sourceRef, targetRef, dimensions.width, dimensions.height]);
+ }, [
+ targetRef,
+ sourceRef,
+ crop,
+ dimensions,
+ fullDimensionsBeforeCrop,
+ cropRect,
+ ]);
useEffect(() => {
const {current} = sourceRef;