mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-25 08:59:31 +02:00
Fix rendering issue with ToolMultipleVideoInput as well as merge functionality.
This commit is contained in:
@@ -3,7 +3,9 @@ import React, { useState } from 'react';
|
||||
import ToolContent from '@components/ToolContent';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
import ToolFileResult from '@components/result/ToolFileResult';
|
||||
import ToolMultipleVideoInput from '@components/input/ToolMultipleVideoInput';
|
||||
import ToolMultipleVideoInput, {
|
||||
MultiVideoInput
|
||||
} from '@components/input/ToolMultipleVideoInput';
|
||||
import { main } from './service';
|
||||
import { InitialValuesType } from './types';
|
||||
|
||||
@@ -13,28 +15,42 @@ export default function MergeVideo({
|
||||
title,
|
||||
longDescription
|
||||
}: ToolComponentProps) {
|
||||
const [input, setInput] = useState<File[] | null>(null);
|
||||
const [input, setInput] = useState<MultiVideoInput[]>([]);
|
||||
const [result, setResult] = useState<File | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const compute = async (_values: InitialValuesType, input: File[] | null) => {
|
||||
if (!input || input.length < 2) return;
|
||||
console.log('MergeVideo component rendering, input:', input);
|
||||
|
||||
const compute = async (
|
||||
_values: InitialValuesType,
|
||||
input: MultiVideoInput[]
|
||||
) => {
|
||||
console.log('Compute called with input:', input);
|
||||
if (!input || input.length < 2) {
|
||||
console.log('Not enough files to merge');
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
try {
|
||||
const mergedBlob = await main(input, initialValues);
|
||||
const files = input.map((item) => item.file);
|
||||
console.log(
|
||||
'Files to merge:',
|
||||
files.map((f) => f.name)
|
||||
);
|
||||
const mergedBlob = await main(files, initialValues);
|
||||
const mergedFile = new File([mergedBlob], 'merged-video.mp4', {
|
||||
type: 'video/mp4'
|
||||
});
|
||||
setResult(mergedFile);
|
||||
console.log('Merge successful');
|
||||
} catch (err) {
|
||||
console.error(`Failed to merge video: ${err}`);
|
||||
setResult(null);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const getGroups = () => [];
|
||||
|
||||
return (
|
||||
<ToolContent
|
||||
title={title}
|
||||
@@ -42,8 +58,13 @@ export default function MergeVideo({
|
||||
inputComponent={
|
||||
<ToolMultipleVideoInput
|
||||
value={input}
|
||||
onChange={setInput}
|
||||
onChange={(newInput) => {
|
||||
console.log('Input changed:', newInput);
|
||||
setInput(newInput);
|
||||
}}
|
||||
accept={['video/*', '.mp4', '.avi', '.mov', '.mkv']}
|
||||
title="Input Videos"
|
||||
type="video"
|
||||
/>
|
||||
}
|
||||
resultComponent={
|
||||
@@ -55,7 +76,7 @@ export default function MergeVideo({
|
||||
/>
|
||||
}
|
||||
initialValues={initialValues}
|
||||
getGroups={getGroups}
|
||||
getGroups={null}
|
||||
setInput={setInput}
|
||||
compute={compute}
|
||||
toolInfo={{ title: `What is a ${title}?`, description: longDescription }}
|
||||
|
@@ -1,4 +1,22 @@
|
||||
import { expect, describe, it } from 'vitest';
|
||||
import { expect, describe, it, vi } from 'vitest';
|
||||
|
||||
// Mock FFmpeg and fetchFile to avoid Node.js compatibility issues
|
||||
vi.mock('@ffmpeg/ffmpeg', () => ({
|
||||
FFmpeg: vi.fn().mockImplementation(() => ({
|
||||
loaded: false,
|
||||
load: vi.fn().mockResolvedValue(undefined),
|
||||
writeFile: vi.fn().mockResolvedValue(undefined),
|
||||
exec: vi.fn().mockResolvedValue(undefined),
|
||||
readFile: vi.fn().mockResolvedValue(new Uint8Array([1, 2, 3, 4])),
|
||||
deleteFile: vi.fn().mockResolvedValue(undefined)
|
||||
}))
|
||||
}));
|
||||
|
||||
vi.mock('@ffmpeg/util', () => ({
|
||||
fetchFile: vi.fn().mockResolvedValue(new Uint8Array([1, 2, 3, 4]))
|
||||
}));
|
||||
|
||||
// Import after mocking
|
||||
import { main } from './service';
|
||||
|
||||
function createMockFile(name: string, type = 'video/mp4') {
|
||||
@@ -7,14 +25,28 @@ function createMockFile(name: string, type = 'video/mp4') {
|
||||
|
||||
describe('merge-video', () => {
|
||||
it('throws if less than two files are provided', async () => {
|
||||
await expect(main([], {})).rejects.toThrow();
|
||||
await expect(main([createMockFile('a.mp4')], {})).rejects.toThrow();
|
||||
await expect(main([], {})).rejects.toThrow(
|
||||
'Please provide at least two video files to merge.'
|
||||
);
|
||||
await expect(main([createMockFile('a.mp4')], {})).rejects.toThrow(
|
||||
'Please provide at least two video files to merge.'
|
||||
);
|
||||
});
|
||||
|
||||
it('merges two video files (mocked)', async () => {
|
||||
// This will throw until ffmpeg logic is implemented
|
||||
await expect(
|
||||
main([createMockFile('a.mp4'), createMockFile('b.mp4')], {})
|
||||
).rejects.toThrow('Video merging not yet implemented.');
|
||||
it('throws if input is not an array', async () => {
|
||||
// @ts-ignore - testing invalid input
|
||||
await expect(main(null, {})).rejects.toThrow(
|
||||
'Please provide at least two video files to merge.'
|
||||
);
|
||||
});
|
||||
|
||||
it('successfully merges video files (mocked)', async () => {
|
||||
const mockFile1 = createMockFile('video1.mp4');
|
||||
const mockFile2 = createMockFile('video2.mp4');
|
||||
|
||||
const result = await main([mockFile1, mockFile2], {});
|
||||
|
||||
expect(result).toBeInstanceOf(Blob);
|
||||
expect(result.type).toBe('video/mp4');
|
||||
});
|
||||
});
|
||||
|
@@ -2,8 +2,6 @@ import { InitialValuesType, MergeVideoInput, MergeVideoOutput } from './types';
|
||||
import { FFmpeg } from '@ffmpeg/ffmpeg';
|
||||
import { fetchFile } from '@ffmpeg/util';
|
||||
|
||||
const ffmpeg = new FFmpeg();
|
||||
|
||||
// This function will use ffmpeg.wasm to merge multiple video files in the browser.
|
||||
// Returns a Promise that resolves to a Blob of the merged video.
|
||||
export async function main(
|
||||
@@ -14,40 +12,62 @@ export async function main(
|
||||
throw new Error('Please provide at least two video files to merge.');
|
||||
}
|
||||
|
||||
if (!ffmpeg.loaded) {
|
||||
await ffmpeg.load({
|
||||
wasmURL:
|
||||
'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.9/dist/esm/ffmpeg-core.wasm'
|
||||
});
|
||||
}
|
||||
// Create a new FFmpeg instance for each operation to avoid conflicts
|
||||
const ffmpeg = new FFmpeg();
|
||||
|
||||
// Write all input files to ffmpeg FS
|
||||
const fileNames = input.map((file, idx) => `input${idx}.mp4`);
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
await ffmpeg.writeFile(fileNames[i], await fetchFile(input[i]));
|
||||
}
|
||||
|
||||
// Create concat list file
|
||||
const concatList = fileNames.map((name) => `file '${name}'`).join('\n');
|
||||
await ffmpeg.writeFile(
|
||||
'concat_list.txt',
|
||||
new TextEncoder().encode(concatList)
|
||||
);
|
||||
|
||||
// Run ffmpeg concat demuxer
|
||||
const fileNames: string[] = [];
|
||||
const outputName = 'output.mp4';
|
||||
await ffmpeg.exec([
|
||||
'-f',
|
||||
'concat',
|
||||
'-safe',
|
||||
'0',
|
||||
'-i',
|
||||
'concat_list.txt',
|
||||
'-c',
|
||||
'copy',
|
||||
outputName
|
||||
]);
|
||||
|
||||
const mergedData = await ffmpeg.readFile(outputName);
|
||||
return new Blob([mergedData], { type: 'video/mp4' });
|
||||
try {
|
||||
// Load FFmpeg
|
||||
if (!ffmpeg.loaded) {
|
||||
await ffmpeg.load({
|
||||
wasmURL:
|
||||
'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.9/dist/esm/ffmpeg-core.wasm'
|
||||
});
|
||||
}
|
||||
|
||||
// Write all input files to ffmpeg FS
|
||||
fileNames.push(...input.map((file, idx) => `input${idx}.mp4`));
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
await ffmpeg.writeFile(fileNames[i], await fetchFile(input[i]));
|
||||
}
|
||||
|
||||
// Create concat list file
|
||||
const concatList = fileNames.map((name) => `file '${name}'`).join('\n');
|
||||
await ffmpeg.writeFile(
|
||||
'concat_list.txt',
|
||||
new TextEncoder().encode(concatList)
|
||||
);
|
||||
|
||||
// Run ffmpeg concat demuxer
|
||||
await ffmpeg.exec([
|
||||
'-f',
|
||||
'concat',
|
||||
'-safe',
|
||||
'0',
|
||||
'-i',
|
||||
'concat_list.txt',
|
||||
'-c',
|
||||
'copy',
|
||||
outputName
|
||||
]);
|
||||
|
||||
const mergedData = await ffmpeg.readFile(outputName);
|
||||
return new Blob([mergedData], { type: 'video/mp4' });
|
||||
} catch (error) {
|
||||
console.error('Error merging videos:', error);
|
||||
throw new Error(`Failed to merge videos: ${error}`);
|
||||
} finally {
|
||||
// Clean up temporary files
|
||||
try {
|
||||
for (const fileName of fileNames) {
|
||||
await ffmpeg.deleteFile(fileName);
|
||||
}
|
||||
await ffmpeg.deleteFile('concat_list.txt');
|
||||
await ffmpeg.deleteFile(outputName);
|
||||
} catch (cleanupError) {
|
||||
console.warn('Error cleaning up temporary files:', cleanupError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user