[FFmpeg-user] Non-monotonic DTS issue with merging MP4 files with different tbn

Abhishek Gupta abhishekgupt at gmail.com
Sat May 11 09:28:31 EEST 2024


Hello all,

I am trying to merge two files below that have different AV properties:
1.mp4
<https://speechacellc-my.sharepoint.com/:v:/g/personal/abhishek_speechacellc_onmicrosoft_com/EUsJDq2Q3LlBub7zlFHYHp0B2xhj4GpyCSjpWfp_m4uq_g?nav=eyJyZWZlcnJhbEluZm8iOnsicmVmZXJyYWxBcHAiOiJPbmVEcml2ZUZvckJ1c2luZXNzIiwicmVmZXJyYWxBcHBQbGF0Zm9ybSI6IldlYiIsInJlZmVycmFsTW9kZSI6InZpZXciLCJyZWZlcnJhbFZpZXciOiJNeUZpbGVzTGlua0NvcHkifX0&e=RL2XMX>
2.mp4
<https://speechacellc-my.sharepoint.com/:v:/g/personal/abhishek_speechacellc_onmicrosoft_com/EdqHFw0X61JLjwdboqiD5GUBvxdVympQQ6WJMp786xv2lw?nav=eyJyZWZlcnJhbEluZm8iOnsicmVmZXJyYWxBcHAiOiJPbmVEcml2ZUZvckJ1c2luZXNzIiwicmVmZXJyYWxBcHBQbGF0Zm9ybSI6IldlYiIsInJlZmVycmFsTW9kZSI6InZpZXciLCJyZWZlcnJhbFZpZXciOiJNeUZpbGVzTGlua0NvcHkifX0&e=jTx1Sm>

*Below is ffprobe output for 1.mp4:*
ffprobe version 6.1.1-3ubuntu5 Copyright (c) 2007-2023 the FFmpeg developers
  built with gcc 13 (Ubuntu 13.2.0-23ubuntu3)
  configuration: --prefix=/usr --extra-version=3ubuntu5
--toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu
--incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl
--disable-stripping --disable-omx --enable-gnutls --enable-libaom
--enable-libass --enable-libbs2b --enable-libcaca --enable-libcdio
--enable-libcodec2 --enable-libdav1d --enable-libflite
--enable-libfontconfig --enable-libfreetype --enable-libfribidi
--enable-libglslang --enable-libgme --enable-libgsm --enable-libharfbuzz
--enable-libmp3lame --enable-libmysofa --enable-libopenjpeg
--enable-libopenmpt --enable-libopus --enable-librubberband
--enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex
--enable-libtheora --enable-libtwolame --enable-libvidstab
--enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265
--enable-libxml2 --enable-libxvid --enable-libzimg --enable-openal
--enable-opencl --enable-opengl --disable-sndio --enable-libvpl
--disable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883
--enable-chromaprint --enable-frei0r --enable-ladspa --enable-libbluray
--enable-libjack --enable-libpulse --enable-librabbitmq --enable-librist
--enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libx264
--enable-libzmq --enable-libzvbi --enable-lv2 --enable-sdl2
--enable-libplacebo --enable-librav1e --enable-pocketsphinx
--enable-librsvg --enable-libjxl --enable-shared
  libavutil      58. 29.100 / 58. 29.100
  libavcodec     60. 31.102 / 60. 31.102
  libavformat    60. 16.100 / 60. 16.100
  libavdevice    60.  3.100 / 60.  3.100
  libavfilter     9. 12.100 /  9. 12.100
  libswscale      7.  5.100 /  7.  5.100
  libswresample   4. 12.100 /  4. 12.100
  libpostproc    57.  3.100 / 57.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp42mp41
    creation_time   : 2024-04-11T07:52:09.000000Z
  Duration: 00:00:10.40, start: 0.000000, bitrate: 1226 kb/s
  *Stream #0:0[0x1](eng): Video: h264 (Main) (avc1 / 0x31637661),
yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 861 kb/s, 30
fps, 30 tbr, 30k tbn (default)*
    Metadata:
      creation_time   : 2024-04-11T07:52:09.000000Z
      handler_name    : ?Mainconcept Video Media Handler
      vendor_id       : [0][0][0][0]
      encoder         : AVC Coding
  Stream #0:1[0x2](eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz,
stereo, fltp, 316 kb/s (default)
    Metadata:
      creation_time   : 2024-04-11T07:52:10.000000Z
      handler_name    : #Mainconcept MP4 Sound Media Handler
      vendor_id       : [0][0][0][0]


*Below is ffprobe output for 2.mp4:*ffprobe version 6.1.1-3ubuntu5
Copyright (c) 2007-2023 the FFmpeg developers
  built with gcc 13 (Ubuntu 13.2.0-23ubuntu3)
  configuration: --prefix=/usr --extra-version=3ubuntu5
--toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu
--incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl
--disable-stripping --disable-omx --enable-gnutls --enable-libaom
--enable-libass --enable-libbs2b --enable-libcaca --enable-libcdio
--enable-libcodec2 --enable-libdav1d --enable-libflite
--enable-libfontconfig --enable-libfreetype --enable-libfribidi
--enable-libglslang --enable-libgme --enable-libgsm --enable-libharfbuzz
--enable-libmp3lame --enable-libmysofa --enable-libopenjpeg
--enable-libopenmpt --enable-libopus --enable-librubberband
--enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex
--enable-libtheora --enable-libtwolame --enable-libvidstab
--enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265
--enable-libxml2 --enable-libxvid --enable-libzimg --enable-openal
--enable-opencl --enable-opengl --disable-sndio --enable-libvpl
--disable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883
--enable-chromaprint --enable-frei0r --enable-ladspa --enable-libbluray
--enable-libjack --enable-libpulse --enable-librabbitmq --enable-librist
--enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libx264
--enable-libzmq --enable-libzvbi --enable-lv2 --enable-sdl2
--enable-libplacebo --enable-librav1e --enable-pocketsphinx
--enable-librsvg --enable-libjxl --enable-shared
  libavutil      58. 29.100 / 58. 29.100
  libavcodec     60. 31.102 / 60. 31.102
  libavformat    60. 16.100 / 60. 16.100
  libavdevice    60.  3.100 / 60.  3.100
  libavfilter     9. 12.100 /  9. 12.100
  libswscale      7.  5.100 /  7.  5.100
  libswresample   4. 12.100 /  4. 12.100
  libpostproc    57.  3.100 / 57.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf59.16.100
  Duration: 00:00:10.89, start: 0.000000, bitrate: 87 kb/s
  *Stream #0:0[0x1](eng): Video: h264 (High) (avc1 / 0x31637661),
yuv420p(progressive), 564x396, 16 kb/s, 24 fps, 24 tbr, 30k tbn (default)*
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1[0x2](eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz,
stereo, fltp, 64 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]

As you can see they have different fps and tbn and tbr. As a next step, I
tried to make file 2.mp4 have the same properties as file 1.mp4 using the
below commands:
Change framerate: *ffmpeg -y -i 2.mp4 -vf "setpts=1.25*PTS" -r 30 2.1.mp4*
Change tbn: *ffmpeg -i 2.1.mp4 -c:v copy -video_track_timescale 30k 2.2.mp4*


*Here is the ffprobe result for 2.2.mp4:*ffprobe version 6.1.1-3ubuntu5
Copyright (c) 2007-2023 the FFmpeg developers
  built with gcc 13 (Ubuntu 13.2.0-23ubuntu3)
  configuration: --prefix=/usr --extra-version=3ubuntu5
--toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu
--incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl
--disable-stripping --disable-omx --enable-gnutls --enable-libaom
--enable-libass --enable-libbs2b --enable-libcaca --enable-libcdio
--enable-libcodec2 --enable-libdav1d --enable-libflite
--enable-libfontconfig --enable-libfreetype --enable-libfribidi
--enable-libglslang --enable-libgme --enable-libgsm --enable-libharfbuzz
--enable-libmp3lame --enable-libmysofa --enable-libopenjpeg
--enable-libopenmpt --enable-libopus --enable-librubberband
--enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex
--enable-libtheora --enable-libtwolame --enable-libvidstab
--enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265
--enable-libxml2 --enable-libxvid --enable-libzimg --enable-openal
--enable-opencl --enable-opengl --disable-sndio --enable-libvpl
--disable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883
--enable-chromaprint --enable-frei0r --enable-ladspa --enable-libbluray
--enable-libjack --enable-libpulse --enable-librabbitmq --enable-librist
--enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libx264
--enable-libzmq --enable-libzvbi --enable-lv2 --enable-sdl2
--enable-libplacebo --enable-librav1e --enable-pocketsphinx
--enable-librsvg --enable-libjxl --enable-shared
  libavutil      58. 29.100 / 58. 29.100
  libavcodec     60. 31.102 / 60. 31.102
  libavformat    60. 16.100 / 60. 16.100
  libavdevice    60.  3.100 / 60.  3.100
  libavfilter     9. 12.100 /  9. 12.100
  libswscale      7.  5.100 /  7.  5.100
  libswresample   4. 12.100 /  4. 12.100
  libpostproc    57.  3.100 / 57.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '2.2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf60.16.100
  Duration: 00:00:13.53, start: 0.000000, bitrate: 77 kb/s
  *Stream #0:0[0x1](eng): Video: h264 (High) (avc1 / 0x31637661),
yuv420p(progressive), 564x396, 17 kb/s, 30 fps, 30 tbr, 30k tbn (default)*
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
      encoder         : Lavc60.31.102 libx264
  Stream #0:1[0x2](eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz,
stereo, fltp, 64 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]

