<blockquote>
  <p>Is there any way to automate position for crossfade (last second of the video)?</p>
</blockquote>

<p>Not with <code>ffmpeg</code> alone (yet at least–there is a patch that never got applied that would simplify this). You can use  <code>ffprobe</code> to get the duration of each input and use that to determine the values for <code>setpts</code> and <code>fade</code>.</p>

<p>Example <code>ffprobe</code> command:</p>

<pre><code>ffprobe -v error -show_entries format=duration -of csv=p=0 input.mp4
</code></pre>

<blockquote>
  <p>Is there any use for PREV_OUTPTS or PREV_INPTS? I didn't manage to get them work.</p>
</blockquote>

<p>No. These do not refer to previous input/output streams or files.</p>

<blockquote>
  <p>Can this effect be done with stream copy? There is no use for encoding the stream again.</p>
</blockquote>

<p>No. You can't filter and stream copy the same stream.</p>

CC BY-SA 4.0
2020-01-27T21:30:03.423Z
Thank you for answer and noting the use of ffprobe. Maybe in some time I'll manage to write script to automate this for me. By the way - will complex filters setpts and fade work with float numbers, for example 442.224 seconds?
Alexander Y.
CC BY-SA 4.0
2020-01-28T19:04:00.493Z
@AlexanderY. Yes, but such accuracy can become moot due to the frame rate.
llogan
CC BY-SA 4.0
2020-01-28T19:44:30.457Z
Thanks. Will experiment more.
Alexander Y.
CC BY-SA 4.0
2020-01-27T18:55:24.070Z
2020-01-27T18:55:24.070Z
llogan
<p>Sorry if my question is dumb, but I'm new to FFmpeg.</p>

<p>I have some videos. They all have the same video codec, resolution and framerate. Audio streams also have the same format. For example, MP4 files, H264, AAC.</p>

<p>I want to merge some of them into one file with smooth transitions between them. I just want the simplest one - when videos overlaps for 2-3 seconds. The first one fades out and the second fades in.</p>

<p>I read the docs and some other questions and found out that I need some set of <code>concat</code> filter with something like <code>blend</code> or <code>overlay</code> or <code>fade</code>.</p>

<p>Please help.</p>

<p><strong>Update</strong>:</p>

<pre><code>ffmpeg \
 -i input0.mp4 \
 -i input1.mp4 \
 -filter_complex \
 "[0:v]setpts=PTS-STARTPTS[v0];[1:v]setpts=PTS-STARTPTS+4/TB,format=yuva444p,fade=st=4:d=1:t=in:alpha=1[v1];[v0][v1]overlay,format=yuv420p[v];[0:a]asetpts=PTS-STARTPTS[a0];[1:a]asetpts=PTS-STARTPTS[a1];[a0][a1]acrossfade=d=1[a]" \
 -map "[v]" -map "[a]" output.mp4
</code></pre>

<p>Log:</p>

<pre><code>ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
  configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input0.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2019-10-20T09:15:30.000000Z
    com.android.version: 8.0.0
  Duration: 00:00:23.81, start: 0.000000, bitrate: 17280 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 17023 kb/s, SAR 1:1 DAR 16:9, 30.04 fps, 30 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2019-10-20T09:15:30.000000Z
      handler_name    : VideoHandle
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 256 kb/s (default)
    Metadata:
      creation_time   : 2019-10-20T09:15:30.000000Z
      handler_name    : SoundHandle
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'input1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2019-10-20T09:16:05.000000Z
    com.android.version: 8.0.0
  Duration: 00:00:28.76, start: 0.000000, bitrate: 17282 kb/s
    Stream #1:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 17019 kb/s, SAR 1:1 DAR 16:9, 30.04 fps, 30 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2019-10-20T09:16:05.000000Z
      handler_name    : VideoHandle
    Stream #1:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 256 kb/s (default)
    Metadata:
      creation_time   : 2019-10-20T09:16:05.000000Z
      handler_name    : SoundHandle
Stream mapping:
  Stream #0:0 (h264) -&gt; setpts
  Stream #0:1 (aac) -&gt; asetpts
  Stream #1:0 (h264) -&gt; setpts
  Stream #1:1 (aac) -&gt; asetpts
  format -&gt; Stream #0:0 (libx264)
  acrossfade -&gt; Stream #0:1 (aac)
Press [q] to stop, [?] for help
[libx264 @ 0x55dd4a513aa0] using SAR=1/1
[libx264 @ 0x55dd4a513aa0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x55dd4a513aa0] profile High, level 4.0
[libx264 @ 0x55dd4a513aa0] 264 - core 152 r2854 e9a5903 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - 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 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=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 'output.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    com.android.version: 8.0.0
    encoder         : Lavf57.83.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
    Metadata:
      encoder         : Lavc57.107.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      encoder         : Lavc57.107.100 aac
