mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-12-02 01:44:03 +01:00
Check for valid coordinates added, bugs fixed.
This commit is contained in:
@@ -24,27 +24,60 @@ export default function CropVideo({ title }: ToolComponentProps) {
|
||||
width: number;
|
||||
height: number;
|
||||
} | null>(null);
|
||||
const [processingError, setProcessingError] = useState<string>('');
|
||||
|
||||
useEffect(() => {
|
||||
if (input) {
|
||||
getVideoDimensions(input)
|
||||
.then((dimensions) => {
|
||||
setVideoDimensions(dimensions);
|
||||
setProcessingError('');
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error getting video dimensions:', error);
|
||||
setProcessingError('Failed to load video dimensions');
|
||||
});
|
||||
} else {
|
||||
setVideoDimensions(null);
|
||||
setProcessingError('');
|
||||
}
|
||||
}, [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 (
|
||||
optionsValues: InitialValuesType,
|
||||
input: File | null
|
||||
) => {
|
||||
if (!input) return;
|
||||
|
||||
const error = validateDimensions(optionsValues);
|
||||
if (error) {
|
||||
setProcessingError(error);
|
||||
return;
|
||||
}
|
||||
|
||||
setProcessingError('');
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
@@ -52,12 +85,16 @@ export default function CropVideo({ title }: ToolComponentProps) {
|
||||
setResult(croppedFile);
|
||||
} catch (error) {
|
||||
console.error('Error cropping video:', error);
|
||||
setProcessingError(
|
||||
'Error cropping video. Please check parameters and video file.'
|
||||
);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const debouncedCompute = useCallback(debounce(compute, 1000), [
|
||||
// 2 seconds to avoid starting job half way through
|
||||
const debouncedCompute = useCallback(debounce(compute, 2000), [
|
||||
videoDimensions
|
||||
]);
|
||||
|
||||
@@ -86,6 +123,11 @@ export default function CropVideo({ title }: ToolComponentProps) {
|
||||
title: 'Crop Coordinates',
|
||||
component: (
|
||||
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
|
||||
{processingError && (
|
||||
<Alert severity="error" sx={{ mb: 2 }}>
|
||||
{processingError}
|
||||
</Alert>
|
||||
)}
|
||||
<Box sx={{ display: 'flex', gap: 2 }}>
|
||||
<TextField
|
||||
label="X (left)"
|
||||
|
||||
@@ -45,6 +45,10 @@ export async function cropVideo(
|
||||
|
||||
const args = [];
|
||||
|
||||
if (options.width <= 0 || options.height <= 0) {
|
||||
throw new Error('Width and height must be positive');
|
||||
}
|
||||
|
||||
args.push('-i', inputName);
|
||||
args.push(
|
||||
'-vf',
|
||||
|
||||
Reference in New Issue
Block a user