Search code examples
ffmpeg

Merging two video files with ffmpeg and xfade filter cuts the first video


I try to concatenate two mp4 videos using ffmpeg's xfade filter:

ffmpeg -y -i x1.mp4 -i x2.mp4 -filter_complex "[0][1]xfade=transition=dissolve:duration=2:offset=2" dissolveVideo4.mp4

x1.mp4 is 14.52 seconds and x2.mp4 is 12.46 seconds. The resulting video is 14.48 seconds. Examining the resulting video shows the first input contributed only ~4 seconds at its end, the remaining is the second video.

This happens with any of the available transitions (fade etc.).

Here is the output of the ffmpeg command above:

ffmpeg version 5.1.2 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
  configuration: 
  libavutil      57. 28.100 / 57. 28.100
  libavcodec     59. 37.100 / 59. 37.100
  libavformat    59. 27.100 / 59. 27.100
  libavdevice    59.  7.100 / 59.  7.100
  libavfilter     8. 44.100 /  8. 44.100
  libswscale      6.  7.100 /  6.  7.100
  libswresample   4.  7.100 /  4.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'x1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2022-12-29T15:10:27.000000Z
    location        : +32.7648+035.0443/
    location-eng    : +32.7648+035.0443/
    com.android.version: 13
    com.android.capture.fps: 30.000000
  Duration: 00:00:14.52, start: 0.000000, bitrate: 17707 kb/s
  Stream #0:0[0x1](eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709/bt709/smpte170m, progressive), 1920x1080, 17449 kb/s, 29.96 fps, 30 tbr, 90k tbn (default)
    Metadata:
      creation_time   : 2022-12-29T15:10:27.000000Z
      handler_name    : VideoHandle
      vendor_id       : [0][0][0][0]
    Side data:
      displaymatrix: rotation of -90.00 degrees
  Stream #0:1[0x2](eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 256 kb/s (default)
    Metadata:
      creation_time   : 2022-12-29T15:10:27.000000Z
      handler_name    : SoundHandle
      vendor_id       : [0][0][0][0]
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'x2.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2022-12-29T15:10:47.000000Z
    location        : +32.7648+035.0443/
    location-eng    : +32.7648+035.0443/
    com.android.version: 13
    com.android.capture.fps: 30.000000
  Duration: 00:00:12.46, start: 0.000000, bitrate: 17461 kb/s
  Stream #1:0[0x1](eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709/bt709/smpte170m, progressive), 1920x1080, 17201 kb/s, 30.01 fps, 30 tbr, 90k tbn (default)
    Metadata:
      creation_time   : 2022-12-29T15:10:47.000000Z
      handler_name    : VideoHandle
      vendor_id       : [0][0][0][0]
    Side data:
      displaymatrix: rotation of -90.00 degrees
  Stream #1:1[0x2](eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 256 kb/s (default)
    Metadata:
      creation_time   : 2022-12-29T15:10:47.000000Z
      handler_name    : SoundHandle
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 (h264) -> xfade (graph 0)
  Stream #1:0 (h264) -> xfade (graph 0)
  xfade:default (graph 0) -> Stream #0:0 (mpeg4)
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, mp4, to 'dissolveVideo4.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    com.android.capture.fps: 30.000000
    location        : +32.7648+035.0443/
    location-eng    : +32.7648+035.0443/
    com.android.version: 13
    encoder         : Lavf59.27.100
  Stream #0:0: Video: mpeg4 (mp4v / 0x7634706D), yuv420p(tv, bt709/bt709/smpte170m, progressive), 1080x1920, q=2-31, 200 kb/s, 30 fps, 15360 tbn
    Metadata:
      encoder         : Lavc59.37.100 mpeg4
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
  Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      creation_time   : 2022-12-29T15:10:27.000000Z
      handler_name    : SoundHandle
      vendor_id       : [0][0][0][0]
      encoder         : Lavc59.37.100 aac
frame=  435 fps=145 q=31.0 Lsize=    7688kB time=00:00:14.48 bitrate=4347.9kbits/s speed=4.82x    
video:7443kB audio:231kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.181913%

What can be a problem?


Solution

  • offset is the duration into the first video where the transition ought to start, so instead of offset=2, given a duration of 14.52s for x1, it should be offset=12.52.