Past duration 0.600319 too large   24832kB time=00:00:27.11 bitrate=7502.4kbits/s speed=0.792x    
Past duration 0.601662 too large
Past duration 0.602989 too large
Past duration 0.603325 too large   25344kB time=00:00:27.11 bitrate=7657.1kbits/s speed=0.78x    
Past duration 0.605995 too large
Past duration 0.605659 too large
Past duration 0.608330 too large
Past duration 0.609993 too large   25856kB time=00:00:28.11 bitrate=7533.2kbits/s speed=0.797x    
Past duration 0.609657 too large
Past duration 0.612663 too large
Past duration 0.613991 too large
Past duration 0.615654 too large
Past duration 0.616997 too large
Past duration 0.617989 too large
Past duration 0.618324 too large
Past duration 0.620995 too large   26368kB time=00:00:29.12 bitrate=7417.8kbits/s speed=0.812x    
Past duration 0.622322 too large
Past duration 0.623665 too large
Past duration 0.623329 too large
Past duration 0.626320 too large
Past duration 0.627327 too large
Past duration 0.628990 too large
Past duration 0.630333 too large
Past duration 0.631325 too large   26880kB time=00:00:29.12 bitrate=7561.9kbits/s speed=0.798x    
Past duration 0.633324 too large
Past duration 0.634666 too large
Past duration 0.634331 too large
Past duration 0.637657 too large
Past duration 0.638664 too large
Past duration 0.639656 too large
Past duration 0.641319 too large   27392kB time=00:00:29.12 bitrate=7705.9kbits/s speed=0.785x    
Past duration 0.642662 too large
Past duration 0.643654 too large
Past duration 0.645653 too large
Past duration 0.646660 too large
Past duration 0.647987 too large
Past duration 0.649330 too large
Past duration 0.650993 too large
Past duration 0.652321 too large   27904kB time=00:00:30.12 bitrate=7588.6kbits/s speed= 0.8x    
Past duration 0.651985 too large
Past duration 0.654991 too large
    Last message repeated 1 times
Past duration 0.657326 too large
Past duration 0.658653 too large
Past duration 0.658989 too large
Past duration 0.661995 too large
Past duration 0.662987 too large   28416kB time=00:00:31.12 bitrate=7478.9kbits/s speed=0.813x    
Past duration 0.664330 too large
    Last message repeated 1 times
Past duration 0.665657 too large
Past duration 0.668663 too large
Past duration 0.667992 too large
Past duration 0.669655 too large
Past duration 0.672661 too large   28928kB time=00:00:31.12 bitrate=7613.7kbits/s speed=0.802x    
Past duration 0.671989 too large
Past duration 0.675331 too large
    Last message repeated 1 times
Past duration 0.676659 too large
Past duration 0.678322 too large   29440kB time=00:00:31.12 bitrate=7748.4kbits/s speed=0.792x    
Past duration 0.678658 too large
Past duration 0.679985 too large
Past duration 0.682655 too large
Past duration 0.683998 too large
Past duration 0.685326 too large
Past duration 0.687325 too large   29952kB time=00:00:31.12 bitrate=7883.2kbits/s speed=0.781x    
Past duration 0.687996 too large
Past duration 0.689323 too large
    Last message repeated 1 times
Past duration 0.692665 too large
Past duration 0.693321 too large
Past duration 0.694328 too large
Past duration 0.695000 too large   30464kB time=00:00:32.12 bitrate=7767.7kbits/s speed=0.796x    
Past duration 0.695656 too large
Past duration 0.698326 too large
Past duration 0.700996 too large
    Last message repeated 1 times
Past duration 0.702324 too large
Past duration 0.705330 too large
Past duration 0.704994 too large   30976kB time=00:00:33.13 bitrate=7659.2kbits/s speed=0.81x    
Past duration 0.706657 too large
Past duration 0.708321 too large
Past duration 0.708656 too large
Past duration 0.710655 too large
Past duration 0.711998 too large
Past duration 0.714653 too large
Past duration 0.715996 too large   31488kB time=00:00:33.13 bitrate=7785.8kbits/s speed=0.799x    
    Last message repeated 1 times
Past duration 0.717659 too large
Past duration 0.720329 too large
    Last message repeated 1 times
Past duration 0.723000 too large
Past duration 0.724327 too large
Past duration 0.725655 too large
Past duration 0.725655 too large   32000kB time=00:00:33.13 bitrate=7912.4kbits/s speed=0.788x    
Past duration 0.726997 too large
Past duration 0.727989 too large
Past duration 0.729652 too large
Past duration 0.732323 too large
    Last message repeated 1 times
Past duration 0.734993 too large
Past duration 0.736320 too large
Past duration 0.736320 too large   32512kB time=00:00:34.13 bitrate=7802.9kbits/s speed=0.802x    
Past duration 0.739326 too large
Past duration 0.740654 too large
Past duration 0.740318 too large
Past duration 0.742653 too large
Past duration 0.744987 too large
Past duration 0.746330 too large
Past duration 0.747658 too large   33024kB time=00:00:35.13 bitrate=7699.6kbits/s speed=0.814x    
Past duration 0.748985 too large
Past duration 0.748329 too large
Past duration 0.751656 too large
Past duration 0.753319 too large
Past duration 0.752663 too large
Past duration 0.755653 too large
Past duration 0.756996 too large
Past duration 0.756996 too large   33536kB time=00:00:35.13 bitrate=7819.0kbits/s speed=0.802x    
Past duration 0.759666 too large
    Last message repeated 1 times
