【发布时间】:2020-11-14 09:06:01
【问题描述】:
我正在尝试将视频的分辨率降低到 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
}
}
这是我与 cmets 一起使用的当前代码。基本上发生的情况是它卡在 while 循环中,因为视频的尺寸不会改变。使用console.log() 行测试。我认为,如果我能以某种方式解决 ffmpeg 问题,一切都会得到解决。
我会很感激任何帮助:)
PS:这都是用typescript制作的,然后用npx tsc构建成js
【问题讨论】:
-
copyFile是异步的,因此在复制完成之前到达循环,getDimensions(...).then也是如此。您不能像这样一个接一个地进行异步调用并期望它们按顺序运行。这基本上是 stackoverflow.com/questions/14220321/… 的副本 -
顺便说一句,你不需要循环也不需要
copyFile这种东西,ffmpeg可以自己处理,我会在稍后发布答案 -
我查看了您链接的线程,它清除了一些东西。不知道 node.js 运行单线程。另外,感谢您尽快发布答案:) 我很感激
-
一个问题,输入视频是正方形的吗(相同的高度和宽度)?如果不是你想要的输出是什么:1. 视频被拉伸以填充
500x500,2. 长宽比保持不变,视频被填充较小边缘上的黑色条纹使其成为500x500或3。 长宽比保持不变,视频不一定是500x500(例如可以是500x400或@987654333 @)? -
我希望视频具有相同的纵横比,但小于 500x500。例如,它是否为 240x360 并不重要,只要它保持相同的纵横比并且低于 500x500。 (基本上你的最后一个选择;))
标签: javascript node.js typescript ffmpeg