Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg - output silent audio track if source has no audio or audio is shorter than source video

Tags:

ffmpeg

I need a video file whose audio and video track duration are always the same. The file must contain an audio track even if source audio has no audio track. How do I tell ffmpeg to add a silent audio track when source has no audio trace. Also, if source has an audio track that is a different duration than the video, I need ffmpeg to append silent audio to make both output audio and video the same duration. Is this possible in one line with ffmpeg?

like image 602
TK3 Avatar asked Dec 24 '22 04:12

TK3


1 Answers

The command below will add a silent track of the same length* as the video, if there is no audio** in the source file.

ffmpeg -i video -f lavfi -i anullsrc=cl=1 -shortest -c:v libx264 -c:a aac output.mov

*Since video frame duration and audio frame duration aren't usually identical, the lengths won't be exactly the same.

**when map is not specified, ffmpeg selects a single audio stream from among the inputs with the highest channel count. If there are two or more streams with same no. of channels, it selects stream with lowest index. anullsrc here has one channel, so it will be passed over except when source has no audio.

like image 173
Gyan Avatar answered Dec 28 '22 06:12

Gyan