亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號