日本語

How to Add Fade-In and Fade-Out to Video with FFmpeg

​​FFmpeg​AAC,command line,H.264,video

To add a fade-in and fade-out to a video with FFmpeg, use the fade filter for video and the afade filter for audio. This article shows how to fade a video in from black at the start and out to black at the end, and how to raise and lower the BGM volume to match. I measured the brightness of each frame and the loudness every 0.1 seconds to check how the values change.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), and libx264 0.164.3108. The source video was a 5-second file made from FFmpeg's testsrc2: 1280×720, 30fps, yuv420p H.264 video with AAC audio (440Hz tone, 44.1kHz). I did not test videos shot with a camera or smartphone, variable frame rate videos, or crossfades between two videos with xfade.

Fading Video and Audio over the First and Last Second

For the 5-second video, I fade in during the first second and fade out during the last second.

bash
ffmpeg -i main.mp4 \
  -vf "fade=t=in:st=0:d=1,fade=t=out:st=4:d=1" \
  -af "afade=t=in:st=0:d=1,afade=t=out:st=4:d=1" \
  -c:v libx264 -c:a aac out.mp4

t is the type (in or out), st is the start time in seconds, and d is the duration in seconds. For a fade-in and a fade-out, chain two filters of the same kind with ,. According to the fade and afade sections of the FFmpeg filter documentation, after a fade-out the video is filled with the color given by color (black by default), and the audio becomes silent (by default).

The output video was 1280×720, 150 frames, and 5 seconds, the same as the original.

I extracted frames every 0.2 seconds during the fade-in. They changed gradually from black to the original brightness.

Six frames from the test pattern video, arranged in two rows of three. At 0.0 seconds the frame is black. At 0.2, 0.4, 0.6, and 0.8 seconds the color bars get gradually brighter. At 1.0 seconds the frame has the original vivid colors.
Frames from 0 to 1 second of the video with fade=t=in:st=0:d=1

I also checked the rate of change numerically. I measured the average Y (luma) of each frame with the signalstats filter. In the fade filter source code (libavfilter/vf_fade.c) for FFmpeg 6.1.1, black is calculated as Y = 16 for this pixel format (yuv420p). So I computed the ratio to the original video as "(faded Y − 16) ÷ (original Y − 16)".

text
コマ  時刻     元の Y  フェード後の Y  割合
  0  0.0000  125.98   16.00   0.000
 15  0.5000  125.96   70.73   0.498
 30  1.0000  126.03  126.01   1.000
120  4.0000  125.94  125.93   1.000
135  4.5000  125.85   71.22   0.503
149  4.9667  125.92   19.52   0.032

The frame at 0 seconds was fully black (Y = 16). By 0.5 seconds it was about half, and at 1 second it had returned to the original brightness. From 4 seconds the video gets darker again. The frames in between also changed in proportion to time.

I measured loudness as RMS (the average volume, in dB) every 0.1 seconds with the astats filter. The original audio was around −21dB. After the fade, the first 0.1 seconds measured −45.97dB, 0.5–0.6 seconds measured −26.28dB, and 1.0–1.1 seconds measured −21.04dB. The last 0.1 seconds (4.9–5.0 seconds) measured −45.89dB.

When I plotted the video and audio ratios on a graph, both formed the same trapezoid shape. For audio, I converted the dB differences to amplitude ratios, so the values show what fraction of the original loudness each part has.

Graph with time from 0 to 5 seconds on the horizontal axis and the ratio to the original video on the vertical axis. A solid light blue line (video brightness) and a gray dashed line (audio loudness) almost overlap. Both rise linearly from 0% at 0 seconds to 100% at 1 second, then fall from 4 seconds. A pink point marks the last point before 5 seconds, labeled 3.2% for the last frame.
Ratio of video brightness and audio loudness to the original video

The default curve for afade is tri (linear). The video fade also changes brightness in proportion to time. Both are linear, so the graphs overlap.

Calculating the Fade-Out Start Time from the Video Length

The st for a fade-out is "video length minus fade duration." You can get the length with ffprobe. You can calculate it in the shell and pass the result directly.

bash
dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 main.mp4)
st=$(echo "$dur - 1" | bc)
ffmpeg -i main.mp4 -vf "fade=t=in:st=0:d=1,fade=t=out:st=$st:d=1" \
  -af "afade=t=in:st=0:d=1,afade=t=out:st=$st:d=1" -c:v libx264 -c:a aac out.mp4

For this video, dur was 5.000000 and st was 4.000000. The results in the section above were made with this command.

The Last Frame Does Not Become Fully Black

As the table above shows, with st=4:d=1 the ratio of the last frame (frame 149, at 4.9667 seconds) is 0.032, so it is not fully black (Y = 16). In a 5-second video at 30fps, the last frame is at 4.9667 seconds, one frame (about 0.0333 seconds) before 5 seconds. The fade is calculated to finish exactly at 5 seconds, so the last frame keeps a little brightness.

