I am trying to crop a single video.mp4 file in 9:16 aspect ratio by trying to crop. But I keep getting the following error:
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0
Conversion failed!
Here is my code:
import ffmpegPath from "ffmpeg-static";
import Ffmpeg from "fluent-ffmpeg";
const inputFilePath = 'myfolder/video.mp4';
const outputFilePath = 'myfolder/video-cropped.mp4'
if(ffmpegPath) {
const cropFilter = 'crop=ih*9/16:iw';
Ffmpeg.setFfmpegPath(ffmpegPath);
Ffmpeg(inputFilePath)
.outputOptions(['-filter:v', cropFilter])
.output(outputFilePath)
.on('end', () => console.log('Cropping complete!'))
.on('error', err => console.error('Error while cropping:', err.message))
.run();
}
@Christian Since it has been a long time I worked on this project the solution was not to use
.outputOptions(['-filter:v', cropFilter])
.output(outputFilePath)
since those two caused the issues. Instead I used something called spawn
import { spawn } from 'child_process';
import ffmpegPath from "ffmpeg-static";
const args = [
'-i', `${localPath}/${fileName}.mp4`,
'-filter_complex', `
split [original][copy];
[copy] crop=${targetWidth}:${targetHeight},
scale=720:1280,
gblur=sigma=20[blurred];
[blurred][original]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2
`,
'-c:a', 'copy',
`${localPath}/${fileName}-cropped.mp4`
];
const ffmpegProcess = spawn(ffmpegPath, args);
ffmpegProcess.on('exit', () => {
console.error('\n', 'Video cropping complete', '\n');
});
await new Promise((resolve, reject) => {
ffmpegProcess.on('error', reject);
ffmpegProcess.on('exit', resolve);
});