fluent-ffmpeg / node-fluent-ffmpeg

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

try to take screenshot & generate audio file in same time #1231

Closed HassanDev13 closed 1 year ago

HassanDev13 commented 1 year ago

Hi , i am trying to take a screenshot & generate an audio file at the same time, is that possible? here my code :

async function extractMp3AndScreenShot(
  inputFilePath: string,
  outputFilePath: string,
  imageName: string,
  outputImagePath: string
): Promise<void> {
  return new Promise<void>(async (resolve, reject) => {
    let count = 0;
    try {
      await checkAndCreateDirectory(outputFilePath);
      ffmpeg()
        .input(inputFilePath)
        .audioCodec("libmp3lame")
        .toFormat("mp3")
        .on("progress", (progress) => {
          console.log(`Processing: ${progress.percent}% done`);
        })
        .takeScreenshots({
          timestamps: ["50%"],
          filename: imageName,
          folder: outputImagePath,
        })
        .on("end", () => {
          count++;
          if (count === 2) {
            console.log("ffmpeg finished");
            resolve();
          }
        })
        .on("error", (err) => {
          console.error("Error:", err);
          reject(err);
        })
        .save(outputFilePath);
    } catch (error) {
      reject(error);
    }
  });
}

my screenshot sometimes generates sometimes not!

Could someone please help ?

njoyard commented 1 year ago

You can only call one of the processing methods (save, takeScreenshots, or any other method listed in https://github.com/fluent-ffmpeg/node-fluent-ffmpeg#starting-ffmpeg-processing) on a ffmpeg() instance.

Use clone() to create a clone, and then call save on one and takeScreenshots on the other.