DEV Community

Cover image for Add Fade In and Fade Out Effects With ffmpeg!
Donald Feury
Donald Feury

Posted on • Edited on • Originally published at donaldfeury.xyz

Add Fade In and Fade Out Effects With ffmpeg!

I figured out that you don't need to use a full fledge video editor to apply some basic transition effects to video and audio.

Using ffmpeg, you can apply basic fade in and fade out to video using the fade filter. For audio, the afade filter can be used to achieve a similar effect.

ffmpeg -i video.mp4 -vf "fade=t=in:st=0:d=3" -c:a copy out.mp4
Enter fullscreen mode Exit fullscreen mode

This will start the video with a black screen and fade in to full view over 3 seconds.

ffmpeg -i video.mp4 -vf "fade=t=out:st=10:d=5" -c:a copy out.mp4
Enter fullscreen mode Exit fullscreen mode

This will make the video start fading to black over 5 seconds at the 10 second mark.

ffmpeg -i music.mp3 -af "afade=t=in:st=0:d=5" out.mp3
Enter fullscreen mode Exit fullscreen mode

The audio file will start at a low volume and fade in to full over 5 seconds.

ffmpeg -i music.mp3 -af "afade=t=out:st=5:d=5" out.mp3
Enter fullscreen mode Exit fullscreen mode

The audio file will start fading to zero volume over 5 seconds starting at the 5 second mark.

ffmpeg -i video.mp4 -vf "fade=t=in:st=0:d=10,fade=t=out:st=10:d=5" -c:a copy out.mp4
Enter fullscreen mode Exit fullscreen mode

You can apply both fade in and out effects at the same time, to avoid having to re-encode twice.

Enjoy

Top comments (10)

Collapse
 
pepa65 profile image
pepa65

Can you also combine the video & audio fade at the same time?

Collapse
 
dak425 profile image
Donald Feury • Edited

Sure, just use the afade filter in your filtergraph with the same start and end times as your fade filters.

Check out the documentation for the afade filter if you get confused - ffmpeg.org/ffmpeg-filters.html#afa...

Collapse
 
pepa65 profile image
pepa65

Got it, thanks! I made this bash function to make things easier for myself:

clipvid(){ # $1:source $2:start(ti:me) $3:fadein(s) $4:end(ti:me) $5:fadeout(s)
  usage="Usage: clipvid <sourcevideo> <start(time)> <fadein(s)>"
  usage+=" <end(time)> <fadeout(s)>"
  [[ -z $1 || $1 = -h || $1 = --help ]] &&
    echo $usage && return 0
  [[ ! -f $1 ]] &&
    echo -e "$usage\nSource video not a file: '$1'" && return 1
  [[ ! $2 = [0-9][0-9]:[0-5][0-9]:[0-5][0-9] ]] &&
    echo -e "$usage\nStarttime '$2' not in time format hh:mm:ss" && return 2
  [[ -z $3 || ${3//[0-9]} ]] &&
    echo -e "$usage\nFade-in seconds '$3' not numerical" && return 3
  [[ ! $4 = [0-9][0-9]:[0-5][0-9]:[0-5][0-9] ]] &&
    echo -e "$usage\nEndtime '$4' not in time format hh:mm:ss" && return 4
  [[ -z $5 || ${5//[0-9]} ]] &&
    echo -e "$usage\nFade-out seconds '$5' not numerical" && return 5
  tmp=$(mktemp).mp4
  local -i len=$(date -d $4 +%s)-$(date -d $2 +%s) ce=len-$5
  ffmpeg -i "$1" -ss "$2" -to "$4" -async 1 $tmp
  ffmpeg -i $tmp -vf "fade=t=in:st=0:d=$3,fade=t=out:st=$ce:d=$5" \
    -af "afade=t=in:st=0:d=$3,afade=t=out:st=$ce:d=$5" "$1.mp4"
  rm $tmp
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
dak425 profile image
Donald Feury

Very nice! Your bash-fu is much stronger than mine 😉

Thread Thread
 
asbjornu profile image
Asbjørn Ulsberg • Edited

Great script, thanks for sharing @pepa65! I took the liberty to expand on it a bit and created the following Bash script which will only fade in from the start and out from the end, the amount of seconds specified in the --fade-in and --fade-out arguments, respectively.

Collapse
 
joedotnot profile image
joedotnot

It appears the s=0 in video fade-in filter is redundant? you are starting from the beginning anyway.
-vf "fade=t=in:st=0:d=3"
is the same as
-vf "fade=t=in:d=3"

Collapse
 
dapeng09 profile image
dapeng09

hi, is there a way to change the color from black to white while fading in/out a video?

Collapse
 
dapeng09 profile image
dapeng09

oh, I just figure it out, like below
ffmpeg -i input.mp4 -vf "fade=t=in:st=0:d=3:color=white" -c:a copy out.mp4

Collapse
 
faroncoder profile image
Faron

This is great!

Collapse
 
leigh_stace_d5f457d8f404c profile image
leighski

Great video explaining how to add fades in ffmpeg - how would i use this to add fades to a large batch of clips at the same time?