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 { ToolComponentProps } from '@tools/defineTool';
import { GetGroupsType } from '@components/options/ToolOptions';
import { main } from './service';
import { InitialValuesType } from './types';
import ToolVideoInput from '@components/input/ToolVideoInput';
import ToolFileResult from '@components/result/ToolFileResult';
@@ -24,6 +23,28 @@ export default function ChangeSpeed({
const [result, setResult] = useState<File | null>(null);
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) => {
if (!input) return;
const { newSpeed } = optionsValues;
@@ -68,14 +89,10 @@ export default function ChangeSpeed({
audioFilter,
'-c:v',
'libx264',
'-crf',
'18',
'-preset',
'slow',
'ultrafast',
'-c:a',
'aac',
'-b:a',
'192k',
outputName
]);
@@ -100,32 +117,10 @@ export default function ChangeSpeed({
} finally {
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
processVideo(input, newSpeed)
processVideo(input, newSpeed);
};
const getGroups: GetGroupsType<InitialValuesType> | null = ({