I need to merge multiple video files (with included audio) into a single video. I've noticed xfade has been recently released and used it but I am running into an audio sync issue.
All videos are in the same format / resolution / fame and bitrate / etc both for video and audio.
Here is what I am using to merge 5 videos of various durations with 0.5 crossfade transitions:
ffmpeg \
-i v0.mp4 \
-i v1.mp4 \
-i v2.mp4 \
-i v3.mp4 \
-i v4.mp4 \
-filter_complex \
"[0][1]xfade=transition=fade:duration=0.5:offset=3.5[V01]; \
[V01][2]xfade=transition=fade:duration=0.5:offset=32.75[V02]; \
[V02][3]xfade=transition=fade:duration=0.5:offset=67.75[V03]; \
[V03][4]xfade=transition=fade:duration=0.5:offset=98.75[video]; \
[0:a][1:a]acrossfade=d=0.5:c1=tri:c2=tri[A01]; \
[A01][2:a]acrossfade=d=0.5:c1=tri:c2=tri[A02]; \
[A02][3:a]acrossfade=d=0.5:c1=tri:c2=tri[A03]; \
[A03][4:a]acrossfade=d=0.5:c1=tri:c2=tri[audio]" \
-vsync 0 -map "[video]" -map "[audio]" out.mp4
The code above generates a video with audio. The first and second segment is aligned with audio but starting with the second transition the sound is misaligned.
Your offsets are incorrect. Try:
ffmpeg -i v0.mp4 -i v1.mp4 -i v2.mp4 -i v3.mp4 -i v4.mp4 -filter_complex \
"[0][1:v]xfade=transition=fade:duration=1:offset=3[vfade1]; \
[vfade1][2:v]xfade=transition=fade:duration=1:offset=10[vfade2]; \
[vfade2][3:v]xfade=transition=fade:duration=1:offset=21[vfade3]; \
[vfade3][4:v]xfade=transition=fade:duration=1:offset=25,format=yuv420p; \
[0:a][1:a]acrossfade=d=1[afade1]; \
[afade1][2:a]acrossfade=d=1[afade2]; \
[afade2][3:a]acrossfade=d=1[afade3]; \
[afade3][4:a]acrossfade=d=1" \
-movflags +faststart out.mp4
How to get xfade offset
values:
input | input duration | + | previous xfade offset |
- | xfade duration |
offset = |
---|---|---|---|---|---|---|
v0.mp4 |
4 | + | 0 | - | 1 | 3 |
v1.mp4 |
8 | + | 3 | - | 1 | 10 |
v2.mp4 |
12 | + | 10 | - | 1 | 21 |
v3.mp4 |
5 | + | 21 | - | 1 | 25 |
These are simplified example durations that are different than the durations shown in the original question.
ffprobe
.