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.mp4Command Breakdown:
-i input_video.mp4: Specifies the first input (video, index 0).-i input_audio.mp3: Specifies the second input (audio, index 1).-map 0:v:0: Selects the first video stream from the first input.-map 1:a:0: Selects the first audio stream from the second input.-c:v copy: Copies the video stream without re-encoding, saving time and preserving quality.-c:a aac: Encodes the audio to AAC format (required if the audio codec needs to change for the container).-shortest: Tells FFmpeg to stop writing to the output as soon as the shortest input stream ends.
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.mp4Alternatively, 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