Past duration 0.760994 too large
Past duration 0.763664 too large
Past duration 0.763664 too large   33792kB time=00:00:35.13 bitrate=7878.7kbits/s speed=0.792x    
Past duration 0.764992 too large
Past duration 0.767662 too large
    Last message repeated 1 times
Past duration 0.768654 too large   34048kB time=00:00:35.13 bitrate=7938.3kbits/s speed=0.783x    
Past duration 0.771660 too large
Past duration 0.773323 too large
Past duration 0.771996 too large
Past duration 0.774330 too large
Past duration 0.776329 too large
Past duration 0.776665 too large
Past duration 0.779655 too large   34560kB time=00:00:36.13 bitrate=7834.2kbits/s speed=0.795x    
Past duration 0.780663 too large
Past duration 0.782326 too large
Past duration 0.783989 too large
Past duration 0.784996 too large
Past duration 0.785988 too large
Past duration 0.787666 too large
Past duration 0.788994 too large
Past duration 0.788994 too large   35072kB time=00:00:37.14 bitrate=7735.6kbits/s speed=0.806x    
Past duration 0.792320 too large
Past duration 0.793663 too large
Past duration 0.794991 too large
Past duration 0.796333 too large
Past duration 0.797661 too large
Past duration 0.798988 too large
Past duration 0.800652 too large
Past duration 0.800331 too large   35328kB time=00:00:37.14 bitrate=7792.1kbits/s speed=0.794x    
Past duration 0.802666 too large
Past duration 0.804329 too large
    Last message repeated 1 times
Past duration 0.807320 too large
Past duration 0.808662 too large
Past duration 0.810326 too large
Past duration 0.811653 too large
Past duration 0.812996 too large   35840kB time=00:00:37.14 bitrate=7905.0kbits/s speed=0.783x    
    Last message repeated 1 times
Past duration 0.815331 too large
Past duration 0.816994 too large
Past duration 0.818321 too large
Past duration 0.819328 too large
Past duration 0.820992 too large
Past duration 0.821999 too large
Past duration 0.822319 too large   36352kB time=00:00:38.14 bitrate=7807.2kbits/s speed=0.795x    
Past duration 0.824989 too large
Past duration 0.825996 too large
Past duration 0.826332 too large
Past duration 0.828987 too large
    Last message repeated 1 times
Past duration 0.830330 too large
Past duration 0.833321 too large
Past duration 0.832985 too large   36864kB time=00:00:39.14 bitrate=7714.3kbits/s speed=0.806x    
Past duration 0.835991 too large
Past duration 0.836998 too large
Past duration 0.837318 too large
Past duration 0.838661 too large
Past duration 0.841331 too large
Past duration 0.840996 too large
Past duration 0.843666 too large
Past duration 0.845329 too large   37120kB time=00:00:39.14 bitrate=7767.9kbits/s speed=0.796x    
Past duration 0.846657 too large
Past duration 0.847664 too large
Past duration 0.849327 too large
Past duration 0.849998 too large
Past duration 0.850990 too large
Past duration 0.853325 too large
Past duration 0.853661 too large
Past duration 0.855995 too large   37632kB time=00:00:39.14 bitrate=7875.0kbits/s speed=0.786x    
Past duration 0.857658 too large
Past duration 0.858986 too large
Past duration 0.860329 too large
Past duration 0.861656 too large
Past duration 0.863319 too large
Past duration 0.864326 too large
Past duration 0.865990 too large
Past duration 0.867332 too large   38144kB time=00:00:40.17 bitrate=7778.7kbits/s speed=0.796x    
Past duration 0.865990 too large
Past duration 0.869331 too large
Past duration 0.869987 too large
Past duration 0.871666 too large
Past duration 0.873665 too large
Past duration 0.872993 too large
Past duration 0.875328 too large
Past duration 0.877998 too large   38656kB time=00:00:41.15 bitrate=7695.1kbits/s speed=0.807x    
Past duration 0.878319 too large
Past duration 0.878654 too large
Past duration 0.881996 too large
Past duration 0.882652 too large
Past duration 0.884987 too large
Past duration 0.885323 too large
Past duration 0.887321 too large
Past duration 0.887657 too large
Past duration 0.889992 too large   39168kB time=00:00:41.15 bitrate=7797.1kbits/s speed=0.798x    
    Last message repeated 1 times
Past duration 0.891655 too large
Past duration 0.893990 too large
Past duration 0.895332 too large
    Last message repeated 1 times
