Thursday, April 20, 2017

FFMpeg Fade Controls -- Fade to Black

A pretty common for the open scenes of a video to 'fade in' and 'fade out' at the ending of the video.  Ffmpeg provides a video filter to easily apply fading in/out, the subject of this post.  Saddle up and prepare to be amazed.

Let's start with a simple 30-second video as our input:
video

Fade In

The video fade-in filter is of the form fade=in:<startFrame><endFrame>, simply specifying a start and stop reference, the fading will begin at the start point and complete at the stop point.  The units, unfortunately, are frame numbers rather than time (e.g. seconds).

Given the video is 24 fps, we can fade into the video in the first 5 seconds by issuing the following command:
$ ffmpeg -y -i opening.mp4 -vf fade=in:0:120 -acodec copy fadeIn-5sec.mp4
video

Fading in the first 10 seconds by:
$ ffmpeg -y -i opening.mp4 -vf fade=in:0:240 -acodec copy fadeIn-10sec.mp4
video

Fading in the first 20 seconds by:
$ ffmpeg -y -i opening.mp4 -vf fade=in:0:480 -acodec copy fadeIn-20sec.mp4
video

Fade Out

The video fade-out filter is of the form fade=out:<startFrame><fade duration>, simply specifying a start and stop reference, the fading will begin at the start point and within the specified duration.  The units, unfortunately, are frames rather than time (e.g. seconds).

Fading out the last 10 seconds is a bit trickier, primarily because of the frame units.  In order to specify the start/duration references we need to know the number of frames in the video and the fps.

Frame count can be determined by:
$ ffprobe -v error -count_frames -select_streams v:0   -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1  opening.mp4
719
10sec = 240 frames

Starting reference would be 719-240 (24fps * 10sec) = 479:
$ ffmpeg -y -i opening.mp4 -vf fade=out:479:240 -acodec copy fadeOut-10sec.mp4
video

Fade In/Out

You can chain fade in and out affects, for instance to fade in the first 10 seconds and fade out the last 10 seconds:
$ ffmpeg -y -i opening.mp4 -vf fade=in:0:240,fade=out:479:240 -acodec copy fadeInOut.mp4
video

Now, go forth and use these tricks responsibly.

Cheers

No comments:

Post a Comment