1
votes

I have the following command line I use with ffmpeg to add an audio track to a video that doesn't have any audio track.

ffmpeg -i final.mp4 -i soundtrack.mp3 -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 -shortest with_audio.mp4

I am looking for a way to fade out the volume nicely at 3s from the end of the video. The soundtrack will be always longer than the video, but the video can be any length. Existing command lines I found require to specify exact timing when to start/end fade effect. I am looking for a way that is agnostic to video length, I just want the sound to fade out to start 3s before video ends.

Is there any way to do this with ffmpeg?

Many thanks in advance for answers!

1

1 Answers

2
votes

Use ffprobe to get duration.

ffprobe -v error -select_streams v:0 -show_entries stream=duration -of csv=p=0 input.mp4

Then use the duration as a variable in your ffmpeg command.

ffmpeg -i input.mp4 -i soundtrack.mp3 -filter_complex "[1:a]afade=t=out:st=$(bc <<< "$duration-$fade"):d=$fade[a]" -map 0:v:0 -map "[a]" -c:v copy -c:a aac -shortest with_audio.mp4