Ffmpeg 将图像中的视频与视频和音频剪切合并

时间:2021-03-29 12:05:38

标签: node.js audio ffmpeg video-processing fluent-ffmpeg

有一个关于 fluent ffmpeg 的问题,我对它很陌生,所以需要一些帮助。

我正在尝试从 S3 获取图像和视频,以便我可以从中制作完整的视频。 所以我正在用 .loop 制作视频,其中包含来自图像的特定视频长度。 然后我添加了一个带有音频的普通视频。 然后我正在削减音频长度,因此添加的音频将仅在由图像制作的视频上播放。 当与图像中的视频合并的视频开始播放时,它会有自己的音频。 问题是,如果我合并所有视频,我会收到此错误: An error occurred while merging video files: ffmpeg exited with code 1: Cannot find a matching stream for unlabeled input pad 7 on filter Parsed_concat_0.

所以我需要向不是由图像制作的视频添加音频,以便流可以工作。我怎样才能避免这种情况并让该视频拥有自己的音频?我应该添加什么 inputOptions?另外,当我剪切音频时它有延迟,我可以添加哪些音频过滤器?

``

  const videosFromImages = [];
  VIDEO_CONFIG.fragments.forEach((fragment) => {
    videosFromImages.push(function (callback) {
      let ffmpegInstance = ffmpeg(
        `./images/images-with-same-size/${fragment.filename}`
      );

      if (fragment.duration && fragment.type === "image") {
        ffmpegInstance.loop(fragment?.duration);
      }

      if (fragment.type === "image") {
        ffmpegInstance
          .addInput("./audio/audio.mp3")
          .inputOptions(
            "-ss",
            time,
            "-to",
            time + fragment.duration,
            "-async",
            "1"
          );

        time = time + fragment.duration;
        console.log("time:", time);
      }

      ffmpegInstance
        .videoCodec("libx264") // Codec from api
        .videoBitrate("12000k") // Video Quality
        .videoFilters([
          {
            filter: "fade",
            options: "in:0:15",
          },
        ]) // Transitions
        .on("error", function (err) {
          console.error("An error occurred: " + err.message);
        })
        .on("end", function () {
          res.write(`<p>Processing finished for ${fragment.filename}</p>`);
          fragment.filePath = `./output/project-${VIDEO_CONFIG.projectId}/videos/video-${fragment.filename}.avi`;

          callback(null, fragment);
        })
        .save(
          `./output/project-${VIDEO_CONFIG.projectId}/videos/video-${fragment.filename}.avi`
        );
    });
  });

  async.series(videosFromImages, function (err, videosFromImages) {
    // result now equals 'done'
    async.waterfall(
      [
        (done) => {
          console.log("VIDEO_CONFIG", VIDEO_CONFIG.fragments);

          VIDEO_CONFIG.fragments
            .reduce((prev, curr) => prev.input(curr.filePath), ffmpeg())
            .outputFPS(60)
            .on("error", (err) => {
              res.end();
              console.log(
                `An error occurred while merging video files: ${err.message}`
              );
            })
            .on("end", () => {
              res.write("FINAL VIDEO END");
              res.end();
              console.log("end:");
            })
            .mergeToFile(
              `./output/project-${VIDEO_CONFIG.projectId}/final-video-${VIDEO_CONFIG.projectId}.mp4`
            );
          done(null);
        },
      ],
      (err) => {
        if (err) {
          console.log("err:", err);
        }
      }
    );
  });

  res.write("<p>Processing</p>");
} catch (error) {
  console.error("error:", error);
}

``

0 个答案:

没有答案
相关问题