2

I want to merge two video files with a cross-fade effect between them (just about a second long), but without fading to black or white or any colour at all - it has to be a smooth transition from one video to another.

I found this answer to be the most helpful so far - here is my example (based on that solution):

ffmpeg -i e:/ffmpeg/part1.mp4 -i e:/ffmpeg/part2.mp4 -f lavfi -i color=black:s=1920x1080 -filter_complex \
"[0:v]format=pix_fmts=yuva420p,fade=t=out:st=10:d=1:alpha=1,setpts=PTS-STARTPTS[va0]; \
 [1:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+10/TB[va1]; \
 [2:v]trim=duration=18[over]; \
 [over][va0]overlay[over1]; \
 [over1][va1]overlay=format=yuv420[outv]" \
-vcodec libx264 -map [outv] e:/ffmpeg/output.mp4

ffmpeg version N-66278-g91459bd Copyright (c) 2000-2014 the FFmpeg developers
  built on Sep 14 2014 22:14:38 with gcc 4.8.3 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzli
b --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libblu
ray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-
libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrw
b --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr -
-enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --
enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enab
le-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-decklink --enable-zlib
  libavutil      54.  7.100 / 54.  7.100
  libavcodec     56.  1.100 / 56.  1.100
  libavformat    56.  4.101 / 56.  4.101
  libavdevice    56.  0.100 / 56.  0.100
  libavfilter     5.  1.100 /  5.  1.100
  libswscale      3.  0.100 /  3.  0.100
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  0.100 / 53.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'e:/ffmpeg/part1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp41isom
  Duration: 00:00:11.24, start: 0.000000, bitrate: 19916 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9],
 19768 kb/s, 29.65 fps, 30 tbr, 30k tbn, 60k tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : AVC Coding
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 97 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'e:/ffmpeg/part2.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp41isom
  Duration: 00:00:08.23, start: 0.000000, bitrate: 19795 kb/s
    Stream #1:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9],
 19670 kb/s, 29.50 fps, 30 tbr, 30k tbn, 60k tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : AVC Coding
    Stream #1:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 97 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Input #2, lavfi, from 'color=black:s=1920x1080':
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #2:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 25 tbr,
 25 tbn, 25 tbc
[libx264 @ 0000000004330980] using SAR=1/1
[libx264 @ 0000000004330980] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.1 Cache64
[libx264 @ 0000000004330980] profile High, level 4.0
[libx264 @ 0000000004330980] 264 - core 142 r2479 dd79a61 - H.264/MPEG-4 AVC codec - Copyleft 2003-2
014 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=
hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 dead
zone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 deci
mate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 dir
ect=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahe
ad=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'e:/ffmpeg/output.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp41isom
    encoder         : Lavf56.4.101
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1920x1080 [SAR 1:1 DAR 16:
9], q=-1--1, 25 fps, 12800 tbn, 25 tbc (default)
    Metadata:
      encoder         : Lavc56.1.100 libx264
Stream mapping:
  Stream #0:0 (h264) -> format
  Stream #1:0 (h264) -> format
  Stream #2:0 (rawvideo) -> trim
  overlay -> Stream #0:0 (libx264)
Press [q] to stop, [?] for help
frame=  451 fps=4.5 q=-1.0 Lsize=   11032kB time=00:00:17.96 bitrate=5031.9kbits/s dup=0 drop=7
video:11028kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.036493%

[libx264 @ 0000000004330980] frame I:11    Avg QP:20.72  size: 63930
[libx264 @ 0000000004330980] frame P:368   Avg QP:23.84  size: 26880
[libx264 @ 0000000004330980] frame B:72    Avg QP:25.29  size:  9679
[libx264 @ 0000000004330980] consecutive B-frames: 69.0% 28.8%  1.3%  0.9%
[libx264 @ 0000000004330980] mb I  I16..4: 16.7% 70.0% 13.3%
[libx264 @ 0000000004330980] mb P  I16..4:  4.6% 10.7%  0.5%  P16..4: 56.9%  7.8%  5.4%  0.0%  0.0%
   skip:14.2%
[libx264 @ 0000000004330980] mb B  I16..4:  0.2%  0.4%  0.0%  B16..8: 42.9%  1.8%  0.2%  direct: 3.3
%  skip:51.3%  L0:39.3% L1:59.7% BI: 0.9%
[libx264 @ 0000000004330980] 8x8 transform intra:68.1% inter:89.7%
[libx264 @ 0000000004330980] coded y,uvDC,uvAC intra: 43.7% 66.3% 4.2% inter: 21.4% 52.4% 0.0%
[libx264 @ 0000000004330980] i16 v,h,dc,p: 38% 17% 10% 35%
[libx264 @ 0000000004330980] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 29% 16% 28%  4%  5%  5%  5%  5%  4%
[libx264 @ 0000000004330980] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 35% 23% 15%  4%  6%  5%  5%  4%  3%
[libx264 @ 0000000004330980] i8c dc,h,v,p: 51% 18% 25%  6%
[libx264 @ 0000000004330980] Weighted P-Frames: Y:3.5% UV:2.7%
[libx264 @ 0000000004330980] ref P L0: 76.4%  9.8% 10.4%  3.3%  0.1%
[libx264 @ 0000000004330980] ref B L0: 91.4%  8.5%  0.0%
[libx264 @ 0000000004330980] ref B L1: 99.6%  0.4%
[libx264 @ 0000000004330980] kb/s:5007.46

The problem is that there is no sound in the output video.

I am looking for something that will join two videos without removing the audio. I could join the audio separately using another software (like e.g. Audacity) eventually, but if there is a way to do all of this in FFmpeg, then I'd like to know.

I'm sorry if that's a lame question, but I'm not too familiar with video filters or video encoding at all. I just found FFmpeg to work best for me (usually I use it only for file conversions) and I tried to join two videos with the transition effect, but none of the solution I've found actually worked.

  • 1
    Could you provide the console output of the command? – Rajib Jan 5 '15 at 19:58
  • I've figured out the problem with the wrong apsect ratio (see my editet post), but I still don't know how to do all this with audio... Here is a pastebin with the full console output. – pafflick Jan 8 '15 at 16:56
  • You want audio to crossfade as well? – Rajib Jan 8 '15 at 19:02
  • Well, I'll be glad if there was any audio, because sof far the output video is just silent. – pafflick Jan 8 '15 at 19:04
2

For audio cross-fading you can use something like this.

ffmpeg -i input_video_1 -i input_video_2 -filter_complex "
[0:a]afade=t=out:st=9:d=2[a0];
[1:a]afade=t=in:st=0:d=2[a1];
aevalsrc=0:d=9[s1];
[s1][a1]concat=n=2:v=0:a=1[ac1];
[a0][ac1]amix[a]" -map [a] -c:a libfdk_aac -ac 2 -b:a 128k output_audio

Here this will cross-fade the audio from two video files and output a audio file. I have attached a silent duration which is same as the duration of the first audio to the beginning of the second video and mixed both at the end. This way you can avoid audio overlapping when mixing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.