Past duration 0.897987 too large
Past duration 0.899666 too large
Past duration 0.900993 too large   39680kB time=00:00:41.15 bitrate=7899.0kbits/s speed=0.79x    
Past duration 0.902657 too large
Past duration 0.903999 too large
Past duration 0.904991 too large
Past duration 0.905327 too large
Past duration 0.906654 too large
Past duration 0.907997 too large
Past duration 0.910652 too large
Past duration 0.910988 too large   40192kB time=00:00:42.85 bitrate=7682.3kbits/s speed=0.814x    
Past duration 0.913322 too large
frame=  985 fps= 13 q=-1.0 Lsize=   60323kB time=00:00:51.58 bitrate=9579.9kbits/s speed=0.695x    
video:59484kB audio:810kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.048264%
[libx264 @ 0x55dd4a513aa0] frame I:5     Avg QP:22.90  size:206885
[libx264 @ 0x55dd4a513aa0] frame P:303   Avg QP:25.09  size:100886
[libx264 @ 0x55dd4a513aa0] frame B:677   Avg QP:27.85  size: 43292
[libx264 @ 0x55dd4a513aa0] consecutive B-frames:  6.3%  4.3%  5.8% 83.7%
[libx264 @ 0x55dd4a513aa0] mb I  I16..4: 11.1% 49.1% 39.8%
[libx264 @ 0x55dd4a513aa0] mb P  I16..4:  6.5% 14.1%  9.5%  P16..4: 37.5% 16.2%  8.2%  0.0%  0.0%    skip: 8.0%
[libx264 @ 0x55dd4a513aa0] mb B  I16..4:  1.5%  1.6%  3.3%  B16..8: 48.1%  8.4%  2.2%  direct: 4.6%  skip:30.3%  L0:52.3% L1:34.9% BI:12.8%
[libx264 @ 0x55dd4a513aa0] 8x8 transform intra:40.2% inter:46.8%
[libx264 @ 0x55dd4a513aa0] coded y,uvDC,uvAC intra: 63.8% 25.9% 2.2% inter: 27.2% 7.1% 0.0%
[libx264 @ 0x55dd4a513aa0] i16 v,h,dc,p: 20% 25% 25% 30%
[libx264 @ 0x55dd4a513aa0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 26% 25%  4%  4%  4%  6%  3%  8%
[libx264 @ 0x55dd4a513aa0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 12% 27% 18%  6%  6%  4% 10%  4% 13%
[libx264 @ 0x55dd4a513aa0] i8c dc,h,v,p: 73% 14% 11%  2%
[libx264 @ 0x55dd4a513aa0] Weighted P-Frames: Y:18.8% UV:5.9%
[libx264 @ 0x55dd4a513aa0] ref P L0: 67.5% 18.8% 10.3%  3.0%  0.5%
[libx264 @ 0x55dd4a513aa0] ref B L0: 96.9%  2.4%  0.7%
[libx264 @ 0x55dd4a513aa0] ref B L1: 98.4%  1.6%
[libx264 @ 0x55dd4a513aa0] kb/s:14841.31
[aac @ 0x55dd4a4b21c0] Qavg: 496.588
</code></pre>

<p><strong>Update 2</strong>: 
As I was told, the effect I was searching for is Crossfade. 
I've found these: </p>

<ul>
<li><a href="https://stackoverflow.com/questions/37783044/ffmpeg-command-for-crossfading-between-5-videos-how-to-manage-setpts-pts-startp">FFmpeg command for crossfading between 5 videos. How to manage setpts=PTS-STARTPTS?</a>, </li>
<li><a href="https://video.stackexchange.com/questions/17502/concate-two-video-file-with-fade-effect-with-ffmpeg-in-linux/17504#17504">Concate two video file with fade effect with ffmpeg in linux</a></li>
<li><a href="https://stackoverflow.com/questions/43333542/what-is-video-timescale-timebase-or-timestamp-in-ffmpeg/43337235#43337235">What is video timescale, timebase, or timestamp in ffmpeg?</a> </li>
</ul>

<p>After some experiments I've finally got the working command for my purpose:</p>

<pre><code>[0:v]setpts=PTS-STARTPTS[v0];
[1:v]setpts=PTS-STARTPTS+(23/TB),format=yuva444p,fade=st=23:d=1:t=in:alpha=1[v1];[v0][v1]overlay,format=yuv420p[v];
[0:a]asetpts=PTS-STARTPTS[a0];
[1:a]asetpts=PTS-STARTPTS[a1];
[a0][a1]acrossfade=d=1[a]
</code></pre>

<p>It works, but I have to set times for crossfade for every pair of videos manually</p>

<p>So what I want to ask again:</p>

<ul>
<li>Is there any way to automate position for crossfade (last second of the video)? Is there any use for PREV_OUTPTS or PREV_INPTS? I didn't manage to get them work.</li>
<li>Can this effect be done with stream copy? There is no use for encoding the stream again.</li>
</ul>

CC BY-SA 4.0
2020-01-10T20:30:04.050Z
Adapt the examples in https://superuser.com/a/834035/
llogan
CC BY-SA 4.0
2020-01-13T21:32:09.817Z
I used an example for crossfade with the complex filter like this for video: `[0:v]setpts=PTS-STARTPTS[v0];[1:v]setpts=PTS-STARTPTS+4/TB,format=yuva444p,fade=st=4:d=1:t=in:alpha=1[v1];`. But this results in output with 4 seconds from the first video then crossfade and complete second video. I suppose something should be done with this `[1:v]setpts=PTS-STARTPTS+4/TB` and this `fade=st=4:` but didn't figure out the correct result
Alexander Y.
CC BY-SA 4.0
2020-01-13T21:32:48.510Z
[Edit] your question to include your full command and the complete log.
llogan
CC BY-SA 4.0
2020-01-26T19:05:26.793Z
@llogan, I've updated my question
Alexander Y.
<ffmpeg>
How to merge videos with FFmpeg with smooth transitions
Alexander Y.
# How to merge videos with FFmpeg with smooth transitions

(Asked by: username_0 on 2020-01-10)

Sorry if my question is dumb, but I'm new to FFmpeg.
I have some videos. They all have the same video codec, resolution and framerate. Audio streams also have the same format. For example, MP4 files, H264, AAC.
I want to merge some of them into one file with smooth transitions between them. I just want the simplest one - when videos overlaps for 2-3 seconds. The first one fades out and the second fades in.
I read the docs and some other questions and found out that I need some set of concat filter with something like blend or overlay or fade.
Please help.
Update:
ffmpeg \
 -i input0.mp4 \
 -i input1.mp4 \
 -filter_complex \
 "[0:v]setpts=PTS-STARTPTS[v0];[1:v]setpts=PTS-STARTPTS+4/TB,format=yuva444p,fade=st=4:d=1:t=in:alpha=1[v1];[v0][v1]overlay,format=yuv420p[v];[0:a]asetpts=PTS-STARTPTS[a0];[1:a]asetpts=PTS-STARTPTS[a1];[a0][a1]acrossfade=d=1[a]" \
 -map "[v]" -map "[a]" output.mp4

Log:
ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
  configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input0.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2019-10-20T09:15:30.000000Z
    com.android.version: 8.0.0
  Duration: 00:00:23.81, start: 0.000000, bitrate: 17280 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 17023 kb/s, SAR 1:1 DAR 16:9, 30.04 fps, 30 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2019-10-20T09:15:30.000000Z
      handler_name    : VideoHandle
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 256 kb/s (default)
    Metadata:
      creation_time   : 2019-10-20T09:15:30.000000Z
      handler_name    : SoundHandle
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'input1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2019-10-20T09:16:05.000000Z
    com.android.version: 8.0.0
  Duration: 00:00:28.76, start: 0.000000, bitrate: 17282 kb/s
    Stream #1:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 17019 kb/s, SAR 1:1 DAR 16:9, 30.04 fps, 30 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2019-10-20T09:16:05.000000Z
      handler_name    : VideoHandle
    Stream #1:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 256 kb/s (default)
    Metadata:
      creation_time   : 2019-10-20T09:16:05.000000Z
      handler_name    : SoundHandle
Stream mapping:
  Stream #0:0 (h264) -> setpts
  Stream #0:1 (aac) -> asetpts
  Stream #1:0 (h264) -> setpts
  Stream #1:1 (aac) -> asetpts
  format -> Stream #0:0 (libx264)
  acrossfade -> Stream #0:1 (aac)
Press [q] to stop, [?] for help
[libx264 @ 0x55dd4a513aa0] using SAR=1/1
[libx264 @ 0x55dd4a513aa0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x55dd4a513aa0] profile High, level 4.0
[libx264 @ 0x55dd4a513aa0] 264 - core 152 r2854 e9a5903 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - 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 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=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 'output.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    com.android.version: 8.0.0
    encoder         : Lavf57.83.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
    Metadata:
      encoder         : Lavc57.107.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      encoder         : Lavc57.107.100 aac
Past duration 0.600319 too large   24832kB time=00:00:27.11 bitrate=7502.4kbits/s speed=0.792x    
Past duration 0.601662 too large
Past duration 0.602989 too large
Past duration 0.603325 too large   25344kB time=00:00:27.11 bitrate=7657.1kbits/s speed=0.78x    
Past duration 0.605995 too large
Past duration 0.605659 too large
Past duration 0.608330 too large
Past duration 0.609993 too large   25856kB time=00:00:28.11 bitrate=7533.2kbits/s speed=0.797x    
Past duration 0.609657 too large
Past duration 0.612663 too large
Past duration 0.613991 too large
Past duration 0.615654 too large
Past duration 0.616997 too large
Past duration 0.617989 too large
Past duration 0.618324 too large
Past duration 0.620995 too large   26368kB time=00:00:29.12 bitrate=7417.8kbits/s speed=0.812x    
Past duration 0.622322 too large
Past duration 0.623665 too large
Past duration 0.623329 too large
Past duration 0.626320 too large
Past duration 0.627327 too large
Past duration 0.628990 too large
Past duration 0.630333 too large
Past duration 0.631325 too large   26880kB time=00:00:29.12 bitrate=7561.9kbits/s speed=0.798x    
Past duration 0.633324 too large
Past duration 0.634666 too large
Past duration 0.634331 too large
Past duration 0.637657 too large
Past duration 0.638664 too large
Past duration 0.639656 too large
Past duration 0.641319 too large   27392kB time=00:00:29.12 bitrate=7705.9kbits/s speed=0.785x    
Past duration 0.642662 too large
Past duration 0.643654 too large
Past duration 0.645653 too large
Past duration 0.646660 too large
Past duration 0.647987 too large
Past duration 0.649330 too large
Past duration 0.650993 too large
Past duration 0.652321 too large   27904kB time=00:00:30.12 bitrate=7588.6kbits/s speed= 0.8x    
Past duration 0.651985 too large
Past duration 0.654991 too large
    Last message repeated 1 times
Past duration 0.657326 too large
Past duration 0.658653 too large
Past duration 0.658989 too large
Past duration 0.661995 too large
Past duration 0.662987 too large   28416kB time=00:00:31.12 bitrate=7478.9kbits/s speed=0.813x    
Past duration 0.664330 too large
    Last message repeated 1 times
Past duration 0.665657 too large
Past duration 0.668663 too large
Past duration 0.667992 too large
Past duration 0.669655 too large
Past duration 0.672661 too large   28928kB time=00:00:31.12 bitrate=7613.7kbits/s speed=0.802x    
Past duration 0.671989 too large
Past duration 0.675331 too large
    Last message repeated 1 times
Past duration 0.676659 too large
Past duration 0.678322 too large   29440kB time=00:00:31.12 bitrate=7748.4kbits/s speed=0.792x    
Past duration 0.678658 too large
Past duration 0.679985 too large
Past duration 0.682655 too large
Past duration 0.683998 too large
Past duration 0.685326 too large
Past duration 0.687325 too large   29952kB time=00:00:31.12 bitrate=7883.2kbits/s speed=0.781x    
Past duration 0.687996 too large
Past duration 0.689323 too large
    Last message repeated 1 times
Past duration 0.692665 too large
Past duration 0.693321 too large
Past duration 0.694328 too large
Past duration 0.695000 too large   30464kB time=00:00:32.12 bitrate=7767.7kbits/s speed=0.796x    
Past duration 0.695656 too large
Past duration 0.698326 too large
Past duration 0.700996 too large
    Last message repeated 1 times
Past duration 0.702324 too large
Past duration 0.705330 too large
Past duration 0.704994 too large   30976kB time=00:00:33.13 bitrate=7659.2kbits/s speed=0.81x    
Past duration 0.706657 too large
Past duration 0.708321 too large
Past duration 0.708656 too large
Past duration 0.710655 too large
Past duration 0.711998 too large
Past duration 0.714653 too large
Past duration 0.715996 too large   31488kB time=00:00:33.13 bitrate=7785.8kbits/s speed=0.799x    
    Last message repeated 1 times
Past duration 0.717659 too large
Past duration 0.720329 too large
    Last message repeated 1 times
Past duration 0.723000 too large
Past duration 0.724327 too large
Past duration 0.725655 too large
Past duration 0.725655 too large   32000kB time=00:00:33.13 bitrate=7912.4kbits/s speed=0.788x    
Past duration 0.726997 too large
Past duration 0.727989 too large
Past duration 0.729652 too large
Past duration 0.732323 too large
    Last message repeated 1 times
Past duration 0.734993 too large
Past duration 0.736320 too large
Past duration 0.736320 too large   32512kB time=00:00:34.13 bitrate=7802.9kbits/s speed=0.802x    
Past duration 0.739326 too large
Past duration 0.740654 too large
Past duration 0.740318 too large
Past duration 0.742653 too large
Past duration 0.744987 too large
Past duration 0.746330 too large
Past duration 0.747658 too large   33024kB time=00:00:35.13 bitrate=7699.6kbits/s speed=0.814x    
Past duration 0.748985 too large
Past duration 0.748329 too large
Past duration 0.751656 too large
Past duration 0.753319 too large
Past duration 0.752663 too large
Past duration 0.755653 too large
Past duration 0.756996 too large
Past duration 0.756996 too large   33536kB time=00:00:35.13 bitrate=7819.0kbits/s speed=0.802x    
Past duration 0.759666 too large
    Last message repeated 1 times
Past duration 0.760994 too large
Past duration 0.763664 too large
Past duration 0.763664 too large   33792kB time=00:00:35.13 bitrate=7878.7kbits/s speed=0.792x    
Past duration 0.764992 too large
Past duration 0.767662 too large
    Last message repeated 1 times
Past duration 0.768654 too large   34048kB time=00:00:35.13 bitrate=7938.3kbits/s speed=0.783x    
Past duration 0.771660 too large
Past duration 0.773323 too large
Past duration 0.771996 too large
Past duration 0.774330 too large
Past duration 0.776329 too large
Past duration 0.776665 too large
Past duration 0.779655 too large   34560kB time=00:00:36.13 bitrate=7834.2kbits/s speed=0.795x    
Past duration 0.780663 too large
Past duration 0.782326 too large
Past duration 0.783989 too large
Past duration 0.784996 too large
Past duration 0.785988 too large
Past duration 0.787666 too large
Past duration 0.788994 too large
Past duration 0.788994 too large   35072kB time=00:00:37.14 bitrate=7735.6kbits/s speed=0.806x    
Past duration 0.792320 too large
Past duration 0.793663 too large
Past duration 0.794991 too large
Past duration 0.796333 too large
Past duration 0.797661 too large
Past duration 0.798988 too large
Past duration 0.800652 too large
Past duration 0.800331 too large   35328kB time=00:00:37.14 bitrate=7792.1kbits/s speed=0.794x    
Past duration 0.802666 too large
Past duration 0.804329 too large
    Last message repeated 1 times
Past duration 0.807320 too large
Past duration 0.808662 too large
Past duration 0.810326 too large
Past duration 0.811653 too large
Past duration 0.812996 too large   35840kB time=00:00:37.14 bitrate=7905.0kbits/s speed=0.783x    
    Last message repeated 1 times
Past duration 0.815331 too large
Past duration 0.816994 too large
Past duration 0.818321 too large
Past duration 0.819328 too large
Past duration 0.820992 too large
Past duration 0.821999 too large
Past duration 0.822319 too large   36352kB time=00:00:38.14 bitrate=7807.2kbits/s speed=0.795x    
Past duration 0.824989 too large
Past duration 0.825996 too large
Past duration 0.826332 too large
Past duration 0.828987 too large
    Last message repeated 1 times
Past duration 0.830330 too large
Past duration 0.833321 too large
Past duration 0.832985 too large   36864kB time=00:00:39.14 bitrate=7714.3kbits/s speed=0.806x    
Past duration 0.835991 too large
Past duration 0.836998 too large
Past duration 0.837318 too large
Past duration 0.838661 too large
Past duration 0.841331 too large
Past duration 0.840996 too large
Past duration 0.843666 too large
Past duration 0.845329 too large   37120kB time=00:00:39.14 bitrate=7767.9kbits/s speed=0.796x    
Past duration 0.846657 too large
Past duration 0.847664 too large
Past duration 0.849327 too large
Past duration 0.849998 too large
Past duration 0.850990 too large
Past duration 0.853325 too large
Past duration 0.853661 too large
Past duration 0.855995 too large   37632kB time=00:00:39.14 bitrate=7875.0kbits/s speed=0.786x    
Past duration 0.857658 too large
Past duration 0.858986 too large
Past duration 0.860329 too large
Past duration 0.861656 too large
Past duration 0.863319 too large
Past duration 0.864326 too large
Past duration 0.865990 too large
Past duration 0.867332 too large   38144kB time=00:00:40.17 bitrate=7778.7kbits/s speed=0.796x    
Past duration 0.865990 too large
Past duration 0.869331 too large
Past duration 0.869987 too large
Past duration 0.871666 too large
Past duration 0.873665 too large
Past duration 0.872993 too large
Past duration 0.875328 too large
Past duration 0.877998 too large   38656kB time=00:00:41.15 bitrate=7695.1kbits/s speed=0.807x    
Past duration 0.878319 too large
Past duration 0.878654 too large
Past duration 0.881996 too large
Past duration 0.882652 too large
Past duration 0.884987 too large
Past duration 0.885323 too large
Past duration 0.887321 too large
Past duration 0.887657 too large
Past duration 0.889992 too large   39168kB time=00:00:41.15 bitrate=7797.1kbits/s speed=0.798x    
    Last message repeated 1 times
Past duration 0.891655 too large
Past duration 0.893990 too large
Past duration 0.895332 too large
    Last message repeated 1 times
Past duration 0.897987 too large
Past duration 0.899666 too large
Past duration 0.900993 too large   39680kB time=00:00:41.15 bitrate=7899.0kbits/s speed=0.79x    
Past duration 0.902657 too large
Past duration 0.903999 too large
Past duration 0.904991 too large
Past duration 0.905327 too large
Past duration 0.906654 too large
Past duration 0.907997 too large
Past duration 0.910652 too large
Past duration 0.910988 too large   40192kB time=00:00:42.85 bitrate=7682.3kbits/s speed=0.814x    
Past duration 0.913322 too large
frame=  985 fps= 13 q=-1.0 Lsize=   60323kB time=00:00:51.58 bitrate=9579.9kbits/s speed=0.695x    
video:59484kB audio:810kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.048264%
[libx264 @ 0x55dd4a513aa0] frame I:5     Avg QP:22.90  size:206885
[libx264 @ 0x55dd4a513aa0] frame P:303   Avg QP:25.09  size:100886
[libx264 @ 0x55dd4a513aa0] frame B:677   Avg QP:27.85  size: 43292
[libx264 @ 0x55dd4a513aa0] consecutive B-frames:  6.3%  4.3%  5.8% 83.7%
[libx264 @ 0x55dd4a513aa0] mb I  I16..4: 11.1% 49.1% 39.8%
[libx264 @ 0x55dd4a513aa0] mb P  I16..4:  6.5% 14.1%  9.5%  P16..4: 37.5% 16.2%  8.2%  0.0%  0.0%    skip: 8.0%
[libx264 @ 0x55dd4a513aa0] mb B  I16..4:  1.5%  1.6%  3.3%  B16..8: 48.1%  8.4%  2.2%  direct: 4.6%  skip:30.3%  L0:52.3% L1:34.9% BI:12.8%
[libx264 @ 0x55dd4a513aa0] 8x8 transform intra:40.2% inter:46.8%
[libx264 @ 0x55dd4a513aa0] coded y,uvDC,uvAC intra: 63.8% 25.9% 2.2% inter: 27.2% 7.1% 0.0%
[libx264 @ 0x55dd4a513aa0] i16 v,h,dc,p: 20% 25% 25% 30%
[libx264 @ 0x55dd4a513aa0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 26% 25%  4%  4%  4%  6%  3%  8%
[libx264 @ 0x55dd4a513aa0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 12% 27% 18%  6%  6%  4% 10%  4% 13%
[libx264 @ 0x55dd4a513aa0] i8c dc,h,v,p: 73% 14% 11%  2%
[libx264 @ 0x55dd4a513aa0] Weighted P-Frames: Y:18.8% UV:5.9%
[libx264 @ 0x55dd4a513aa0] ref P L0: 67.5% 18.8% 10.3%  3.0%  0.5%
[libx264 @ 0x55dd4a513aa0] ref B L0: 96.9%  2.4%  0.7%
[libx264 @ 0x55dd4a513aa0] ref B L1: 98.4%  1.6%
[libx264 @ 0x55dd4a513aa0] kb/s:14841.31
[aac @ 0x55dd4a4b21c0] Qavg: 496.588

Update 2: 
As I was told, the effect I was searching for is Crossfade. 
I've found these: 

FFmpeg command for crossfading between 5 videos. How to manage setpts=PTS-STARTPTS?, 
Concate two video file with fade effect with ffmpeg in linux
What is video timescale, timebase, or timestamp in ffmpeg? 

After some experiments I've finally got the working command for my purpose:
[0:v]setpts=PTS-STARTPTS[v0];
[1:v]setpts=PTS-STARTPTS+(23/TB),format=yuva444p,fade=st=23:d=1:t=in:alpha=1[v1];[v0][v1]overlay,format=yuv420p[v];
[0:a]asetpts=PTS-STARTPTS[a0];
[1:a]asetpts=PTS-STARTPTS[a1];
[a0][a1]acrossfade=d=1[a]

It works, but I have to set times for crossfade for every pair of videos manually
So what I want to ask again:

Is there any way to automate position for crossfade (last second of the video)? Is there any use for PREV_OUTPTS or PREV_INPTS? I didn't manage to get them work.
Can this effect be done with stream copy? There is no use for encoding the stream again.

**Tags:** ffmpeg

> **Comments:**
> **username_1**: Adapt the examples in https://superuser.com/a/834035/  
> **username_0**: I used an example for crossfade with the complex filter like this for video: `[0:v]setpts=PTS-STARTPTS[v0];[1:v]setpts=PTS-STARTPTS+4/TB,format=yuva444p,fade=st=4:d=1:t=in:alpha=1[v1];`. But this results in output with 4 seconds from the first video then crossfade and complete second video. I suppose something should be done with this `[1:v]setpts=PTS-STARTPTS+4/TB` and this `fade=st=4:` but didn't figure out the correct result  
> **username_1**: [Edit] your question to include your full command and the complete log.  
> **username_0**: @username_1, I've updated my question  

---

## 1 answers

### Answer by username_1 on 2020-01-27. Score: 1

Is there any way to automate position for crossfade (last second of the video)?

Not with ffmpeg alone (yet at least–there is a patch that never got applied that would simplify this). You can use  ffprobe to get the duration of each input and use that to determine the values for setpts and fade.
Example ffprobe command:
ffprobe -v error -show_entries format=duration -of csv=p=0 input.mp4


Is there any use for PREV_OUTPTS or PREV_INPTS? I didn't manage to get them work.

No. These do not refer to previous input/output streams or files.

Can this effect be done with stream copy? There is no use for encoding the stream again.

No. You can't filter and stream copy the same stream.

> **Comments:**
> **username_0**: Thank you for answer and noting the use of ffprobe. Maybe in some time I'll manage to write script to automate this for me. By the way - will complex filters setpts and fade work with float numbers, for example 442.224 seconds?  
> **username_1**: @AlexanderY. Yes, but such accuracy can become moot due to the frame rate.  
> **username_0**: Thanks. Will experiment more.  

---

