日本語

How to Crossfade Two Videos with FFmpeg’s xfade Filter

​​FFmpeg​command line,ffprobe,video

To join two videos with a crossfade in FFmpeg, apply xfade to the video and acrossfade to the audio. This is the command-line equivalent of a "dissolve" or "transition" in a video editor. It works for switching scenes or combining several clips into one file.

For offset, the time where the switch starts, use the length of the first video minus the length of the switch in seconds. If you switch between two 5-second videos over 1 second, the result is 9 seconds long.

Note

Test environment: Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), a virtual machine with 4 CPU cores. The source files are three 5-second MP4s made with FFmpeg (1280×720, 30fps, H.264, yuv420p; audio is AAC 48kHz with tones at 440Hz, 880Hz, and 660Hz). The video is a single solid color (light blue, pink, or gray) with the letters "A", "B", or "C" and the time within each video drawn on it. I did not test camera footage, variable frame rate videos, custom expressions with transition=custom, or the GPU-based xfade_opencl.

Switching between two videos over 1 second

This command crossfades from the 5-second a.mp4 to the 5-second b.mp4 over 1 second.

bash
ffmpeg -i a.mp4 -i b.mp4 -filter_complex \
  "[0:v][1:v]xfade=transition=fade:duration=1:offset=4[v];[0:a][1:a]acrossfade=d=1[a]" \
  -map "[v]" -map "[a]" -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 128k fade.mp4

According to the official xfade documentation, the options mean the following:

  • transition: the type of switch. The default is fade
  • duration: how many seconds the switch takes. The default is 1 second, and values from 0 to 60 seconds are allowed
  • offset: how many seconds from the start of the first input the switch begins. The default is 0

[0:v] is the video of the first input (-i a.mp4), and [1:v] is the video of the second input. xfade is a video-only filter, so the audio is crossfaded separately with acrossfade, and both outputs are included with -map.

This is the result of checking the output with ffprobe.

text
stream|codec_type=video|width=1280|height=720|r_frame_rate=30/1|duration=9.000000|nb_frames=270
stream|codec_type=audio|r_frame_rate=0/0|duration=9.026000|nb_frames=425

The video is 9.000 seconds long, with 270 frames. This is 5 + 5 seconds minus the 1 second that overlaps.

The audio is slightly longer at 9.026 seconds. When I decoded the audio of each original video, each was 5.013 seconds (240,640 samples at 48000Hz). The crossfaded audio contained 433,280 samples, which is exactly 240,640 × 2 minus the 48,000 samples of the 1-second overlap.

The colors blend gradually during the switch

I sampled the color at the upper-left point (x=20, y=20) at each time. At this position, a.mp4 is 0 199 232 (light blue) and b.mp4 is 216 87 119 (pink).

text
t=3.9:    0 199 232
t=4.0:    0 199 232
t=4.1:   20 187 220
t=4.25:   56 169 199
t=4.5:  108 143 175
t=4.75:  163 112 144
t=4.9:  193  99 128
t=5.0:  216  87 119
t=5.1:  216  87 119

Up to 4 seconds the color is from a.mp4, and from 5 seconds it is from b.mp4. In between, it shifts gradually toward pink. The value 108 143 175 at 4.5 seconds is exactly the average of the two colors (108, 143, 175.5).

Another important point is that b.mp4 plays from its own beginning as soon as the switch starts. In the figure below, the 4.5-second frame of the slideleft row shows b.mp4 at the time "00:00:00.500". The second video starts overlapping from its first frame at the time set by offset.

Changing the type of switch

Changing transition produces different effects, such as a blend, a horizontal wipe, a horizontal push, or an expanding circle. In FFmpeg 6.1.1 in the test environment, ffmpeg -h filter=xfade lists 58 types, not counting custom.

I compared frames at 4.2, 4.5, and 4.8 seconds for five of them.

Five rows labeled fade, wipeleft, slideleft, circleopen, and pixelize. Each row shows three frames at 4.2, 4.5, and 4.8 seconds during the switch from the light blue "A" screen to the pink "B" screen. In fade, the two screens overlap and the colors blend. In wipeleft, a pink area spreads from the right. In slideleft, the A screen is pushed to the left. In circleopen, a pink circle expands from the center. In pixelize, the picture turns into mosaic blocks as it switches.
Frames at 4.2, 4.5, and 4.8 seconds for different transition types

With wipeleft, the position of the a.mp4 picture stays the same, and the b.mp4 picture spreads in from the right. With slideleft, the a.mp4 picture itself moves to the left, and the letter "A" moves with it. This is the command that produces the slideleft output.

bash
ffmpeg -i a.mp4 -i b.mp4 -filter_complex \
  "[0:v][1:v]xfade=transition=slideleft:duration=1:offset=4" -an slide.mp4

What happens when offset is wrong

Even with the wrong offset, FFmpeg can produce a video without an error. These are the results from the test environment.

offset value Length of the output video What happened
4 (5 seconds minus 1 second) 9.000 seconds The switch happens at 4 to 5 seconds
2 7.000 seconds The switch happens at 2 to 3 seconds. Nothing of a.mp4 after 3 seconds is used
Not specified (0) 5.000 seconds The switch happens in the first second. a.mp4 appears only during the switch
6 (longer than the first video) 5.067 seconds a.mp4 plays until the end, and b.mp4 never appears

With offset=6, the exit code was 0 and no message was printed. The upper-left color at 1 second, 3 seconds, 4.5 seconds, and the last frame was always the light blue of a.mp4. You can catch these mistakes by checking with ffprobe that the output length equals the first video's length plus the second video's length minus the switch length.

