为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

拉风的咖菲猫 2023-06-15 10:19:33
我有这段代码: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);  });}没有任何效果,我试过只带走音频,尝试更改视频编解码器,尝试输出而不是保存,在承诺之外尝试。我也尝试过不同的路径等。发送请求时,res.send('done')实际上会立即发送,因此清除也没有运行,没有错误,而且我知道函数正在运行,因为我在其中放置了调试语句。 ..依然没有。
查看完整描述

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.

  });


查看完整回答
反对 回复 2023-06-15
  • 1 回答
  • 0 关注
  • 111 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信