0

I am having trouble creating Video from multiple images using fluent-ffmpeg in node.js.

Here, I am getting the images from rquest body and downloading them in **temp **directory

   const imageUrls = req.body.imageUrls;
   const timeInBetween = parseFloat(req.query.time_in_between) || 1.0;

const tempDir = path.join(
      context.executionContext.functionDirectory,
      "temp"
    );

const downloadedImages = await Promise.all(
      imageUrls.map(async (imageUrl, index) => {
        try {
          const response = await axios.get(imageUrl, {
            responseType: "arraybuffer",
          });
          const imageName = `image_${index + 1}.png`;
          const imagePath = path.join(tempDir, imageName);
          await fs.writeFile(imagePath, response.data);
          return imagePath;
        } catch (error) {
          context.log(`Error downloading ${imageUrl}: ${error.message}`);
          return null;
        }
      })
    );

I want to combine these images that are in downloadedImages array and create a video

const outputVideoPath = path.join(tempDir, "output.mp4");

    let ffmpegCommand = ffmpeg();

    for (let i = 0; i < downloadedImages.length; i++) {
      context.log(downloadedImages.length);
      ffmpegCommand
        .input(downloadedImages[i])

        .inputOptions(["-framerate", `1/${timeInBetween}`])
        .inputFormat("image2")
        .videoCodec("libx264")
        .outputOptions(["-pix_fmt", "yuv420p"]);
    }

    ffmpegCommand
      .output(outputVideoPath)
      .on("end", () => {
        context.log("Video generation successful.");
        context.res = {
          status: 200,
          body: "Video generation and cleanup successful.",
        };
      })
      .on("error", (err) => {
        context.log.error("Error generating video:", err.message);
        context.res = {
          status: 500,
          body: "Error generating video: " + err.message,
        };
      })
      .run();

By running it and giving value of "time_in_between" as 2 I get video of 2 seconds with a single image.

  • Utilized Fluent-FFmpeg library to generate a video from a list of downloaded images.
  • Expected the video to include all images, each displayed for a specified duration.
  • Tried mapping through the image paths and using chained inputs for each image.
  • Expected the video to have a sequence of images displayed.
  • Observed that the generated video only contained the first image and was of 0 seconds duration.

0 Answers0