When moving offset earlier, align the audio too

offset is an option of xfade only. As the official documentation describes, acrossfade overlaps at the end of the first audio input. So if you set offset=2 and keep acrossfade=d=1, the video and audio lengths no longer match.

text
stream|codec_type=video|duration=7.000000
stream|codec_type=audio|duration=9.026000

If the video switches at 2 to 3 seconds, cut the first audio at 3 seconds (offset + duration) and then crossfade. Use atrim to cut it.

bash
ffmpeg -i a.mp4 -i b.mp4 -filter_complex \
  "[0:v][1:v]xfade=transition=fade:duration=1:offset=2[v];[0:a]atrim=0:3[a0];[a0][1:a]acrossfade=d=1[a]" \
  -map "[v]" -map "[a]" -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 128k out.mp4

The video is 7.000 seconds and the audio is 7.013 seconds, so the lengths now match. I exported the audio alone and measured the loudness of 440Hz (the tone in a.mp4) and 880Hz (the tone in b.mp4) in 0.2-second segments at 1.5, 2.5, and 4 seconds.

Time 440Hz 880Hz
1.5 seconds -21.6 dB -50.6 dB
2.5 seconds -29.4 dB -25.8 dB
4 seconds -50.5 dB -21.3 dB

Around 2.5 seconds, the two tones are at about the same loudness. At 4 seconds, only 880Hz remains. The audio switch lines up with the video switch at 2 to 3 seconds.

If you do not write d, the length of the acrossfade switch is set by the default nb_samples of 44100 samples (check with ffmpeg -h filter=acrossfade). With 48kHz audio, that is shorter than 1 second. Writing the length in seconds, as in d=1, makes it easier to match the video duration.

Calculating offset when joining three or more clips

To join a third clip, apply xfade again to the output of the first xfade. The second offset is counted from the start of the first output (the joined video so far).

bash
ffmpeg -i a.mp4 -i b.mp4 -i c.mp4 -filter_complex \
  "[0:v][1:v]xfade=transition=fade:duration=1:offset=4[ab];[ab][2:v]xfade=transition=fade:duration=1:offset=8[v];[0:a][1:a]acrossfade=d=1[aba];[aba][2:a]acrossfade=d=1[a]" \
  -map "[v]" -map "[a]" -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 128k abc.mp4

The first output is 9 seconds long, so the second offset is 9 − 1 = 8. The resulting video is 13.000 seconds with 390 frames, and the upper-left color changed as follows.

text
c.mp4:  153 163 169
t=2:    0 199 232
t=4.5:  108 143 175
t=6:  216  87 119
t=8.5:  183 125 142
t=10:  153 163 169

If I set the second offset to 3, intending it as a position within b.mp4 rather than within the joined video, the video becomes 8.000 seconds long. At 4.5 and 6 seconds, the colors were both the gray of c.mp4. The color of b.mp4 never appeared, and no error was shown.

Joining clips of different lengths follows the same logic. The first offset is "length of the first clip − duration". Each following offset is "previous offset + length of the clip that was appended at the end in the previous step − duration", accumulated in order (in the example above, 4 + 5 − 1 = 8). You can check each length with a command such as ffprobe -v error -show_entries format=duration -of csv=p=0 a.mp4.

Videos with different size or timebase cause errors

The official documentation states that both inputs must have a constant frame rate and the same resolution, pixel format, frame rate, and timebase. I changed the settings of b.mp4 and got the following errors, which stopped the process (I omitted the [Parsed_xfade_0 @ …] prefix).

text
First input link main parameters (size 1280x720) do not match the corresponding second input link xfade parameters (size 640x360)
First input link main timebase (1/15360) do not match the corresponding second input link xfade timebase (1/90000)
First input link main frame rate (30/1) do not match the corresponding second input link xfade frame rate (24/1)

The timebase is the unit used to count each frame's time (its timestamp). Videos can have the same size and frame rate but still differ in timebase. In my test, the second error appeared with a b.mp4 that I had re-wrapped with -c copy -video_track_timescale 90000 to change only the container.

The third error, about frame rate, appeared when I forced the timebase of a 24fps video to 1/15360. A 24fps video made only with -r 24 stopped earlier because of its timebase (1/12288).

In each case, matching the setting before xfade fixed the error.

Mismatch Filter added before xfade Result
Size (640×360) scale=1280:720,setsar=1 on the second input 9.000 seconds, 1280×720
Timebase (1/90000) settb=AVTB on both inputs 9.000 seconds, 30fps
Frame rate (24fps) fps=30,settb=AVTB on both inputs 9.100 seconds, 30fps

When I added only settb=AVTB to the videos with a different frame rate, the timebase error went away, but the frame rate error stopped the process. Match the frame rate with fps, then match the timebase with settb. settb=AVTB sets the timebase to AVTB, the value FFmpeg uses by default.

bash
ffmpeg -i a.mp4 -i b_24fps.mp4 -filter_complex \
  "[0:v]fps=30,settb=AVTB[a];[1:v]fps=30,settb=AVTB[b];[a][b]xfade=transition=fade:duration=1:offset=4[v]" \
  -map "[v]" -an out.mp4

The result is 9.100 seconds because the second video, which was 5.083 seconds (122 frames) at 24fps, became 153 frames (5.1 seconds) after fps=30. 4 + 5.1 = 9.1 seconds, so again the length is "up to the switch point in the first video + the whole second video".

​Posted by 86