我在aws lambda函数中调用了三个块,所以我想一一执行调用代码块,应该等待执行时间 可以看到我的代码
在此我提到了第一块,第二块和第三块 我想在运行第二个块之后执行我的第一个块代码,然后应该执行第三个块。
我从不想使用setTimeout函数,我认为这是不正确的。
'use strict';
const ffmpeg = require("fluent-ffmpeg");
const finalVideoPath = __dirname + "/bin/video.mp4";
const margeVideoPath = __dirname + "/video/videofinal.mp4";
const Jimp = require("jimp");
const frames = [
__dirname + "/images/FullSizeRender-1.jpg",
__dirname + "/images/FullSizeRender-2.jpg",
__dirname + "/images/FullSizeRender-3.jpg",
__dirname + "/images/FullSizeRender-4.jpg",
__dirname + "/images/FullSizeRender-5.jpg",
__dirname + "/images/FullSizeRender-6.jpg",
__dirname + "/images/FullSizeRender-7.jpg",
__dirname + "/images/FullSizeRender-8.jpg",
__dirname + "/images/FullSizeRender-9.jpg",
__dirname + "/images/FullSizeRender-10.jpg",
__dirname + "/images/FullSizeRender-11.jpg",
__dirname + "/images/FullSizeRender-12.jpg",
__dirname + "/images/FullSizeRender-13.jpg",
__dirname + "/images/FullSizeRender-14.jpg",
__dirname + "/images/FullSizeRender-15.jpg",
__dirname + "/images/FullSizeRender-16.jpg",
];
module.exports.hello = async function
(event, context, callback) {
return new Promise(function (resolve, reject) {
/* First block*/
//resize images
frames.forEach(element => {
Jimp.read(element, (err, lenna) => {
if (err) console.log('error', err);
lenna
.resize(1000, 1000) // resize
.quality(10) // set JPEG quality
.write(element); // save
});
console.log('resize');
});
/* second block*/
//create video
setTimeout(() => {
ffmpeg().addInput(__dirname + "/images/FullSizeRender-%1d.jpg")
.complexFilter([{
filter: "zoompan",
options: {
x: "iw/2-(iw/zoom/2)",
y: "ih/2-(ih/zoom/2)",
z: "if(lte(zoom,1.0),1.5,max(1.001,zoom-0.0040))",
d: "125",
s: "200x200"
}
}])
.addInputOption(["-framerate 20", ])
.addOutputOption(["-c:v libx264", "-r 30", "-pix_fmt yuv420p"])
.addOption("-crf", 26)
.format("mp4")
.videoBitrate("1024k")
.videoCodec("mpeg4")
.on("error", function (er) {
console.log("error occured: " + er.message, er);
reject('er.message');
})
.on("end", function (data) {
console.log("created video has been completed");
}).save(finalVideoPath);
}, 10000);
/* thitrd block*/
// marge video
setTimeout(() => {
new ffmpeg({
source: finalVideoPath,
nolog: true,
})
.mergeAdd(__dirname + "/video/video.mp4")
.mergeAdd(__dirname + "/video/video2.mp4")
.mergeToFile(margeVideoPath, function () {
console.log("marge video");
resolve();
});
}, 20000);
})
};