If you want the last frame to be fully black, start the fade one frame earlier. I added only fade=t=out and compared Y for the last four frames.

text
st=4       146: 30.71  147: 27.04  148: 23.36  149: 19.52
st=3.9667  146: 27.04  147: 23.36  148: 19.52  149: 16
st=3.9     146: 19.52  147: 16     148: 16     149: 16

With st=3.9667 (4 − 1/30, written as a decimal), the last frame came out at exactly 16. With st=3.9, the last three frames are fully black. If you want the video to end on black with a short pause, you can start the fade a little earlier.

Frames Before st Stay Black

If the fade-in st is not 0, the frames before it are filled with the color from color. With fade=t=in:st=2:d=1, I got the following.

text
コマ  時刻     元の Y  フェード後の Y
  0  0.0000  125.98   16.00
 59  1.9667  126.01   16.00
 60  2.0000  126.02   16.00
 75  2.5000  125.81   70.65
 90  3.0000  126.06  126.08

From 0 to 2 seconds the video is black, it starts getting brighter at 2 seconds, and it returns to the original brightness at 3 seconds. The documentation also includes an example, fade=t=in:st=5.5:d=0.5, which keeps the first 5.5 seconds black and then fades in over 0.5 seconds. It is useful when you want a pause of black before the video starts.

Without d, the Fade Is Counted in 25 Frames

If you omit d, as in fade=t=in, the fade length is set by a number of frames instead of a time. The frame count nb_frames (n) defaults to 25.

text
コマ  時刻     元の Y  フェード後の Y
 24  0.8000  126.04  121.53
 25  0.8333  126.03  126.03

In a 30fps video, the brightness returned to the original at frame 25 (0.8333 seconds). Since the duration depends on the frame rate, specify d when you want to set the time in seconds. According to the documentation, when both d and n are given, d is used.

Fading In from White

You can fade from a color other than black with color (c).

bash
ffmpeg -i main.mp4 -vf "fade=t=in:st=0:d=1:color=white" -an -c:v libx264 white.mp4

The Y of the frame at 0 seconds was 235. At 0.5 seconds it was 180.38, and at 1 second it was 125.78, which returned to the original brightness (126.03).

When Cutting a Section, Write Times Relative to the Cut

You can cut a section starting partway through with -ss and add a fade at the same time. In this case, write st as the time in the cut video (starting from 0 seconds), not the time in the original video.

bash
ffmpeg -ss 2 -t 2 -i main.mp4 \
  -vf "fade=t=in:st=0:d=0.5,fade=t=out:st=1.5:d=0.5" \
  -af "afade=t=in:st=0:d=0.5,afade=t=out:st=1.5:d=0.5" \
  -c:v libx264 -c:a aac cut.mp4

This produced a 2-second, 60-frame video cut from seconds 2 to 4 of the original. The first frame had Y = 16, Y at 0.5 seconds was 125.85, and the last frame (1.9667 seconds) had Y = 23.38.

When I made the same cut but wrote fade=t=in:st=2:d=0.5, thinking of the time in the original video, all 60 frames of the 2-second video became fully black (Y = 16). In the cut video's timeline, 2 seconds is already past the end.

Changing the Audio Fade Curve

The curve option of afade lets you choose how the loudness changes. The documentation lists many shapes, including tri (linear, the default) and qsin (a quarter-period sine wave). I added a 1-second fade-in to the 440Hz tone and compared the RMS every 0.1 seconds.

text
時刻  tri      qsin
0.0  -45.87   -41.96
0.2  -33.06   -29.38
0.5  -26.26   -23.46
0.8  -22.48   -21.33
1.0  -21.07   -21.07

qsin gets louder sooner and was almost at the original loudness (around −21dB) by 0.8 seconds. The video fade has no such option. Its brightness changes in proportion to time.

Re-encode the Video When Adding a Fade

A fade rewrites the pixel values of each frame, so you cannot add one while using -c:v copy. I combined -vf fade=... with -c:v copy and ran the following command.

bash
ffmpeg -i main.mp4 -vf "fade=t=in:st=0:d=1" -c:v copy out/copy.mp4

It stopped with this error (I extracted only the ffmpeg error lines).

text
[vost#0:0/copy @ 0x559554175a00] Filtergraph 'fade=t=in:st=0:d=1' was specified, but codec copy was selected. Filtering and streamcopy cannot be used together.
Error opening output file out/copy.mp4.
Error opening output files: Function not implemented

Audio works the same way. Combining -af "afade=t=in:st=0:d=1" with -c:a copy stopped with the same error. When you use -af, do not specify -c:a copy. Instead, specify an encoder such as -c:a aac.

​Posted by 86