Video and Audio fade in / fade out



Let’s say you need to apply a Video and Audio fade in of 1 second, as well as a Video and Audio fade out of 4 seconds, to a given video.

Below I suggest you a method that will allow you to achieve this, regardless of the duration of the video.

Prerequisites

This method requires the use of two command line tools :

  • ffmpeg, a powerful tool for manipulating videos, images and audio.

More info on the following website : https://ffmpeg.org

  • ffprobe, which allows you to display media information.

More info here : https://ffmpeg.org/ffprobe.html

Note that ffprobe is part of ffmpeg.

There are several ways to install ffmpeg, depending on your operating system.

Here’s a useful link for installing it on main operating systems :

https://trac.ffmpeg.org/wiki/CompilationGuide

An easy way to install ffmpeg on macOS is the following one :

  • Install homebrew. More info at https://brew.sh.
  • Then, install ffmpeg as follows :

Open the Terminal application, and type the following command :

brew install ffmpeg

Steps fo follow

In this example, for the sake of simplicity, we will work in one folder, including a source video named my_video.mp4, and in which the processed video will be named my_video_fade_in_fade_out.mp4.

Let’s say the path of this folder is :

/Users/johndoe/Desktop/my_videos

(where johndoe is your macOS session username)

Via the Terminal you go to this folder using the following command :

cd /Users/johndoe/Desktop/my_videos

In order to be able to make sure that the video and audio fade out effects start at the right time, we’ll need to get the video duration, via ffprobe. Single line sample command :

ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1 my_video.mp4

Let’s say we get this result :

duration=344.561000

This is the video’s duration, expressed in seconds with a high accuracy (seconds and microseconds).

We need this value to be able to get the precise fade out start time, which must be included in the final command, in this specific format.

Knowing that in this case we want a fade out of 4 seconds, both for the video and for the audio, we just need to do a little subtraction :

344.561000 – 4 = 340.561000

So, 340.561000 is the exact value ffmpeg will need to apply the desired fade out effect so that it begins at the appropriate time.

And now, let’s process the video !

Single line sample command to apply a 1 second video and audio fade in, and a 4 seconds video and audio fade out :

ffmpeg -i my_video.mp4 -filter_complex "[0:v] fade = type = in : duration = 1, fade = type = out : duration = 4 : start_time = 340.561000 [v]; [0:a] afade = type = in : duration = 1, afade = type = out : duration = 4 : start_time = 340.561000 [a];" -map "[v]" -map "[a]" my_video_fade_in_fade_out.mp4

Note : The parameters between the [0:v] and [v] tags are related to the video part, and the ones between the [0:a] and [a] tags correspond to the audio part. It is therefore quite possible to define different fade in and fade out durations for the video and audio parts.

That’s it !

In case you just want to apply a fade out, here’s a sample command to apply a 4 seconds video and audio fade out :

ffmpeg -i my_video.mp4 -filter_complex "[0:v] fade = type = out : duration = 4 : start_time = 340.561000 [v]; [0:a] afade = type = out : duration = 4 : start_time = 340.561000 [a];" -map "[v]" -map "[a]" my_video_fade_out.mp4

If you only need to apply a fade in, no need to use ffprobe in this case. Here’s a sample command to apply a 1 second video and audio fade in :

ffmpeg -i my_video.mp4 -filter_complex "[0:v] fade = type = in : duration = 1 [v]; [0:a] afade = type = in : duration = 1 [a];" -map "[v]" -map "[a]" my_video_fade_in.mp4

Enjoy !


Best Regards.

Franck Mallouk, aka Dj Franck Goss.

Franck Mallouk

www.oniricforge.com

Oniric Forge Whatsapp Community


Useful links related to his tutorial :


  • FFmpeg official website :

https://ffmpeg.org

  • FFmpeg Commands: 31 Must-Haves for Beginners in 2022 :

https://www.videoproc.com/resource/ffmpeg-commands.htm