Skip to content

🎥 Make videos programmatically with React

nice, cropping works well

JonnyBurger6f37cce3b7b2
diff --git a/bun.lock b/bun.lock
index a152136740..b556de02e8 100644
--- a/bun.lock
+++ b/bun.lock
@@ -244,6 +244,7 @@
"@remix-run/react": "^2.15.2",
"@remix-run/serve": "^2.15.2",
"@remotion/captions": "workspace:*",
+ "@remotion/paths": "workspace:*",
"@remotion/shapes": "workspace:*",
"@remotion/whisper-web": "workspace:*",
"@vercel/remix": "^2.15.2",
@@ -4260,7 +4261,7 @@
"bun": ["[email protected]", "", { "optionalDependencies": { "@oven/bun-darwin-aarch64": "1.3.0", "@oven/bun-darwin-x64": "1.3.0", "@oven/bun-darwin-x64-baseline": "1.3.0", "@oven/bun-linux-aarch64": "1.3.0", "@oven/bun-linux-aarch64-musl": "1.3.0", "@oven/bun-linux-x64": "1.3.0", "@oven/bun-linux-x64-baseline": "1.3.0", "@oven/bun-linux-x64-musl": "1.3.0", "@oven/bun-linux-x64-musl-baseline": "1.3.0", "@oven/bun-windows-x64": "1.3.0", "@oven/bun-windows-x64-baseline": "1.3.0" }, "os": [ "linux", "win32", "darwin", ], "cpu": [ "x64", "arm64", ], "bin": { "bun": "bin/bun.exe", "bunx": "bin/bunx.exe" } }, "sha512-YI7mFs7iWc/VsGsh2aw6eAPD2cjzn1j+LKdYVk09x1CrdTWKYIHyd+dG5iQoN9//3hCDoZj8U6vKpZzEf5UARA=="],
- "bun-plugin-tailwind": ["[email protected]", "", { "dependencies": { "tailwindcss": "4.0.0-beta.9" }, "peerDependencies": { "typescript": "^5.0.0" } }, "sha512-rTbzx+pK3F3cqgbtfbYibfWfKSWdcMNjdPTHgm+BbPhZWzMkBR6NpGqmKExJuIvdKRZgqoA0vd2/lke6rwZqMA=="],
+ "bun-plugin-tailwind": ["[email protected]", "", { "peerDependencies": { "typescript": "5.8.2" } }, "sha512-qtAXMNGG4R0UGGI8zWrqm2B7BdXqx48vunJXBPzfDOHPA5WkRUZdTSbE7TFwO4jLhYqSE23YMWsM9NhE6ovobw=="],
"bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-u8X0thhx+yJ0KmkxuEo9HAtdfgCBaM/aI9K90VQcQioAmkVp3SG3FkwWGibUFz3WdXAdcsqOcbU40lK7tbHdkQ=="],
@@ -8120,8 +8121,6 @@
"@remotion/media-parser/mediabunny": ["[email protected]", "", { "dependencies": { "@types/dom-mediacapture-transform": "^0.1.11", "@types/dom-webcodecs": "0.1.13" } }, "sha512-9fQfAYlnuVSHMfbJtKK0pAgDlfwGDoyLQZ7gbOCug9jHPfjdi0XYQwvi/kaOFFMD70ouPkXzvKRc3AMPwkSK6Q=="],
- "@remotion/promo-pages/bun-plugin-tailwind": ["[email protected]", "", { "peerDependencies": { "typescript": "5.8.2" } }, "sha512-qtAXMNGG4R0UGGI8zWrqm2B7BdXqx48vunJXBPzfDOHPA5WkRUZdTSbE7TFwO4jLhYqSE23YMWsM9NhE6ovobw=="],
-
"@remotion/promo-pages/hls.js": ["[email protected]", "", {}, "sha512-C020dKWEJcyvLnrqsFKW4q6D/6IEzKWdhktIS5bgoyEFE8lHgrFBq4RIngdy113abJOlIruhv8qjg7UX8hwxOw=="],
"@remotion/promo-pages/tailwindcss": ["[email protected]", "", {}, "sha512-QNbdmeS979Efzim2g/bEvfuh+fTcIdp1y7gA+sb6OYSW74rt7Cr7M78AKdf6HqWT3d5AiTb7SwTT3sLQxr4/qw=="],
@@ -8434,8 +8433,6 @@
"browserify-zlib/pako": ["[email protected]", "", {}, "sha512-NUcwaKxUxWrZLpDG+z/xZaCgQITkA/Dv4V/T6bw7VON6l1Xz/VnrBqrYjZQ12TamKHzITTfOEIYUj48y2KXImA=="],
- "bun-plugin-tailwind/tailwindcss": ["[email protected]", "", {}, "sha512-96KpsfQi+/sFIOfyFnGzyy5pobuzf1iMBD9NVtelerPM/lPI2XUS4Kikw9yuKRniXXw77ov1sl7gCSKLsn6CJA=="],
-
"cacache/fs-minipass": ["[email protected]", "", { "dependencies": { "minipass": "7.1.2" } }, "sha512-XUBA9XClHbnJWSfBzjkm6RvPsyg3sryZt06BEQoXcF7EK/xpGaQYJgQKDJSUH5SGZ76Y7pFx1QBnXz09rU5Fbw=="],
"cacache/glob": ["[email protected]", "", { "dependencies": { "foreground-child": "3.3.0", "jackspeak": "3.4.3", "minimatch": "9.0.5", "minipass": "7.1.2", "package-json-from-dist": "1.0.0", "path-scurry": "1.11.1" }, "bin": { "glob": "dist/esm/bin.mjs" } }, "sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg=="],
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsx
index 63d3394f20..8dada1ca30 100644
--- a/packages/convert/app/components/ConvertUi.tsx
+++ b/packages/convert/app/components/ConvertUi.tsx
@@ -1,6 +1,7 @@
import {Button} from '@/components/ui/button';
import type MediaFox from '@mediafox/core';
import type {
+ CropRectangle,
Input,
InputAudioTrack,
InputFormat,
@@ -70,6 +71,9 @@ const ConvertUI = ({
name,
input,
mediafox,
+ crop,
+ setCrop,
+ cropRect,
}: {
readonly setSrc: React.Dispatch<React.SetStateAction<Source | null>>;
readonly currentAudioCodec: InputAudioTrack['codec'] | null;
@@ -94,8 +98,11 @@ const ConvertUI = ({
readonly flipVertical: boolean;
readonly setFlipHorizontal: React.Dispatch<React.SetStateAction<boolean>>;
readonly setFlipVertical: React.Dispatch<React.SetStateAction<boolean>>;
+ readonly crop: boolean;
+ readonly setCrop: React.Dispatch<React.SetStateAction<boolean>>;
readonly sampleRate: number | null;
readonly mediafox: MediaFox;
+ readonly cropRect: CropRectangle;
}) => {
const [outputContainer, setOutputContainer] = useState<OutputContainer>(() =>
getDefaultOutputFormat(inputContainer),
@@ -286,6 +293,7 @@ const ConvertUI = ({
videoFrames++;
return flipped;
},
+ crop: crop ? cropRect : undefined,
rotate: userRotation as Rotation,
forceTranscode: true,
...calculateMediabunnyResizeOption(
@@ -404,6 +412,8 @@ const ConvertUI = ({
supportedConfigs,
userRotation,
videoOperationSelection,
+ crop,
+ cropRect,
]);
const dimissError = useCallback(() => {
@@ -657,6 +667,16 @@ const ConvertUI = ({
);
}
+ if (section === 'crop') {
+ return (
+ <div key="crop">
+ <ConvertUiSection active={crop} setActive={setCrop}>
+ Crop
+ </ConvertUiSection>
+ </div>
+ );
+ }
+
if (section === 'resize') {
if (inputIsAudioExclusively) {
return null;
diff --git a/packages/convert/app/components/FileAvailable.tsx b/packages/convert/app/components/FileAvailable.tsx
index 35ee63edd4..7af27b881f 100644
--- a/packages/convert/app/components/FileAvailable.tsx
+++ b/packages/convert/app/components/FileAvailable.tsx
@@ -1,4 +1,5 @@
import {MediaFox} from '@mediafox/core';
+import type {CropRectangle} from 'mediabunny';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {normalizeVideoRotation} from '~/lib/calculate-new-dimensions-from-dimensions';
import type {Source} from '~/lib/convert-state';
@@ -47,6 +48,15 @@ export const FileAvailable: React.FC<{
const [userRotation, setRotation] = useState(90);
const [flipHorizontal, setFlipHorizontal] = useState(true);
const [flipVertical, setFlipVertical] = useState(false);
+ const [cropOperation, setCropOperation] = useState<CropRectangle>(() => {
+ return {
+ left: 0,
+ top: 0,
+ width: Infinity,
+ height: Infinity,
+ };
+ });
+ const [crop, setCrop] = useState<boolean>(false);
const [waveform, setWaveform] = useState<number[]>([]);
@@ -74,6 +84,9 @@ export const FileAvailable: React.FC<{
isAudio={isAudio}
waveform={waveform}
mediaFox={mediaFox}
+ crop={crop}
+ setUnclampedRect={setCropOperation}
+ unclampedRect={cropOperation}
/>
) : null}
<div className="h-8" />
@@ -107,6 +120,8 @@ export const FileAvailable: React.FC<{
mediaFox ? (
<div className="gap-4">
<ConvertUI
+ crop={crop}
+ setCrop={setCrop}
mediafox={mediaFox}
inputContainer={probeResult.container}
currentAudioCodec={probeResult.audioCodec ?? null}
@@ -130,6 +145,7 @@ export const FileAvailable: React.FC<{
sampleRate={probeResult.sampleRate}
name={probeResult.name}
input={probeResult.input}
+ cropRect={cropOperation}
/>
</div>
) : null}
diff --git a/packages/convert/app/components/MediaPlayer.tsx b/packages/convert/app/components/MediaPlayer.tsx
index 29ceaa260a..0a614acea4 100644
--- a/packages/convert/app/components/MediaPlayer.tsx
+++ b/packages/convert/app/components/MediaPlayer.tsx
@@ -1,4 +1,5 @@
import type {MediaFox} from '@mediafox/core';
+import type {CropRectangle} from 'mediabunny';
import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {Source} from '~/lib/convert-state';
import {cn} from '~/lib/utils';
@@ -26,11 +27,19 @@ export function VideoPlayer({
isAudio,
waveform,
mediaFox,
+ crop,
+ setUnclampedRect,
+ unclampedRect,
}: {
readonly src: Source;
readonly waveform: number[];
readonly isAudio: boolean;
readonly mediaFox: MediaFox;
+ readonly crop: boolean;
+ readonly unclampedRect: CropRectangle;
+ readonly setUnclampedRect: React.Dispatch<
+ React.SetStateAction<CropRectangle>
+ >;
}) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const containerRef = useRef<HTMLDivElement>(null);
@@ -80,8 +89,6 @@ export function VideoPlayer({
}
}, [containerRef]);
- const [showCropUI, setShowCropUI] = useState(false);
-
return (
<div
ref={containerRef}
@@ -99,7 +106,7 @@ export function VideoPlayer({
className={cn(
'border-2',
'rounded-md',
- showCropUI ? 'border-transparent' : 'border-black',
+ crop ? 'border-transparent' : 'border-black',
'overflow-hidden',
)}
>
@@ -114,6 +121,14 @@ export function VideoPlayer({
className="group-fullscreen:flex-1"
/>
</div>
+ {crop ? (
+ <CropUI
+ setUnclampedRect={setUnclampedRect}
+ unclampedRect={unclampedRect}
+ mediaFox={mediaFox}
+ />
+ ) : null}
+
<div className="h-2" />
{mediaFox ? (
<div
@@ -144,13 +159,6 @@ export function VideoPlayer({
/>
</div>
) : null}
- {showCropUI ? <CropUI mediaFox={mediaFox} /> : null}
-
- <input
- type="checkbox"
- checked={showCropUI}
- onChange={() => setShowCropUI(!showCropUI)}
- />
</div>
);
}
diff --git a/packages/convert/app/components/crop-ui/Backdrop.tsx b/packages/convert/app/components/crop-ui/Backdrop.tsx
new file mode 100644
index 0000000000..e713c85e95
--- /dev/null
+++ b/packages/convert/app/components/crop-ui/Backdrop.tsx
@@ -0,0 +1,87 @@
+import {serializeInstructions, type ReducedInstruction} from '@remotion/paths';
+import type {CropRectangle} from 'mediabunny';
+import type {Dimensions} from '~/lib/calculate-new-dimensions-from-dimensions';
+
+export const CropBackdrop: React.FC<{
+ readonly rect: CropRectangle;
+ readonly dimensions: Dimensions;
+}> = ({rect, dimensions}) => {
+ const path1: ReducedInstruction[] = [
+ {
+ type: 'M',
+ x: 0,
+ y: 0,
+ },
+ {
+ type: 'L',
+ x: rect.left + rect.width,
+ y: 0,
+ },
+ {
+ type: 'L',
+ x: rect.left + rect.width,
+ y: rect.top,
+ },
+ {
+ type: 'L',
+ x: rect.left,
+ y: rect.top,
+ },
+ {
+ type: 'L',
+ x: rect.left,
+ y: dimensions.height,
+ },
+ {
+ type: 'L',
+ x: 0,
+ y: dimensions.height,
+ },
+ {
+ type: 'Z',
+ },
+ ];
+ const d1 = serializeInstructions(path1);
+
+ const path2: ReducedInstruction[] = [
+ {
+ type: 'M',
+ x: dimensions.width,
+ y: dimensions.height,
+ },
+ {
+ type: 'L',
+ x: dimensions.width,
+ y: 0,
+ },
+ {
+ type: 'L',
+ x: dimensions.width,
+ y: 0,
+ },
+ {
+ type: 'L',
+ x: rect.left + rect.width,
+ y: 0,
+ },
+ {
+ type: 'L',
+ x: rect.left + rect.width,
+ y: rect.top + rect.height,
+ },
+ {type: 'L', x: rect.left, y: rect.top + rect.height},
+ {
+ type: 'L',
+ x: rect.left,
+ y: dimensions.height,
+ },
+ {type: 'Z'},
+ ];
+ const d2 = serializeInstructions(path2);
+ return (
+ <svg viewBox={`0 0 ${dimensions.width} ${dimensions.height}`}>
+ <path d={d1} fill="black" strokeWidth={10} opacity={0.5} />
+ <path d={d2} fill="black" strokeWidth={10} opacity={0.5} />
+ </svg>
+ );
+};
diff --git a/packages/convert/app/components/crop-ui/CropUi.tsx b/packages/convert/app/components/crop-ui/CropUi.tsx
index 27a0d7d816..4f48a2f43e 100644
--- a/packages/convert/app/components/crop-ui/CropUi.tsx
+++ b/packages/convert/app/components/crop-ui/CropUi.tsx
@@ -2,24 +2,20 @@ import type {MediaFox, PlayerStateData} from '@mediafox/core';
import type {CropRectangle} from 'mediabunny';
import type React from 'react';
import {useEffect, useMemo, useRef, useState} from 'react';
+import {CropBackdrop} from './Backdrop';
import {ResizeHandle} from './ResizeHandle';
export const CropUI: React.FC<{
readonly mediaFox: MediaFox;
-}> = ({mediaFox}) => {
+ readonly unclampedRect: CropRectangle;
+ readonly setUnclampedRect: React.Dispatch<
+ React.SetStateAction<CropRectangle>
+ >;
+}> = ({mediaFox, unclampedRect, setUnclampedRect}) => {
const [state, setState] = useState<PlayerStateData | null>(() =>
mediaFox.getState(),
);
- const [unclampedRect, setUnclampedRect] = useState<CropRectangle>(() => {
- return {
- left: 0,
- top: 0,
- width: Infinity,
- height: Infinity,
- };
- });
-
const ref = useRef<HTMLDivElement>(null);
const dimensions = useMemo(() => {
@@ -88,6 +84,7 @@ export const CropUI: React.FC<{
aspectRatio: `${dimensions.width} / ${dimensions.height}`,
}}
>
+ <CropBackdrop rect={rect} dimensions={dimensions} />
<div
className="border-brand absolute border-4 rounded-md "
style={{
diff --git a/packages/convert/app/lib/config.ts b/packages/convert/app/lib/config.ts
index e6de54e256..3f1170e421 100644
--- a/packages/convert/app/lib/config.ts
+++ b/packages/convert/app/lib/config.ts
@@ -1,4 +1,3 @@
export const TEST_FAST = false;
-export const SAMPLE_FILE_CONVERT =
- 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4';
+export const SAMPLE_FILE_CONVERT = 'https://remotion.media/video-1m.mp4';
export const SAMPLE_FILE_TRANSCRIBE = 'https://remotion.media/dialogue.wav';
diff --git a/packages/convert/app/lib/default-ui.ts b/packages/convert/app/lib/default-ui.ts
index 1fdb9a8962..9958b8cd96 100644
--- a/packages/convert/app/lib/default-ui.ts
+++ b/packages/convert/app/lib/default-ui.ts
@@ -109,6 +109,7 @@ export type ConvertSections =
| 'rotate'
| 'mirror'
| 'resize'
+ | 'crop'
| 'resample';
export const getOrderOfSections = (
@@ -118,6 +119,7 @@ export const getOrderOfSections = (
return {
rotate: 0,
resize: 1,
+ crop: 2,
mirror: 2,
convert: 3,
resample: 4,
@@ -128,6 +130,7 @@ export const getOrderOfSections = (
return {
convert: 0,
resize: 1,
+ crop: 2,
rotate: 2,
mirror: 3,
resample: 4,
@@ -138,6 +141,7 @@ export const getOrderOfSections = (
return {
convert: 0,
resize: 1,
+ crop: 2,
rotate: 2,
mirror: 3,
resample: 4,
@@ -148,6 +152,7 @@ export const getOrderOfSections = (
return {
mirror: 0,
resize: 1,
+ crop: 2,
rotate: 2,
convert: 3,
resample: 4,
@@ -163,6 +168,7 @@ export const getOrderOfSections = (
return {
resize: 0,
rotate: 1,
+ crop: 2,
mirror: 2,
convert: 3,
resample: 4,
diff --git a/packages/convert/package.json b/packages/convert/package.json
index 272d06ae76..b3e37fe67c 100644
--- a/packages/convert/package.json
+++ b/packages/convert/package.json
@@ -29,6 +29,7 @@
"@remix-run/react": "^2.15.2",
"@remix-run/serve": "^2.15.2",
"@remotion/captions": "workspace:*",
+ "@remotion/paths": "workspace:*",
"@remotion/shapes": "workspace:*",
"@remotion/whisper-web": "workspace:*",
"@vercel/remix": "^2.15.2",