DEV Community

Discussion on: Add Fade In and Fade Out Effects With ffmpeg!

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.