Refactored code for readability

This commit is contained in:
Mario Fragnito
2025-05-23 15:35:28 +02:00
parent 2d55cab940
commit 9d89f8c0b9

View File

@@ -4,7 +4,6 @@ import React, { useState } from 'react';
import ToolContent from '@components/ToolContent'; import ToolContent from '@components/ToolContent';
import { ToolComponentProps } from '@tools/defineTool'; import { ToolComponentProps } from '@tools/defineTool';
import { GetGroupsType } from '@components/options/ToolOptions'; import { GetGroupsType } from '@components/options/ToolOptions';
import { main } from './service';
import { InitialValuesType } from './types'; import { InitialValuesType } from './types';
import ToolVideoInput from '@components/input/ToolVideoInput'; import ToolVideoInput from '@components/input/ToolVideoInput';
import ToolFileResult from '@components/result/ToolFileResult'; import ToolFileResult from '@components/result/ToolFileResult';
@@ -24,6 +23,28 @@ export default function ChangeSpeed({
const [result, setResult] = useState<File | null>(null); const [result, setResult] = useState<File | null>(null);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
// FFmpeg only supports atempo between 0.5 and 2.0, so we chain filters
const computeAudioFilter = (speed: number): string => {
if (speed <= 2 && speed >= 0.5) {
return `atempo=${speed}`;
}
// Break into supported chunks
const filters = [];
let remainingSpeed = speed;
while (remainingSpeed > 2.0) {
filters.push('atempo=2.0');
remainingSpeed /= 2.0;
}
while (remainingSpeed < 0.5) {
filters.push('atempo=0.5');
remainingSpeed /= 0.5;
}
filters.push(`atempo=${remainingSpeed.toFixed(2)}`);
return filters.join(',');
};
const compute = (optionsValues: InitialValuesType, input: File | null) => { const compute = (optionsValues: InitialValuesType, input: File | null) => {
if (!input) return; if (!input) return;
const { newSpeed } = optionsValues; const { newSpeed } = optionsValues;
@@ -68,14 +89,10 @@ export default function ChangeSpeed({
audioFilter, audioFilter,
'-c:v', '-c:v',
'libx264', 'libx264',
'-crf',
'18',
'-preset', '-preset',
'slow', 'ultrafast',
'-c:a', '-c:a',
'aac', 'aac',
'-b:a',
'192k',
outputName outputName
]); ]);
@@ -100,52 +117,30 @@ export default function ChangeSpeed({
} finally { } finally {
setLoading(false); setLoading(false);
} }
// FFmpeg only supports atempo between 0.5 and 2.0, so we chain filters
function computeAudioFilter(speed: number): string {
if (speed <= 2 && speed >= 0.5) {
return `atempo=${speed}`;
}
// Break into supported chunks
const filters = [];
let remainingSpeed = speed;
while (remainingSpeed > 2.0) {
filters.push('atempo=2.0');
remainingSpeed /= 2.0;
}
while (remainingSpeed < 0.5) {
filters.push('atempo=0.5');
remainingSpeed /= 0.5;
}
filters.push(`atempo=${remainingSpeed.toFixed(2)}`);
return filters.join(',');
}
}; };
// Here we set the output video // Here we set the output video
processVideo(input, newSpeed) processVideo(input, newSpeed);
}; };
const getGroups: GetGroupsType<InitialValuesType> | null = ({ const getGroups: GetGroupsType<InitialValuesType> | null = ({
values, values,
updateField updateField
}) => [ }) => [
{ {
title: 'New Video Speed', title: 'New Video Speed',
component: ( component: (
<Box> <Box>
<TextFieldWithDesc <TextFieldWithDesc
value={values.newSpeed.toString()} value={values.newSpeed.toString()}
onOwnChange={(val) => updateField('newSpeed', Number(val))} onOwnChange={(val) => updateField('newSpeed', Number(val))}
description="Default multiplier: 2 means 2x faster" description="Default multiplier: 2 means 2x faster"
type="number" type="number"
/> />
</Box> </Box>
) )
} }
]; ];
return ( return (
<ToolContent <ToolContent
title={title} title={title}