Oniric Forge Media processing,Tutorials Video and audio fade in / fade out

Video and audio fade in / fade out



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

Here is a method that will allow you to do 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/ffmpeg.html

  • 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

Operating system used in this tutorial : macOS Catalina.

Here is one easy way to install ffmpeg on macOS :

Open the Terminal application, and type the following command :

brew install ffmpeg

Steps

  • Let’s get the video duration in seconds, via ffprobe. Sample command (on one line) :
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 my_cool_demo.mp4


Let’s say we get this result :
344.561000

Note the decimal part of the duration. You can bypass the use of ffprobe and determine by yourself the number of seconds, but it might be at the cost of a loss of accuracy.

  • Now that we know its duration, let’s edit the video via a one line command.

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

ffmpeg -i my_cool_demo.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]" output_with_fade_in_and_fade_out.mp4

Note the 340.561000 part (344.561000 – 340.561000 = 4 seconds. This corresponds to the wished fade out duration in this case). Remember to take this into account, in order to get the fade out to work. In particular, In particular, double-check this if there is no sound in the resulting video.

That’s it !

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

ffmpeg -i my_cool_demo.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]" output_with_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_cool_demo.mp4 -filter_complex "[0:v]fade=type=in:duration=1[v];[0:a]afade=type=in:duration=1[a]" -map "[v]" -map "[a]" output_with_fade_in.mp4

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

Enjoy !

Useful links :

FFMPEG official website :

https://ffmpeg.org/ffmpeg.html

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

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

Leave a Reply