How to Use FFmpeg Shortest Option to Mux Audio and Video

When merging audio and video files of different lengths, FFmpeg by default matches the duration of the longest input, which often results in a frozen final frame or a long silence. This article explains how to use the -shortest option in FFmpeg to automatically terminate the output file as soon as the shortest input stream finishes, ensuring a clean and professional mux.

The Basic Command

To combine a video and an audio stream and force the output to match the duration of whichever input is shorter, use the following syntax:

ffmpeg -i input_video.mp4 -i input_audio.mp3 -map 0:v:0 -map 1:a:0 -c:v copy -c:a aac -shortest output.mp4

Command Breakdown:

Troubleshooting and Keyframe Alignment

Because video streams rely on keyframes (I-frames), using -shortest with -c:v copy can sometimes result in an output that is slightly longer than the audio. This happens because FFmpeg cannot cut the video precisely without re-encoding it.

If you require millisecond-perfect alignment, you should re-encode the video instead of copying it:

ffmpeg -i input_video.mp4 -i input_audio.mp3 -map 0:v:0 -map 1:a:0 -c:v libx264 -c:a aac -shortest output.mp4

Alternatively, if you experience issues where the output still does not cut off properly, you can add the -fflags +genpts option to force the generation of presentation timestamps:

ffmpeg -fflags +genpts -i input_video.mp4 -i input_audio.mp3 -map 0:v:0 -map 1:a:0 -c:v copy -c:a aac -shortest output.mp4