How to fade in/out a video/audio clip with unknown duration?

I'm working on a project where I'm using ffmpeg to batch convert some video files, and adding fade in/out effects at the head and tail of each clip.

I've been using this bit of code to add the fade/out effect on clips with known durations:

 ffmpeg -i clip.mp4 -filter:v 'fade=in:0:30,fade=out:9650:30' -c:v libx264 -crf 22 -preset veryfast -c:a copy fadeInOut.mp4 

My two questions are:

1. How can I simultaneously fade the audio in/out?
2. Can ffmpeg automatically figure out the duration of the clip and be told just fade out the last 30 frames?

Thanks!

#1 To simultaneously fade the audio in/out:

ffmpeg -i clip.mp4 -vf 'fade=in:0:30,fade=out:960:30'
-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
-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.

• This gives the error "no such filter!" for the audio filter. – felwithe Dec 6 '17 at 16:55
• There are 4 audio filters used. Which one? Run your command with -report added and share the report. – Gyan Dec 6 '17 at 17:18

It's a terrible hack, but this might work if all you want to do is audio fade in/out but don't know exactly how long the clip is:

ffmpeg -i input.mp4 -filter_complex "afade=d=0.5, areverse, afade=d=0.5, areverse" output.mp4


[edit 2019-07-24: Note that this solution is not good for streaming solutions since it requires the full track to be processed before the first byte can be streamed]

another option I is to use acrossfade with a silent track:

ffmpeg -i input.mp4 -filter_complex "aevalsrc=0:d=0.6 [a_silence]; [0:a:0] [a_silence] acrossfade=d=0.6" output.mp4

• This is actually pretty elegant :-) – bk138 Aug 30 '18 at 19:30
• this works great. could you explain how it works. trying to make sense of the documentation.. – oberhamsi Jul 21 '19 at 16:23
• @oberhamsi: Option 1: The first filter adds a fade at the beginning of the track. It then reverse the audio and adds another fade in at the beginning (which is actually the end). Finally the track is reversed again. Option 2 creates a silent track of 0.6 seconds and crossfades it for the same amount of time with the original track. All the filters and their usage are documented here: ffmpeg.org/ffmpeg-filters.html (although I have to agree that it's a bit cryptic) – marekventur Jul 24 '19 at 21:22
• What would be the script to merge Video + Audio (Audio is longer) and have fade in and our for the audio (Let's say Fade In finishes after 2 seconds, Fade out starts at 48 and ends at 51)? – Royi Mar 26 '20 at 1:44

The answer to question 2 is a resounding YES! I was looking for the same functionality and I ended up writing a bash script that asks for fade duration in seconds and calculates the initial frame for the fade-out:

#!/bin/bash

f="$*" # all args p="${f##*/}" # file
fn="${p%.*}" # name only e="${p##*.}" # extension

echo
echo $f echo$p
echo $fn echo$e
echo

read -e -p "Number of seconds of fade-out: " -i 1 sec # prompt for fade duration in seconds

r=$(ffprobe -show_streams "$f" 2> /dev/null | grep r_frame_rate | head -1 | cut -d \= -f 2) # frame rate
let "frames = $r *$sec" # duration of fade as number of frames
echo $r echo$sec
echo $frames lf=$(ffprobe -show_streams "$f" 2> /dev/null | grep nb_frames | head -1 | cut -d \= -f 2) # total number of frames let "lf =$lf - $frames" # initial frame to start fade at ffmpeg -loglevel quiet -i "$f" -vf fade=out:$lf:$frames -r $r -vcodec libx264 -preset slow -crf 12 -bf 2 -flags +cgop -pix_fmt yuv420p -acodec copy "$fn $sec sec fade-out.$e"
open "$fn$sec sec fade-out.\$e"


Fades require re-encoding so using a low crf value for libx264 gives a high quality re-encode. The comments should explain everything else.

• The answer to question 2 is a resounding YES! --> it's still No. The Q is Can ffmpeg automatically figure out the duration... but you've written a script that runs multiple ff* commands to do it. Useful for *nix systems, though. – Gyan May 7 '17 at 4:58
• I see, so you insist that YOUR answer is STILL correct? If you follow my script, you will see that it does indeed automatically figure out the duration using ffprobe, which is installed with ffmpeg. Who said anything about any restriction on the number of commands or *nix? It seems that the OP is using a version of UNIX. No .exe in the ffmpeg command. I myself am using OS X which is built on UNIX but is not UNIX. – hmj6jmh May 7 '17 at 15:32
• * If you follow my script, you will see that it does indeed automatically figure out* --> your script does, ffmpeg does not. – Gyan May 7 '17 at 15:42
• But ffprobe is an integral part of the ffmpeg distribution. You're picking at nits to save face IMO. – hmj6jmh May 7 '17 at 15:47
• I'd have to agree that something being done by a bash script is not something being automatically done by ffmpeg. But if this works, this is a way to work around that problem. Hence the term "workaround". I can't say if it works because I, like most people out there, don't have bash or a unix system. +1 for the effort anyhow. – ashleedawg Dec 3 '18 at 18:45