1

I am trying to combine a static image & an audio into a video. I want the video to be as long as the audio file. I am using ffmpeg and am executing it via nodejs:

var args = [
    "-loop",
    "1",
    "-i",
    pathToImage,
    "-i",
    pathToAudio,
    "-c:v",
    "libx264",
    "-tune",
    "stillimage",
    "-b:a",
    "96k",
    "-c:a",
    "aac",
    "-pix_fmt",
    "yuv420p",
    "-f",
    "mp4",
    "-shortest",
    destinationPath,
  ]
  var cmd = "ffmpeg"
  var proc = spawn(cmd, args)

So the ffmpeg command that's executed should be this:

-loop 1 -i pathToImage.png -i pathToAudio.mp3 -c:v libx264 -tune stillimage -b:a 96k -c:a aac -pix_fmt yuv420p -f mp4 -shortest destinationPath.mp4

However, this creates a video which is quite a few seconds longer than my audio file, and I do not understand why that is or how to fix it? I have to admit I do not understand all the parameters 100%, so I am wondering if someone could maybe help me out here.

1

1 Answer 1

2

I don't completely understand this issue myself (something about buffering data apparently), but the Stack Overflow link provided in the comments offers a brief explanation.

Based on the answers provided there, I solved this in my own experimentation by combining the example in Gyan's answer with a sufficiently high input framerate hinted at in Debrune e brune's answer. With your example above, this resulted in:

ffmpeg -y -loop 1 -framerate 10 -i pathToImage.png -i pathToAudio.mp3 -c:v libx264 -tune stillimage -c:a aac -b:a 96k -pix_fmt yuv420p -shortest -fflags +shortest -max_interleave_delta 100M destinationPath.mp4

In this case, anything lower then -framerate 10 resulted in a file that was anywhere from 1-4 seconds too long. However, with the given settings, the resulting .mp4 was exactly the length of the original audio file.

For what it's worth, boosting the framerate higher (in this case) added a small amount of size to the file but otherwise didn't seem to have an effect on length.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .