I'm adding audio to a video file using ffmpeg like so
ffmpeg -i videofile.mp4 -i audiofile.wav output.mp4
However this extends the output video file to be the length of the audio file if it is longer than the video. Using -shortest cuts the video file short if the audio file is shorter than the video. So is there a flag to tell ffmpeg to cut the keep the length of the output video to the length of the input video?
Best Answer
-shortest
is what you want.There is no flag to automate this decision.
EDIT
Inspired by @deadcode's answer, I need to make clear, that "no flag to automate" is of course not true, if you are willing to reencode: In this case go with
apad
as suggested by @deadcode.If however you want to avoid reencoding (i.e.
-c:v copy
) the answer stands.There is a workaround using the
ffconcat
demuxer, but it needs a bit of work:.
ffmpeg -i videofile.mp4 -f concat -i audio.ffconcat -c:v copy output.mp4
This will synthesize the
apad
filter without a filter graph, thus allowing a mux without reencoding.