fix: merge videos

This commit is contained in:
Ibrahima G. Coulibaly
2025-07-18 02:34:32 +01:00
parent f730c0548e
commit 9fa493e27b
3 changed files with 105 additions and 45 deletions

View File

@@ -19,32 +19,22 @@ export default function MergeVideo({
const [result, setResult] = useState<File | null>(null); const [result, setResult] = useState<File | null>(null);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
console.log('MergeVideo component rendering, input:', input);
const compute = async ( const compute = async (
_values: InitialValuesType, _values: InitialValuesType,
input: MultiVideoInput[] input: MultiVideoInput[]
) => { ) => {
console.log('Compute called with input:', input);
if (!input || input.length < 2) { if (!input || input.length < 2) {
console.log('Not enough files to merge');
return; return;
} }
setLoading(true); setLoading(true);
try { try {
const files = input.map((item) => item.file); const files = input.map((item) => item.file);
console.log(
'Files to merge:',
files.map((f) => f.name)
);
const mergedBlob = await mergeVideos(files, initialValues); const mergedBlob = await mergeVideos(files, initialValues);
const mergedFile = new File([mergedBlob], 'merged-video.mp4', { const mergedFile = new File([mergedBlob], 'merged-video.mp4', {
type: 'video/mp4' type: 'video/mp4'
}); });
setResult(mergedFile); setResult(mergedFile);
console.log('Merge successful');
} catch (err) { } catch (err) {
console.error(`Failed to merge video: ${err}`);
setResult(null); setResult(null);
} finally { } finally {
setLoading(false); setLoading(false);
@@ -59,7 +49,6 @@ export default function MergeVideo({
<ToolMultipleVideoInput <ToolMultipleVideoInput
value={input} value={input}
onChange={(newInput) => { onChange={(newInput) => {
console.log('Input changed:', newInput);
setInput(newInput); setInput(newInput);
}} }}
accept={['video/*', '.mp4', '.avi', '.mov', '.mkv']} accept={['video/*', '.mp4', '.avi', '.mov', '.mkv']}

View File

@@ -49,4 +49,14 @@ describe('merge-video', () => {
expect(result).toBeInstanceOf(Blob); expect(result).toBeInstanceOf(Blob);
expect(result.type).toBe('video/mp4'); expect(result.type).toBe('video/mp4');
}); });
it('handles different video formats by re-encoding', async () => {
const mockFile1 = createMockFile('video1.avi', 'video/x-msvideo');
const mockFile2 = createMockFile('video2.mov', 'video/quicktime');
const result = await mergeVideos([mockFile1, mockFile2], {});
expect(result).toBeInstanceOf(Blob);
expect(result.type).toBe('video/mp4');
});
}); });

View File

@@ -2,8 +2,6 @@ import { InitialValuesType, MergeVideoInput, MergeVideoOutput } from './types';
import { FFmpeg } from '@ffmpeg/ffmpeg'; import { FFmpeg } from '@ffmpeg/ffmpeg';
import { fetchFile } from '@ffmpeg/util'; import { fetchFile } from '@ffmpeg/util';
// 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 mergeVideos( export async function mergeVideos(
input: MergeVideoInput, input: MergeVideoInput,
options: InitialValuesType options: InitialValuesType
@@ -19,55 +17,118 @@ export async function mergeVideos(
const outputName = 'output.mp4'; const outputName = 'output.mp4';
try { try {
// Load FFmpeg // Load FFmpeg with proper error handling
if (!ffmpeg.loaded) { if (!ffmpeg.loaded) {
await ffmpeg.load({ await ffmpeg.load({
wasmURL: wasmURL:
'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.9/dist/esm/ffmpeg-core.wasm' 'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.9/dist/esm/ffmpeg-core.wasm',
workerURL:
'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.9/dist/esm/ffmpeg-core.worker.js'
}); });
} }
// Write all input files to ffmpeg FS // Write all input files to ffmpeg FS with better error handling
fileNames.push(...input.map((file, idx) => `input${idx}.mp4`));
for (let i = 0; i < input.length; i++) { for (let i = 0; i < input.length; i++) {
await ffmpeg.writeFile(fileNames[i], await fetchFile(input[i])); const fileName = `input${i}.mp4`;
fileNames.push(fileName);
try {
const fileData = await fetchFile(input[i]);
await ffmpeg.writeFile(fileName, fileData);
console.log(`Successfully wrote ${fileName}`);
} catch (fileError) {
console.error(`Failed to write ${fileName}:`, fileError);
throw new Error(`Failed to process input file ${i + 1}: ${fileError}`);
}
} }
// Create concat list file // Build the filter_complex string for concat filter
const concatList = fileNames.map((name) => `file '${name}'`).join('\n'); const videoInputs = fileNames.map((_, idx) => `[${idx}:v]`).join(' ');
await ffmpeg.writeFile( const audioInputs = fileNames.map((_, idx) => `[${idx}:a]`).join(' ');
'concat_list.txt', const filterComplex = `${videoInputs} ${audioInputs} concat=n=${input.length}:v=1:a=1 [v] [a]`;
new TextEncoder().encode(concatList)
);
// Run ffmpeg concat demuxer // Build input arguments
await ffmpeg.exec([ const inputArgs = [];
'-f', for (const fileName of fileNames) {
'concat', inputArgs.push('-i', fileName);
'-safe', }
'0',
'-i', console.log('Starting FFmpeg processing...');
'concat_list.txt', console.log('Filter complex:', filterComplex);
'-c',
'copy', // Method 2: Fallback to concat demuxer
outputName try {
]); console.log('Trying concat demuxer method...');
const concatList = fileNames.map((name) => `file '${name}'`).join('\n');
await ffmpeg.writeFile(
'concat_list.txt',
new TextEncoder().encode(concatList)
);
await ffmpeg.exec([
'-f',
'concat',
'-safe',
'0',
'-i',
'concat_list.txt',
'-c:v',
'libx264',
'-preset',
'ultrafast',
'-crf',
'30',
'-threads',
'0',
'-y',
outputName
]);
// Check if output was created
try {
const testRead = await ffmpeg.readFile(outputName);
if (testRead && testRead.length > 0) {
console.log('Concat demuxer method succeeded');
}
} catch (readError) {
console.log('Concat demuxer method failed to produce output');
}
} catch (execError) {
console.error('Concat demuxer method failed:', execError);
}
// Check if output file exists and read it
let mergedData;
try {
mergedData = await ffmpeg.readFile(outputName);
console.log('Successfully read output file');
} catch (readError) {
console.error('Failed to read output file:', readError);
throw new Error('Failed to read merged video file');
}
if (!mergedData || mergedData.length === 0) {
throw new Error('Output file is empty or corrupted');
}
const mergedData = await ffmpeg.readFile(outputName);
return new Blob([mergedData], { type: 'video/mp4' }); return new Blob([mergedData], { type: 'video/mp4' });
} catch (error) { } catch (error) {
console.error('Error merging videos:', error); console.error('Error merging videos:', error);
throw new Error(`Failed to merge videos: ${error}`); throw error instanceof Error
? error
: new Error('Unknown error occurred during video merge');
} finally { } finally {
// Clean up temporary files // Clean up temporary files with better error handling
try { const filesToClean = [...fileNames, outputName, 'concat_list.txt'];
for (const fileName of fileNames) {
for (const fileName of filesToClean) {
try {
await ffmpeg.deleteFile(fileName); await ffmpeg.deleteFile(fileName);
} catch (cleanupError) {
// Ignore cleanup errors - they're not critical
console.warn(`Could not delete ${fileName}:`, cleanupError);
} }
await ffmpeg.deleteFile('concat_list.txt');
await ffmpeg.deleteFile(outputName);
} catch (cleanupError) {
console.warn('Error cleaning up temporary files:', cleanupError);
} }
} }
} }