Check for valid coordinates added, bugs fixed.

This commit is contained in:
nevolodia
2025-05-25 14:14:01 +02:00
parent 13a566566f
commit 2f104f5a1b
2 changed files with 47 additions and 1 deletions

View File

@@ -24,27 +24,60 @@ export default function CropVideo({ title }: ToolComponentProps) {
width: number; width: number;
height: number; height: number;
} | null>(null); } | null>(null);
const [processingError, setProcessingError] = useState<string>('');
useEffect(() => { useEffect(() => {
if (input) { if (input) {
getVideoDimensions(input) getVideoDimensions(input)
.then((dimensions) => { .then((dimensions) => {
setVideoDimensions(dimensions); setVideoDimensions(dimensions);
setProcessingError('');
}) })
.catch((error) => { .catch((error) => {
console.error('Error getting video dimensions:', error); console.error('Error getting video dimensions:', error);
setProcessingError('Failed to load video dimensions');
}); });
} else { } else {
setVideoDimensions(null); setVideoDimensions(null);
setProcessingError('');
} }
}, [input]); }, [input]);
const validateDimensions = (values: InitialValuesType): string => {
if (!videoDimensions) return '';
if (values.x < 0 || values.y < 0) {
return 'X and Y coordinates must be non-negative';
}
if (values.width <= 0 || values.height <= 0) {
return 'Width and height must be positive';
}
if (values.x + values.width > videoDimensions.width) {
return `Crop area extends beyond video width (${videoDimensions.width}px)`;
}
if (values.y + values.height > videoDimensions.height) {
return `Crop area extends beyond video height (${videoDimensions.height}px)`;
}
return '';
};
const compute = async ( const compute = async (
optionsValues: InitialValuesType, optionsValues: InitialValuesType,
input: File | null input: File | null
) => { ) => {
if (!input) return; if (!input) return;
const error = validateDimensions(optionsValues);
if (error) {
setProcessingError(error);
return;
}
setProcessingError('');
setLoading(true); setLoading(true);
try { try {
@@ -52,12 +85,16 @@ export default function CropVideo({ title }: ToolComponentProps) {
setResult(croppedFile); setResult(croppedFile);
} catch (error) { } catch (error) {
console.error('Error cropping video:', error); console.error('Error cropping video:', error);
setProcessingError(
'Error cropping video. Please check parameters and video file.'
);
} finally { } finally {
setLoading(false); setLoading(false);
} }
}; };
const debouncedCompute = useCallback(debounce(compute, 1000), [ // 2 seconds to avoid starting job half way through
const debouncedCompute = useCallback(debounce(compute, 2000), [
videoDimensions videoDimensions
]); ]);
@@ -86,6 +123,11 @@ export default function CropVideo({ title }: ToolComponentProps) {
title: 'Crop Coordinates', title: 'Crop Coordinates',
component: ( component: (
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}> <Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
{processingError && (
<Alert severity="error" sx={{ mb: 2 }}>
{processingError}
</Alert>
)}
<Box sx={{ display: 'flex', gap: 2 }}> <Box sx={{ display: 'flex', gap: 2 }}>
<TextField <TextField
label="X (left)" label="X (left)"

View File

@@ -45,6 +45,10 @@ export async function cropVideo(
const args = []; const args = [];
if (options.width <= 0 || options.height <= 0) {
throw new Error('Width and height must be positive');
}
args.push('-i', inputName); args.push('-i', inputName);
args.push( args.push(
'-vf', '-vf',