#1 To simultaneously fade the audio in/out:
ffmpeg -i clip.mp4 -vf 'fade=in:0:30,fade=out:960:30'
-af 'afade=in:st=0:d=1,afade=out:st=32:d=1'
-c:v libx264 -crf 22 -preset veryfast fadeInOut.mp4
The afade times are in seconds.
#2 Automatically? No. But see workaround below
You can first run ffprobe to get duration.
ffprobe -i clip.mp4 -show_entries stream=codec_type,duration -of compact=p=0:nk=1
You'll get something like this:
video|13.556000
audio|13.816000
You can then use the above to place your fades. These times are in seconds.
Workaround
ffmpeg -i clip.mp4 -sseof -1 -copyts -i clip.mp4 -filter_complex
"[1]fade=out:0:30[t];[0][t]overlay,fade=in:0:30[v];
anullsrc,atrim=0:2[at];[0][at]acrossfade=d=1,afade=d=1[a]"
-map "[v]" -map "[a]" -c:v libx264 -crf 22 -preset veryfast -shortest fadeInOut.mp4
FFmpeg has a sseof
option that allows one to seek an input from the end. We can use that to accomplish our goal. So we feed the input twice, with the 2nd time ingesting only the last second. We tell FFmpeg to preserve the timestamps, so that ffmpeg preserves the temporal position of this tail portion.
We apply a fade out to this tail and then overlay the result onto the full input. Since they are the same media file, the foreground completely covers the background, and since copyts
was applied, the overlay happens upon the corresponding identical frame in the background input.
For audio, we create a blank dummy audio of duration 2 seconds, and then apply an audio crossfade from the main audio to this dummy audio. Since the 2nd audio is blank, this is, in effect, a fade-out for the main input. The -shortest
is added to leave out portions of the dummy audio after the crossfade has occurred.