Crossfading audio at beginning and ending of video
For purposes of this answer I'll assume the audio is encoded with an aac codec.

For each video:

Use ffmpeg to extract the sound track to its own file.

ffmpeg -i "${f%.mp4}" -vn -acodec copy "${f%-sound.aac}"


If not yet known, determine the duration of the audio file.  In some cases ffmpeg is known to give an incorrect duration (https://stackoverflow.com/questions/10437750/how-to-get-the-real-actual-duration-of-an-mp3-file-vbr-or-cbr-server-side), so I'd use sox.

duration=$(sox soundtrack_before_processing.mp3 -n stat 2&gt;&amp;1 | sed -n 's#^Length (seconds):[^0-9]*\([0-9.]*\)$#\1#p')


Use sox to apply fades in and out.

sox "${f%-sound.aac}" "${f%-sound-with-fades.aac}" fade t $fade_in_seconds $duration $fade_out_seconds


Replace the audio track in the original video file with the faded track.

ffmpeg -y -i "${f%.mp4}" -i "${f%-sound-with-fades.aac}" -map 0:v -map 1 -vcodec copy -acodec aac -strict experimental -shortest "${%f-new.mp4}"