How to add an audio that is shorter than the video in FFmpeg

Mixing videos and audio files with FFMPEG is kind of easy as long as you have the instruction to do it (or you are patient enough to read the official documentation of FFMPEG). But there's a problem you may encounter but that probably you won't find an immediate solution, consider the following example of mixing 2 videos in a single one. The problem with this situation is that the first video contains the stream of a student, whose video and audio tracks have different lengths (basically because when the stream was recorded in the server, the audio track, and video are separate files, and when the student hangs up, the audio transmission stops, however, there are some frames of the video that are recorded):

Mix Audio Video Track FFMPEG

In this case, as you can see our video of 3 minutes doesn't have an audio track attached, but the one we are trying to add of 2 minutes and 54 seconds, so the audio is way shorter than the video! When joining these 2 with some formats like OPUS for audio and WebM for video, you won't be able to join them if they have different lengths.

In this article, I will explain to you how to join a video track and shorter audio.

If the video doesn't have any audio track

If just like in our example, the video doesn't have an audio track, the command to use can be breakdown like this:

  • -i: the first input argument corresponds to the video track that doesn't have audio.
  • -i: the second input argument is the audio that is shorter than the video.
  • -af apad: we are applying the apad filter, which pads the end of an audio stream with silence. Used together with -shortest to extend audio streams to the same length as the video stream.
  • As final positional argument the output video.

In our case, with a video in WebM format, we'll add an opus audio file like this:

ffmpeg -i ./video-track.webm -i ./audio-track.opus -af apad -shortest ./output-video.webm

As mentioned, this option will work only if the video that you are processing doesn't have any audio track, if it does, you will need to replace the audio stream with some extra options that you'll find in the next section.

If the video has an audio track

In some cases, the video that you will want to join with the new audio will have an audio stream, even though if it's a silenced audio track for example when exported with tools like Vegas Pro 17, so what you will try to do in this case is to replace the audio stream:

FFmpeg mix audio and video streams

To proceed in this case, you will need to map the audio source to use the new one instead of the current one using the map option. For the video and audio codecs, we will just copy them, so for the mapping, we will use the video source from the first input and the audio source of the second input:

ffmpeg -i ./video-track.mp4 -i ./audio-track.mp3 -c:v copy -c:a copy -map 0:v:0 -map 1:a:0 ./output-video.mp4

If you need to change the start position of the audio, for example, instead of leaving the pad at the end of the video, but at the beginning:

FFmpeg mix audio and video streams at specific time location

You need to specify the position with the option --ss that will seek in this input file to the given position, in our case we want our audio track to start at the second 6 so the padding will be at the beginning:

ffmpeg -i ./video-track.mp4 -i ./audio-track.mp3 -ss 00:00:06 -c:v copy -c:a copy -map 0:v:0 -map 1:a:0 ./output-video.mp4

Happy coding ❤️!

This could interest you

Become a more social person