226 lines
6.6 KiB
TypeScript
Raw Normal View History

2025-07-12 23:02:35 -07:00
import { Box, Typography, TextField, Alert } from '@mui/material';
import React, { useState, useCallback } from 'react';
2025-05-25 13:39:21 +02:00
import ToolContent from '@components/ToolContent';
import { ToolComponentProps } from '@tools/defineTool';
import { cropVideo, getVideoDimensions } from './service';
import { InitialValuesType } from './types';
2025-07-12 23:02:35 -07:00
import ToolVideoInput from '@components/input/ToolVideoInput';
import { GetGroupsType } from '@components/options/ToolOptions';
import ToolFileResult from '@components/result/ToolFileResult';
import { debounce } from 'lodash';
import { useTranslation } from 'react-i18next';
2025-05-25 13:39:21 +02:00
2025-05-26 19:41:27 +01:00
const initialValues: InitialValuesType = {
2025-05-25 13:39:21 +02:00
x: 0,
y: 0,
width: 100,
height: 100
};
export default function CropVideo({ title }: ToolComponentProps) {
2025-07-13 13:01:02 +01:00
const { t } = useTranslation('video');
2025-05-25 13:39:21 +02:00
const [input, setInput] = useState<File | null>(null);
const [result, setResult] = useState<File | null>(null);
const [loading, setLoading] = useState(false);
const [videoDimensions, setVideoDimensions] = useState<{
width: number;
height: number;
} | null>(null);
const [processingError, setProcessingError] = useState<string>('');
2025-05-25 13:39:21 +02:00
const validateDimensions = (values: InitialValuesType): string => {
if (!videoDimensions) return '';
if (values.x < 0 || values.y < 0) {
2025-07-13 13:01:02 +01:00
return t('cropVideo.errorNonNegativeCoordinates');
}
if (values.width <= 0 || values.height <= 0) {
2025-07-13 13:01:02 +01:00
return t('cropVideo.errorPositiveDimensions');
}
if (values.x + values.width > videoDimensions.width) {
2025-07-13 13:01:02 +01:00
return t('cropVideo.errorBeyondWidth', {
2025-07-12 23:02:35 -07:00
width: videoDimensions.width
});
}
if (values.y + values.height > videoDimensions.height) {
2025-07-13 13:01:02 +01:00
return t('cropVideo.errorBeyondHeight', {
2025-07-12 23:02:35 -07:00
height: videoDimensions.height
});
}
return '';
};
2025-05-25 13:39:21 +02:00
const compute = async (
optionsValues: InitialValuesType,
input: File | null
) => {
if (!input) return;
const error = validateDimensions(optionsValues);
if (error) {
setProcessingError(error);
return;
}
setProcessingError('');
2025-05-25 13:39:21 +02:00
setLoading(true);
try {
const croppedFile = await cropVideo(input, optionsValues);
setResult(croppedFile);
} catch (error) {
console.error('Error cropping video:', error);
2025-07-13 13:01:02 +01:00
setProcessingError(t('cropVideo.errorCroppingVideo'));
2025-05-25 13:39:21 +02:00
} finally {
setLoading(false);
}
};
// 2 seconds to avoid starting job half way through
const debouncedCompute = useCallback(debounce(compute, 2000), [
2025-05-25 13:39:21 +02:00
videoDimensions
]);
const getGroups: GetGroupsType<InitialValuesType> = ({
values,
updateField
}) => [
{
2025-07-13 13:01:02 +01:00
title: t('cropVideo.videoInformation'),
2025-05-25 13:39:21 +02:00
component: (
<Box>
{videoDimensions ? (
<Typography variant="body2" sx={{ mb: 2 }}>
2025-07-13 13:01:02 +01:00
{t('cropVideo.videoDimensions', {
2025-07-12 23:02:35 -07:00
width: videoDimensions.width,
height: videoDimensions.height
})}
2025-05-25 13:39:21 +02:00
</Typography>
) : (
<Typography variant="body2" sx={{ mb: 2 }}>
2025-07-13 13:01:02 +01:00
{t('cropVideo.loadVideoForDimensions')}
2025-05-25 13:39:21 +02:00
</Typography>
)}
</Box>
)
},
{
2025-07-13 13:01:02 +01:00
title: t('cropVideo.cropCoordinates'),
2025-05-25 13:39:21 +02:00
component: (
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
{processingError && (
<Alert severity="error" sx={{ mb: 2 }}>
{processingError}
</Alert>
)}
2025-05-25 13:39:21 +02:00
<Box sx={{ display: 'flex', gap: 2 }}>
<TextField
2025-07-13 13:01:02 +01:00
label={t('cropVideo.xCoordinate')}
2025-05-25 13:39:21 +02:00
type="number"
value={values.x}
onChange={(e) => updateField('x', parseInt(e.target.value) || 0)}
size="small"
inputProps={{ min: 0 }}
/>
<TextField
2025-07-13 13:01:02 +01:00
label={t('cropVideo.yCoordinate')}
2025-05-25 13:39:21 +02:00
type="number"
value={values.y}
onChange={(e) => updateField('y', parseInt(e.target.value) || 0)}
size="small"
inputProps={{ min: 0 }}
/>
</Box>
<Box sx={{ display: 'flex', gap: 2 }}>
<TextField
2025-07-13 13:01:02 +01:00
label={t('cropVideo.width')}
2025-05-25 13:39:21 +02:00
type="number"
value={values.width}
onChange={(e) =>
updateField('width', parseInt(e.target.value) || 0)
}
size="small"
inputProps={{ min: 1 }}
/>
<TextField
2025-07-13 13:01:02 +01:00
label={t('cropVideo.height')}
2025-05-25 13:39:21 +02:00
type="number"
value={values.height}
onChange={(e) =>
updateField('height', parseInt(e.target.value) || 0)
}
size="small"
inputProps={{ min: 1 }}
/>
</Box>
</Box>
)
}
];
return (
<ToolContent
title={title}
input={input}
2025-05-26 19:41:27 +01:00
renderCustomInput={(values, setFieldValue) => (
2025-05-25 13:39:21 +02:00
<ToolVideoInput
value={input}
2025-05-26 19:41:27 +01:00
onChange={(video) => {
if (video) {
getVideoDimensions(video)
.then((dimensions) => {
const newOptions: InitialValuesType = {
x: dimensions.width / 4,
y: dimensions.height / 4,
width: dimensions.width / 2,
height: dimensions.height / 2
};
setFieldValue('x', newOptions.x);
setFieldValue('y', newOptions.y);
setFieldValue('width', newOptions.width);
setFieldValue('height', newOptions.height);
setVideoDimensions(dimensions);
setProcessingError('');
})
.catch((error) => {
console.error('Error getting video dimensions:', error);
2025-07-13 13:01:02 +01:00
setProcessingError(t('cropVideo.errorLoadingDimensions'));
2025-05-26 19:41:27 +01:00
});
} else {
setVideoDimensions(null);
setProcessingError('');
}
setInput(video);
}}
2025-07-13 13:01:02 +01:00
title={t('cropVideo.inputTitle')}
2025-05-25 13:39:21 +02:00
/>
2025-05-26 19:41:27 +01:00
)}
2025-05-25 13:39:21 +02:00
resultComponent={
loading ? (
<ToolFileResult
2025-07-13 13:01:02 +01:00
title={t('cropVideo.croppingVideo')}
2025-05-25 13:39:21 +02:00
value={null}
loading={true}
extension={''}
/>
) : (
<ToolFileResult
2025-07-13 13:01:02 +01:00
title={t('cropVideo.resultTitle')}
2025-05-25 13:39:21 +02:00
value={result}
extension={'mp4'}
/>
)
}
initialValues={initialValues}
getGroups={getGroups}
compute={debouncedCompute}
setInput={setInput}
/>
);
}