Why is fluent-ffmpeg not concatenating my files?

I’m attempting to create a single MP4 file out of many MP3 and JPG files. But when I run the code, it either creates an empty MP4 file or one that only has the audio from the original MP3 file. Despite numerous code modifications, the desired result is still elusive. Is there anyone who can pinpoint the problem with my strategy?

Here is the code,

const path = require("path");
  const ffmpeg = require("fluent-ffmpeg");

  let newMp4 = ffmpeg();
  newMp4
    .input(`${dirPath}/darkpatio.jpg`)
    .input(`${dirPath}/backgarden.jpg`)
    .input(`${dirPath}/sound_000000_NARRATOR.mp3`)
    .input(`${dirPath}/sound_000001_NARRATOR.mp3`)
    .input(`${dirPath}/sound_000002_NARRATOR.mp3`)
    .save(`${dirPath}/temp.mp4`)
    .outputFPS(12)
    .frames(2)
    .on("end", () => {
      console.log("done");

2 Likes

Hey Roman,

I see you’ve been attempting to use fluent-ffmpeg to combine MP3 and JPG files into an MP4 video, but you’ve run into problems where the final product is either empty or only contains the sound from the initial MP3 file.
Let’s troubleshoot your code and make it better:

const path = require("path");
const ffmpeg = require("fluent-ffmpeg");

const dirPath = "your_directory_path"; // Replace this with your actual directory path

const newMp4 = ffmpeg()
  .input(`${dirPath}/darkpatio.jpg`)
  .input(`${dirPath}/backgarden.jpg`)
  .input(`${dirPath}/sound_000000_NARRATOR.mp3`)
  .input(`${dirPath}/sound_000001_NARRATOR.mp3`)
  .input(`${dirPath}/sound_000002_NARRATOR.mp3`)
  .inputOptions("-framerate 12")
  .inputOptions("-t 2") // Set the video duration (2 seconds in this example)
  .videoCodec("libx264") // Specify the video codec
  .audioCodec("aac") // Specify the audio codec
  .on("end", () => {
    console.log("Video creation done");
  })
  .on("error", (err) => {
    console.error("Error during video creation:", err);
  })
  .save(`${dirPath}/output.mp4`);

The following are the significant additions and clarifications:

  1. The directory where your files are placed is represented by the dirPath variable I introduced. Please substitute the correct path for “your_directory_path”.

  2. I used inputOptions to specify input options in order to fix the problems. This pertains to the frame rate as well as the length of the video, which in this case must have 2 seconds of material. If necessary, alter the time frame.

  3. To guarantee compatibility and appropriate encoding, the video codec is set to libx264, and the audio codec is set to aac.

  4. To catch and report any mistakes that can happen when making the video, I added error handling using.on("error").

These improvements should enable your code to generate an MP4 video file that contains the graphics as well as the concatenated audio from the MP3 files. Verify that FFmpeg is properly installed on your computer and reachable from the Node.js environment.

Find more here: