Skip to content

🎥 Make videos programmatically with React

alright

JonnyBurger5185a399ff7d
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsx
index 8dada1ca30..c868aa3fa5 100644
--- a/packages/convert/app/components/ConvertUi.tsx
+++ b/packages/convert/app/components/ConvertUi.tsx
@@ -11,6 +11,7 @@ import type {
} from 'mediabunny';
import {Conversion, Output, StreamTarget} from 'mediabunny';
import React, {useCallback, useMemo, useState} from 'react';
+import {applyCrop} from '~/lib/apply-crop';
import type {Dimensions} from '~/lib/calculate-new-dimensions-from-dimensions';
import {calculateNewDimensionsFromRotateAndScale} from '~/lib/calculate-new-dimensions-from-dimensions';
import {canRotateOrMirror} from '~/lib/can-rotate-or-mirror';
@@ -63,7 +64,6 @@ const ConvertUI = ({
setFlipHorizontal,
setFlipVertical,
inputContainer,
- unrotatedDimensions,
videoThumbnailRef,
rotation,
dimensions,
@@ -80,7 +80,6 @@ const ConvertUI = ({
readonly currentVideoCodec: InputVideoTrack['codec'] | null;
readonly tracks: InputTrack[] | null;
readonly videoThumbnailRef: React.RefObject<VideoThumbnailRef | null>;
- readonly unrotatedDimensions: Dimensions | null;
readonly dimensions: Dimensions | null | undefined;
readonly durationInSeconds: number | null;
readonly rotation: number | null;
@@ -454,6 +453,14 @@ const ConvertUI = ({
return (tracks?.filter((t) => t.isVideoTrack()).length ?? 0) === 0;
}, [tracks]);
+ const unrotatedDimensions = useMemo(() => {
+ if (!dimensions) {
+ return null;
+ }
+
+ return applyCrop(dimensions, cropRect);
+ }, [dimensions, cropRect]);
+
const newDimensions = useMemo(() => {
if (unrotatedDimensions === null) {
return null;
@@ -692,7 +699,9 @@ const ConvertUI = ({
</ConvertUiSection>
{resizeOperation !== null &&
newDimensions !== null &&
- unrotatedDimensions !== null ? (
+ unrotatedDimensions !== null &&
+ dimensions !== null &&
+ dimensions !== undefined ? (
<>
<div className="h-2" />
<ResizeUi
diff --git a/packages/convert/app/components/FileAvailable.tsx b/packages/convert/app/components/FileAvailable.tsx
index d5657394c1..3db3ef9a1d 100644
--- a/packages/convert/app/components/FileAvailable.tsx
+++ b/packages/convert/app/components/FileAvailable.tsx
@@ -130,7 +130,6 @@ export const FileAvailable: React.FC<{
currentVideoCodec={probeResult.videoCodec ?? null}
tracks={probeResult.tracks}
setSrc={setSrc}
- unrotatedDimensions={probeResult.unrotatedDimensions}
dimensions={probeResult.dimensions}
durationInSeconds={probeResult.durationInSeconds ?? null}
action={routeAction}
diff --git a/packages/convert/app/components/crop-ui/ResizeHandle.tsx b/packages/convert/app/components/crop-ui/ResizeHandle.tsx
index 0b3778c411..7d02f152a4 100644
--- a/packages/convert/app/components/crop-ui/ResizeHandle.tsx
+++ b/packages/convert/app/components/crop-ui/ResizeHandle.tsx
@@ -111,7 +111,7 @@ export const ResizeHandle: React.FC<{
);
if (position === 'top-left') {
- updateRect((prev) => {
+ return updateRect((prev) => {
const x = Math.min(cropPixelsX, prev.width + prev.left - MIN_SIZE);
const y = Math.min(cropPixelsY, prev.height + prev.top - MIN_SIZE);
@@ -128,7 +128,7 @@ export const ResizeHandle: React.FC<{
}
if (position === 'top-right') {
- updateRect((prev) => {
+ return updateRect((prev) => {
const newHeight = prev.height - (cropPixelsY - prev.top);
const y = Math.min(cropPixelsY, prev.height + prev.top - MIN_SIZE);
@@ -142,7 +142,7 @@ export const ResizeHandle: React.FC<{
}
if (position === 'bottom-left') {
- updateRect((prev) => {
+ return updateRect((prev) => {
const x = Math.min(cropPixelsX, prev.width + prev.left - MIN_SIZE);
const diffX = x - prev.left;
@@ -162,6 +162,7 @@ export const ResizeHandle: React.FC<{
width: Math.max(MIN_SIZE, cropPixelsX - prev.left),
height: Math.max(MIN_SIZE, cropPixelsY - prev.top),
}));
+ return;
}
throw new Error('Unknown position: ' + JSON.stringify(position));
diff --git a/packages/convert/app/components/use-probe.ts b/packages/convert/app/components/use-probe.ts
index 2697ce091a..fc1c23a3d5 100644
--- a/packages/convert/app/components/use-probe.ts
+++ b/packages/convert/app/components/use-probe.ts
@@ -24,8 +24,6 @@ export const useProbe = ({src}: {src: Source}) => {
const [dimensions, setDimensions] = useState<Dimensions | undefined | null>(
undefined,
);
- const [unrotatedDimensions, setUnrotatedDimensions] =
- useState<Dimensions | null>(null);
const name = useMemo(
() => (src.type === 'url' ? src.url.split('/').pop()! : src.file.name),
[src],
@@ -76,10 +74,6 @@ export const useProbe = ({src}: {src: Source}) => {
height: track.displayHeight,
});
setRotation(track.rotation);
- setUnrotatedDimensions({
- width: track.codedWidth,
- height: track.codedHeight,
- });
track.hasHighDynamicRange().then((hdr) => setHdr(hdr));
} else if (track.isAudioTrack()) {
hasAudioTrack = true;
@@ -97,7 +91,6 @@ export const useProbe = ({src}: {src: Source}) => {
setVideoCodec(null);
setFps(null);
setDimensions(null);
- setUnrotatedDimensions(null);
setHdr(false);
}
};
@@ -138,7 +131,6 @@ export const useProbe = ({src}: {src: Source}) => {
error,
rotation,
metadata,
- unrotatedDimensions,
sampleRate,
input,
};
@@ -157,7 +149,6 @@ export const useProbe = ({src}: {src: Source}) => {
error,
rotation,
metadata,
- unrotatedDimensions,
sampleRate,
input,
]);
diff --git a/packages/convert/app/lib/apply-crop.ts b/packages/convert/app/lib/apply-crop.ts
new file mode 100644
index 0000000000..1632da4ec7
--- /dev/null
+++ b/packages/convert/app/lib/apply-crop.ts
@@ -0,0 +1,9 @@
+import type {CropRectangle} from 'mediabunny';
+import type {Dimensions} from './calculate-new-dimensions-from-dimensions';
+
+export const applyCrop = (dimensions: Dimensions, cropRect: CropRectangle) => {
+ return {
+ width: Math.min(dimensions.width, cropRect.width - cropRect.left),
+ height: Math.min(dimensions.height, cropRect.height - cropRect.top),
+ };
+};