首页 > 解决方案 > 循环更改视频分辨率

问题描述

我正在尝试将视频的分辨率降低到 500x500 以下。我不想将其更改为 500x500,因为这会影响视频质量。所以我想要做的是在一个循环中将分辨率降低 75%,并且该循环只会在视频低于 500x500 时停止。理论上这并不难,但我似乎无法弄清楚。

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
    }
}

这是我在注释中使用的当前代码。基本上发生的情况是它卡在 while 循环中,因为视频的尺寸不会改变。用console.log()线测试。我认为,如果我能以某种方式解决 ffmpeg 问题,一切都会得到解决。我会很感激任何帮助:)

PS:这都是在打字稿中制作的,然后使用构建到js中npx tsc

标签: javascriptnode.jstypescriptffmpeg

解决方案


问题是循环阻止回调被调用,因为 javascript 在一个线程上运行(在另一个 SO 问题中了解更多信息:Callback of an asynchronous function is never called)。没有被调用的回调之一then是变量vidwidthvidheight被更改位置的回调,因此检查它们是否小于 500 并最终中断循环的条件永远不会true,并且循环会永远运行。无论如何,这不是处理异步函数的正确方法(在另一个 SO 问题中了解更多信息:如何从异步调用返回响应?)。

顺便说一句,copyFile这种while工作根本不需要循环,您可以使用它getDimensions来获取视频的尺寸,根据它们计算所需的尺寸并开始一个ffmpeg任务(ffmpeg将处理结果文件的创建无需更改输入文件,因此无需copyFile)。像这样:

getDimensions(filepath2).then((dimensions: any) => {                            // get the dimension of the input file
  let sizeStr = dimensions.width < dimensions.height ? "?x500" : "500x?";       // if width is smaller than height, reduce the height to 500 and calculate width based on that, same goes for the other way around
  
  ffmpeg(filepath2)                                                             // the input is the original video, don't worry 'ffmpeg' won't alter the input file
    .output('./media/media.mp4')                                                // the output file path
    .size(sizeStr)                                                              // use the 'sizeStr' string calculated previously (read more about it here: https://github.com/fluent-ffmpeg/node-fluent-ffmpeg#video-frame-size-options) 
    .on('end', () => console.log('Finished processing'))
    .run();       
});

就如此容易!


推荐阅读