I am trying to decrease the resolution of a video to under 500x500. I don't want to change it to exactly 500x500 because that would mess up video quality. So what I am trying to do is to decrease the resolution by 75% in a loop, and that loop would only stop when the video is under 500x500. In theory that would not be hard, but I can't seem to figure it out.
var vidwidth = 501; //Create variable and put it to 501
var vidheight = 501; //so that it won't go through the If Statement
fs.copyFile(filepath2, './media/media.mp4', (err: any) => { //Copy given file to directory
console.log('filepath2 was copied to media.mp4'); //Log confirmation (Not appearing for some reason, but file is copied)
})
while (true) {
getDimensions('./media/media.mp4').then(function (dimensions: any) { //Get dimensions of copied video
var vidwidth = parseInt(dimensions.width) //Parse to Int
var vidheight = parseInt(dimensions.height) //and put in variables
})
ffmpeg('./media/media.mp4') //Call ffmpeg function with copied video path
.output('./media/media.mp4') //Set output to the same file so we can loop it
.size('75%') //Reduce resolution by 75%
.on('end', function() { //Log confirmation on end
console.log('Finished processing'); //(Not appearing)
}) //
.run(); //Run function
if (vidwidth < 500 && vidheight < 500) { //Check if both the width and height is under 500px
break; //If true, break the loop and continue
}
}
This is the current code I am using with comments. Basically what happens is it gets stuck in the while loop because the dimensions of the video won't change. Tested with console.log()
line. I think that if I can fix the ffmpeg problem somehow it will all be fixed.
I'd appreciate any help :)
PS: This is all made in typescript, then build into js using npx tsc