1 回答

TA貢獻1810條經驗 獲得超5個贊
幾個問題。您不是在等待 mv 回調。要么也讓它成為一個承諾,要么在它的回調之后運行代碼。嘗試這個。
const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path;
const ffmpeg = require("fluent-ffmpeg");
ffmpeg.setFfmpegPath(ffmpegPath);
app.put("/upload-content", async(req, res) => {
try {
// 1. Get video and save locally to the server
const video = req.files.video;
const localTempPath = "./tmp/" + video.name;
video.mv(localTempPath, async function(error) {
if (error) return res.send(error);
const resp = await processVideo(localTempPath);
return res.send("done");
});
} catch (err) {
return res.send(error);
}
});
function processVideo(localTempPath) {
return new Promise((resolve, reject) => {
ffmpeg()
.input(localTempPath)
.withVideoCodec("libx264")
.withSize("630x320")
.withOutputFormat("avi")
.on("error", (error) => reject("Failed to process video: " + error))
.output(newpath)
.on("progress", (progress) => console.log(progress))
.on('end', function() {
console.log('Finished processing');
})
.run();
});;
}
要么讓這個函數成為一個承諾,要么在它回調后執行。
video.mv(localTempPath, function (error) {
if (error) return res.send(error);
// save file if nothing went wrong. Also wait for processVideo to complete.
});
添加回答
舉報