fluent-ffmpeg / node-fluent-ffmpeg

A fluent API to FFMPEG (http://www.ffmpeg.org)
MIT License
7.97k stars 881 forks source link

How can I process a file multiple times #1207

Open OriX0 opened 1 year ago

OriX0 commented 1 year ago

Version information

Code to reproduce

I need to edit a video multiple times, which means there are multiple startTimes and durations. But I don't know what to do. When I run the following code, I can only handle the first one, and then the processor goes crazy and gets stuck. I think it should be a resource occupation, but I don't know how to make a release.


const videoPath = '1.mp4';
ffmpeg
  .addInput(videoPath)
  .outputOptions("-y")
  .setStartTime("00:00:30")
  .setDuration(10)
  .withVideoCodec("copy")
  .output("outPut1.mp4")
  .on("end", function (err) {
    console.log("end");
    if (!err) {
      console.log("conversion Done");
    }
  })
  .on("error", function (err) {
    console.log("error: ", err);
  })
  .run();

ffmpeg
  .addInput(videoPath)
  .outputOptions("-y")
  .setStartTime("00:00:30")
  .setDuration(10)
  .withVideoCodec("copy")
  .output("outPut1.mp4")
  .on("end", function (err) {
    console.log("end");
    if (!err) {
      console.log("conversion Done");
    }
  })
  .on("error", function (err) {
    console.log("error: ", err);
  })
  .run();

(note: if the problem only happens with some inputs, include a link to such an input file)

Expected results

Normal speed operation

Observed results

the processor goes crazy and gets stuck

Checklist

njoyard commented 1 year ago

Some hints:

A very crude example:

const videoPath = '1.mp4';

const baseCommand = ffmpeg()
  .addInput(videoPath)
  .outputOptions("-y")
  .setDuration(10)
  .withVideoCodec("copy")

const first = baseCommand.clone()
  .setStartTime("00:00:30")
  .output("output1.mp4")

const second = baseCommand.clone()
  .setStartTime("00:01:00")
  .output("output2.mp4")

first
  .on("end", function (err) {
    console.log("first end");
    if (!err) {
      console.log("first conversion Done");
    }

    second
      .on("end", function (err) {
        console.log("second end");
        if (!err) {
          console.log("second conversion Done");
        }
      })
      .on("error", function (err) {
        console.log("second error: ", err);
      })
      .run()
  })
  .on("error", function (err) {
    console.log("first error: ", err);
  })
  .run()