gpt4 book ai didi

javascript - Fluent-ffmpeg 不会处理/对视频做任何事情 - Nodejs

转载 作者:行者123 更新时间:2023-12-04 23:11:58 24 4
gpt4 key购买 nike

我有这个代码:

const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path;
const ffmpeg = require("fluent-ffmpeg");
ffmpeg.setFfmpegPath(ffmpegPath);

app.put("/upload-content", async (req, res) => {
// 1. Get video and save locally to the server
const video = req.files.video;
const localTempPath = "./tmp/" + video.name;
video.mv(localTempPath, function (error) {
if (error) return res.send(error);
});
// 2. Convert video and apply settings
processVideo(localTempPath).catch((err) => {
return res.send(err);
});
return res.send("done");
});

function processVideo(localTempPath) {
return new Promise((resolve, reject) => {
ffmpeg(localTempPath)
.withVideoCodec("libx264")
.withSize("630x320")
.withOutputFormat("avi")
.on("error", (error) => reject("Failed to process video: " + error))
.on("progress", (progress) => console.log(progress))
.on("end", resolve("Successfully processed video"))
.saveToFile(localTempPath);
});
}
没有任何效果,我试过只是拿走音频,尝试更改视频编解码器,尝试输出而不是保存,在 promise 之外尝试过。我也尝试过不同的路径等。
发送请求时, res.send('done')实际上会立即发送,因此清除也没有运行,没有错误,并且我知道该函数正在运行,因为我已将调试语句放入其中...仍然没有。

最佳答案

几个问题。你不是在等待 mv 回调。要么让它成为一个 promise ,要么在它的回调之后运行代码。尝试这个。

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();
});;
}
要么使这个函数成为一个 promise ,要么在它被回调后执行。
video.mv(localTempPath, function (error) {
if (error) return res.send(error);
// save file if nothing went wrong. Also wait for processVideo to complete.
});

关于javascript - Fluent-ffmpeg 不会处理/对视频做任何事情 - Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64970038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com