I have a nodejs script which encodes/converts multiple video files using the fluent-ffmpeg package. Since FFmpeg is very resource intensive my Linux server runs out of resources and my scripts start throwing an error. This is because all the videos are encoded concurrently.
Is there a way to encode the videos one at a time and not concurrently?
My code snippet looks like this:
const Ffmpeg = require(“fluent-ffmpeg”);
videos.forEach(video=> {
covertVideo(filename, (compressionResult)=>{
//do something here
});
});
function covertVideo(filename, callback){
var ffmpeg = new FfmpegCommand(filename);
ffmpeg.setFfmpegPath(pathToFfmpeg);
ffmpeg.addOptions(“-threads 1”)
.fps(23)
.complexFilter([
“scale=w=‘if(gt(a,0.75),240,trunc(320*a/2)*2)’:h=‘if(lt(a,0.75),320,trunc(240/a/2)*2)’,pad=w=240:h=320:x=‘if(gt(a,0.75),0,(240-iw)/2)’:y=‘if(lt(a,0.75),0,(320-ih)/2)’:color=black[scaled]”
])
.on(‘error’, function(err) {
console.log('An error occurred: ’ + err.message);
return callback(false);
})
.on(‘end’, function() {
console.log(‘Compression finished !’);
return callback(true);
})
.save(“./tmp/”+filename);
}
Hey!
You will have to make the function convertVideo return a promise which gets resolved by ffmegs end callback. This would allow the iterator to await the response from the convertVideo function which in turn would process the videos one at a time and thus manage the resource usage.
Great! works for me, Thanks.