mirror of
https://github.com/iib0011/omni-tools.git
synced 2026-12-01 09:22:25 +03:00
chore: video to gif updated
This commit is contained in:
1 parent
8fd30960b9
commit
0dd15b934e
4 files changed
+135
-156
No files matched your search
@@ -143,6 +143,18 @@
|
||||
"videoToGif": {
|
||||
"description": "Convert video files to animated GIF format. Extract specific time ranges and create shareable animated images.",
|
||||
"shortDescription": "Convert video to animated GIF",
|
||||
"title": "Video to GIF"
|
||||
"title": "Video to GIF",
|
||||
"inputTitle": "Input Video",
|
||||
"resultTitle": "Output GIF",
|
||||
"options": {
|
||||
"quality": "Quality",
|
||||
"low": "Low",
|
||||
"mid": "Medium",
|
||||
"high": "High",
|
||||
"ultra": "Ultra",
|
||||
"timestamps": "Timestamps",
|
||||
"startTime": "Start Time",
|
||||
"endTime": "End Time"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,172 +1,67 @@
|
||||
import { Box } from '@mui/material';
|
||||
import React, { useState } from 'react';
|
||||
import * as Yup from 'yup';
|
||||
import React, { useState, useCallback } from 'react';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import { GetGroupsType } from '@components/options/ToolOptions';
|
||||
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
|
||||
import { updateNumberField } from '@utils/string';
|
||||
import { InitialValuesType } from './types';
|
||||
import { InitialValuesType, GIF_PRESETS, Quality } from './types';
|
||||
import ToolVideoInput from '@components/input/ToolVideoInput';
|
||||
import ToolFileResult from '@components/result/ToolFileResult';
|
||||
import SimpleRadio from '@components/options/SimpleRadio';
|
||||
import { FFmpeg } from '@ffmpeg/ffmpeg';
|
||||
import { fetchFile } from '@ffmpeg/util';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { debounce } from 'lodash';
|
||||
import { videoToGif } from './service';
|
||||
|
||||
const initialValues: InitialValuesType = {
|
||||
quality: 'mid',
|
||||
fps: '10',
|
||||
scale: '320:-1:flags=bicubic',
|
||||
start: 0,
|
||||
end: 100
|
||||
};
|
||||
|
||||
const validationSchema = Yup.object({
|
||||
start: Yup.number().min(0, 'Start time must be positive'),
|
||||
end: Yup.number().min(
|
||||
Yup.ref('start'),
|
||||
'End time must be greater than start time'
|
||||
)
|
||||
});
|
||||
|
||||
export default function VideoToGif({
|
||||
title,
|
||||
longDescription
|
||||
}: ToolComponentProps) {
|
||||
export default function VideoToGif({ title }: ToolComponentProps) {
|
||||
const { t } = useTranslation('video');
|
||||
const [input, setInput] = useState<File | null>(null);
|
||||
const [result, setResult] = useState<File | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const compute = (values: InitialValuesType, input: File | null) => {
|
||||
const compute = async (values: InitialValuesType, input: File | null) => {
|
||||
if (!input) return;
|
||||
const { fps, scale, start, end } = values;
|
||||
let ffmpeg: FFmpeg | null = null;
|
||||
let ffmpegLoaded = false;
|
||||
setLoading(true);
|
||||
|
||||
const convertVideoToGif = async (
|
||||
file: File,
|
||||
fps: string,
|
||||
scale: string,
|
||||
start: number,
|
||||
end: number
|
||||
): Promise<void> => {
|
||||
setLoading(true);
|
||||
|
||||
if (!ffmpeg) {
|
||||
ffmpeg = new FFmpeg();
|
||||
}
|
||||
|
||||
if (!ffmpegLoaded) {
|
||||
await ffmpeg.load({
|
||||
wasmURL:
|
||||
'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.9/dist/esm/ffmpeg-core.wasm'
|
||||
});
|
||||
ffmpegLoaded = true;
|
||||
}
|
||||
|
||||
const fileName = file.name;
|
||||
const outputName = 'output.gif';
|
||||
|
||||
try {
|
||||
ffmpeg.writeFile(fileName, await fetchFile(file));
|
||||
|
||||
await ffmpeg.exec([
|
||||
'-i',
|
||||
fileName,
|
||||
'-ss',
|
||||
start.toString(),
|
||||
'-to',
|
||||
end.toString(),
|
||||
'-vf',
|
||||
`fps=${fps},scale=${scale},palettegen`,
|
||||
'palette.png'
|
||||
]);
|
||||
|
||||
await ffmpeg.exec([
|
||||
'-i',
|
||||
fileName,
|
||||
'-i',
|
||||
'palette.png',
|
||||
'-ss',
|
||||
start.toString(),
|
||||
'-to',
|
||||
end.toString(),
|
||||
'-filter_complex',
|
||||
`fps=${fps},scale=${scale}[x];[x][1:v]paletteuse`,
|
||||
outputName
|
||||
]);
|
||||
|
||||
const data = await ffmpeg.readFile(outputName);
|
||||
|
||||
const blob = new Blob([data as any], { type: 'image/gif' });
|
||||
const convertedFile = new File([blob], outputName, {
|
||||
type: 'image/gif'
|
||||
});
|
||||
|
||||
await ffmpeg.deleteFile(fileName);
|
||||
await ffmpeg.deleteFile(outputName);
|
||||
|
||||
setResult(convertedFile);
|
||||
} catch (err) {
|
||||
console.error(`Failed to convert video: ${err}`);
|
||||
throw err;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
convertVideoToGif(input, fps, scale, start, end);
|
||||
try {
|
||||
const resultFile = await videoToGif(input, values);
|
||||
setResult(resultFile);
|
||||
} catch (error) {
|
||||
console.error('Error converting video to gif:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const debouncedCompute = useCallback(debounce(compute, 1000), []);
|
||||
|
||||
const getGroups: GetGroupsType<InitialValuesType> | null = ({
|
||||
values,
|
||||
updateField
|
||||
}) => [
|
||||
{
|
||||
title: 'Set Quality',
|
||||
title: t('videoToGif.options.quality'),
|
||||
component: (
|
||||
<Box>
|
||||
<SimpleRadio
|
||||
title="Low"
|
||||
onClick={() => {
|
||||
updateField('quality', 'low');
|
||||
updateField('fps', '5');
|
||||
updateField('scale', '240:-1:flags=bilinear');
|
||||
}}
|
||||
checked={values.quality === 'low'}
|
||||
/>
|
||||
<SimpleRadio
|
||||
title="Mid"
|
||||
onClick={() => {
|
||||
updateField('quality', 'mid');
|
||||
updateField('fps', '10');
|
||||
updateField('scale', '320:-1:flags=bicubic');
|
||||
}}
|
||||
checked={values.quality === 'mid'}
|
||||
/>
|
||||
<SimpleRadio
|
||||
title="High"
|
||||
onClick={() => {
|
||||
updateField('quality', 'high');
|
||||
updateField('fps', '15');
|
||||
updateField('scale', '480:-1:flags=lanczos');
|
||||
}}
|
||||
checked={values.quality === 'high'}
|
||||
/>
|
||||
<SimpleRadio
|
||||
title="Ultra"
|
||||
onClick={() => {
|
||||
updateField('quality', 'ultra');
|
||||
updateField('fps', '15');
|
||||
updateField('scale', '640:-1:flags=lanczos');
|
||||
}}
|
||||
checked={values.quality === 'ultra'}
|
||||
/>
|
||||
{(Object.keys(GIF_PRESETS) as Quality[]).map((quality) => (
|
||||
<SimpleRadio
|
||||
key={quality}
|
||||
title={t(`videoToGif.options.${quality}`)}
|
||||
checked={values.quality === quality}
|
||||
onClick={() => updateField('quality', quality)}
|
||||
/>
|
||||
))}
|
||||
</Box>
|
||||
)
|
||||
},
|
||||
{
|
||||
title: 'Timestamps',
|
||||
title: t('videoToGif.options.timestamps'),
|
||||
component: (
|
||||
<Box>
|
||||
<TextFieldWithDesc
|
||||
@@ -174,7 +69,7 @@ export default function VideoToGif({
|
||||
updateNumberField(value, 'start', updateField)
|
||||
}
|
||||
value={values.start}
|
||||
label="Start Time"
|
||||
label={t('videoToGif.options.startTime')}
|
||||
sx={{ mb: 2, backgroundColor: 'background.paper' }}
|
||||
/>
|
||||
<TextFieldWithDesc
|
||||
@@ -182,7 +77,7 @@ export default function VideoToGif({
|
||||
updateNumberField(value, 'end', updateField)
|
||||
}
|
||||
value={values.end}
|
||||
label="End Time"
|
||||
label={t('videoToGif.options.endTime')}
|
||||
/>
|
||||
</Box>
|
||||
)
|
||||
@@ -198,7 +93,7 @@ export default function VideoToGif({
|
||||
<ToolVideoInput
|
||||
value={input}
|
||||
onChange={setInput}
|
||||
title={'Input Video'}
|
||||
title={t('videoToGif.inputTitle')}
|
||||
showTrimControls={true}
|
||||
onTrimChange={(start, end) => {
|
||||
setFieldValue('start', start);
|
||||
@@ -210,25 +105,17 @@ export default function VideoToGif({
|
||||
);
|
||||
}}
|
||||
resultComponent={
|
||||
loading ? (
|
||||
<ToolFileResult
|
||||
title="Converting to Gif"
|
||||
value={null}
|
||||
loading={true}
|
||||
/>
|
||||
) : (
|
||||
<ToolFileResult
|
||||
title="Converted to Gif"
|
||||
value={result}
|
||||
extension="gif"
|
||||
/>
|
||||
)
|
||||
<ToolFileResult
|
||||
title={t('videoToGif.resultTitle')}
|
||||
loading={loading}
|
||||
value={result}
|
||||
extension="gif"
|
||||
/>
|
||||
}
|
||||
initialValues={initialValues}
|
||||
getGroups={getGroups}
|
||||
setInput={setInput}
|
||||
compute={compute}
|
||||
toolInfo={{ title: `What is a ${title}?`, description: longDescription }}
|
||||
compute={debouncedCompute}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { runFFmpegTask } from 'lib/ffmpeg';
|
||||
import { fetchFile } from '@ffmpeg/util';
|
||||
import { GIF_PRESETS, InitialValuesType } from './types';
|
||||
|
||||
export const videoToGif = async (
|
||||
input: File,
|
||||
options: InitialValuesType
|
||||
): Promise<File> => {
|
||||
return runFFmpegTask(async ({ ffmpeg, tempFile }) => {
|
||||
const fileName = tempFile('.mp4');
|
||||
const outputName = tempFile('.gif');
|
||||
const paletteName = tempFile('.png');
|
||||
|
||||
const { start, end, quality } = options;
|
||||
|
||||
const { fps, scale } = GIF_PRESETS[quality];
|
||||
|
||||
await ffmpeg.writeFile(fileName, await fetchFile(input));
|
||||
|
||||
await ffmpeg.exec([
|
||||
'-i',
|
||||
fileName,
|
||||
'-ss',
|
||||
start.toString(),
|
||||
'-to',
|
||||
end.toString(),
|
||||
'-vf',
|
||||
`fps=${fps},scale=${scale},palettegen`,
|
||||
paletteName
|
||||
]);
|
||||
|
||||
await ffmpeg.exec([
|
||||
'-i',
|
||||
fileName,
|
||||
'-i',
|
||||
paletteName,
|
||||
'-ss',
|
||||
start.toString(),
|
||||
'-to',
|
||||
end.toString(),
|
||||
'-filter_complex',
|
||||
`fps=${fps},scale=${scale}[x];[x][1:v]paletteuse`,
|
||||
outputName
|
||||
]);
|
||||
|
||||
const data = await ffmpeg.readFile(outputName);
|
||||
|
||||
const blob = new Blob([new Uint8Array(data as Uint8Array)], {
|
||||
type: 'image/gif'
|
||||
});
|
||||
return new File([blob], `${input.name.replace(/\.[^/.]+$/, '')}.gif`, {
|
||||
type: 'image/gif'
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -1,7 +1,32 @@
|
||||
export type Quality = 'low' | 'mid' | 'high' | 'ultra';
|
||||
|
||||
export type InitialValuesType = {
|
||||
quality: 'mid' | 'high' | 'low' | 'ultra';
|
||||
fps: string;
|
||||
scale: string;
|
||||
quality: Quality;
|
||||
start: number;
|
||||
end: number;
|
||||
};
|
||||
|
||||
export const GIF_PRESETS: Record<
|
||||
Quality,
|
||||
{
|
||||
fps: string;
|
||||
scale: string;
|
||||
}
|
||||
> = {
|
||||
low: {
|
||||
fps: '5',
|
||||
scale: '240:-1:flags=bilinear'
|
||||
},
|
||||
mid: {
|
||||
fps: '10',
|
||||
scale: '320:-1:flags=bicubic'
|
||||
},
|
||||
high: {
|
||||
fps: '15',
|
||||
scale: '480:-1:flags=lanczos'
|
||||
},
|
||||
ultra: {
|
||||
fps: '15',
|
||||
scale: '640:-1:flags=lanczos'
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user