0

I have this code:

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);
  });
}

Nothing works, I have tried just taking away audio, tried changing the video codec, tried outputting instead of saving, tried it outside a promise. I've also tried different Paths etc. When the request is sent, the res.send('done') practically gets sent straight away so it clearing isn't running either, there's no errors and I know the function is getting run as I have put debug statements inside it...still nothing.

Nathan
  • 1,393
  • 1
  • 9
  • 40

1 Answers1

1

Couple of issues. You are not waiting for the mv callback. Either make it a promise as well or run the code after its callback. Try this.

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();
});;
}

Either make this function a promise or execute after its called back.

video.mv(localTempPath, function (error) {
      if (error) return res.send(error);
 // save file if nothing went wrong. Also wait for processVideo to complete.
  });
kg99
  • 746
  • 2
  • 14
  • I tried both your verison, and the promise verison I made and it still does not work. No errors or outputs. I will upadte my answer with the two new updates in a second – Nathan Nov 23 '20 at 14:33
  • What happens. Do you get an error? Are there any logs from the processVideo function. You need to be specific. – kg99 Nov 23 '20 at 14:35
  • I stated that theres not erros or logs on any functions in the first comment...I've added debug logs and they are running, so the function is running – Nathan Nov 23 '20 at 14:36
  • That means you have an issue with fluent-ffmpeg. What type of video are you using? – kg99 Nov 23 '20 at 14:37
  • How would you debug ffmpeg when its not stating theres an error? – Nathan Nov 23 '20 at 14:38
  • This should work. 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('end', function() { console.log('Finished processing'); }) .run(); }); – kg99 Nov 23 '20 at 14:49
  • Could you stick that in your answer please? – Nathan Nov 23 '20 at 14:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224986/discussion-between-kg99-and-nathan). – kg99 Nov 23 '20 at 14:50