DEV Community

loading...
Cover image for Add Fade In and Fade Out Effects With FFMPEG!

Add Fade In and Fade Out Effects With FFMPEG!

dak425 profile image Donald Feury Updated on ・1 min read

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

Discussion

pic
Editor guide
Collapse
pepa65 profile image
pepa65

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

Collapse
dak425 profile image
Donald Feury Author

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
}
Thread Thread
dak425 profile image
Donald Feury Author

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

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