As can be observed now the value for 1.mp4 and 2.mp4 match:
1.mp4 - Stream #0:0[0x1](eng): Video: h264 (Main) (avc1 / 0x31637661),
yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 861 kb/s, 30
fps, 30 tbr, 30k tbn (default)
2.mp4 -  Stream #0:0[0x1](eng): Video: h264 (High) (avc1 / 0x31637661),
yuv420p(progressive), 564x396, 17 kb/s, 30 fps, 30 tbr, 30k tbn (default)

Now if I try to merge the 2 files as follows, I get the notorious
non-monotonic DTS error as below:
*merge.txt*
file 1.mp4
file 2.2.mp4

ffmpeg -f concat -i merge.txt -vcodec copy -acodec copy combined.mp4
ffmpeg version 6.1.1-3ubuntu5 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 13 (Ubuntu 13.2.0-23ubuntu3)
  configuration: --prefix=/usr --extra-version=3ubuntu5
--toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu
--incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl
--disable-stripping --disable-omx --enable-gnutls --enable-libaom
--enable-libass --enable-libbs2b --enable-libcaca --enable-libcdio
--enable-libcodec2 --enable-libdav1d --enable-libflite
--enable-libfontconfig --enable-libfreetype --enable-libfribidi
--enable-libglslang --enable-libgme --enable-libgsm --enable-libharfbuzz
--enable-libmp3lame --enable-libmysofa --enable-libopenjpeg
--enable-libopenmpt --enable-libopus --enable-librubberband
--enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex
--enable-libtheora --enable-libtwolame --enable-libvidstab
--enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265
--enable-libxml2 --enable-libxvid --enable-libzimg --enable-openal
--enable-opencl --enable-opengl --disable-sndio --enable-libvpl
--disable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883
--enable-chromaprint --enable-frei0r --enable-ladspa --enable-libbluray
--enable-libjack --enable-libpulse --enable-librabbitmq --enable-librist
--enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libx264
--enable-libzmq --enable-libzvbi --enable-lv2 --enable-sdl2
--enable-libplacebo --enable-librav1e --enable-pocketsphinx
--enable-librsvg --enable-libjxl --enable-shared
  libavutil      58. 29.100 / 58. 29.100
  libavcodec     60. 31.102 / 60. 31.102
  libavformat    60. 16.100 / 60. 16.100
  libavdevice    60.  3.100 / 60.  3.100
  libavfilter     9. 12.100 /  9. 12.100
  libswscale      7.  5.100 /  7.  5.100
  libswresample   4. 12.100 /  4. 12.100
  libpostproc    57.  3.100 / 57.  3.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x57a3a3fbb500] Auto-inserting h264_mp4toannexb
bitstream filter
Input #0, concat, from 'merge.txt':
  Duration: N/A, start: 0.000000, bitrate: 1178 kb/s
  Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv,
bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 861 kb/s, 30 fps, 30
tbr, 30k tbn
    Metadata:
      creation_time   : 2024-04-11T07:52:09.000000Z
      handler_name    : ?Mainconcept Video Media Handler
      vendor_id       : [0][0][0][0]
      encoder         : AVC Coding
  Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo,
fltp, 316 kb/s
    Metadata:
      creation_time   : 2024-04-11T07:52:10.000000Z
      handler_name    : #Mainconcept MP4 Sound Media Handler
      vendor_id       : [0][0][0][0]
Output #0, mp4, to 'combined.mp4':
  Metadata:
    encoder         : Lavf60.16.100
  Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv,
bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 861 kb/s, 30
fps, 30 tbr, 30k tbn
    Metadata:
      creation_time   : 2024-04-11T07:52:09.000000Z
      handler_name    : ?Mainconcept Video Media Handler
      vendor_id       : [0][0][0][0]
      encoder         : AVC Coding
  Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo,
fltp, 316 kb/s
    Metadata:
      creation_time   : 2024-04-11T07:52:10.000000Z
      handler_name    : #Mainconcept MP4 Sound Media Handler
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x57a3a3fbb500] Auto-inserting h264_mp4toannexb
bitstream filter

*[mp4 @ 0x57a3a4046b40] Non-monotonic DTS in output stream 0:0; previous:
310000, current: 310000; changing to 310001. This may result in incorrect
timestamps in the output file.[mp4 @ 0x57a3a4046b40] Non-monotonic DTS in
output stream 0:1; previous: 498688, current: 498176; changing to 498689.
This may result in incorrect timestamps in the output file.*
[out#0/mp4 @ 0x57a3a4016c00] video:1123kB audio:489kB subtitle:0kB other
streams:0kB global headers:0kB muxing overhead: 1.575911%
size=    1637kB time=00:00:23.83 bitrate= 562.6kbits/s speed= 516x

I have spent all day trying to solve this one. Please can someone help? I
will be deeply obliged.

Thanks and please let me know if you have further questions or need more
information.

Abhishek


More information about the ffmpeg-user mailing list