5

I'm trying to concat videos with the concat demuxer, but it does not work when using one video ("video2.mp4" below). By does not work, I mean playing the concatenated video on a player will work until the second video part starts (it just cannot read the video anymore). It works with the concat filter though. They are both mp4 videos, so I think it's because of the time base ? I can concat other videos with the concat demuxer and it works fine (even with different resolutions/bitrate). It only happens when trying to concat "video2.mp4".

Also, I have a lot of warning/errors like this, probably when ffmpeg starts concatenating the 2nd video :

[mp4 @ 0x7f847a814800] Non-monotonous DTS in output stream 0:0; previous: 906906, current: 302359; changing to 906907. This may result in incorrect timestamps in the output file.

What would be the best way to have a minimum concat time ? Do I really need to use the concat filter or can I change the time base of "video1.mp4" if it's really the problem ?

Any help would be appreciated, thanks !

Video 1 :

ffprobe version 3.2.2 Copyright (c) 2007-2016 the FFmpeg developers
  built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.2.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
  libavutil      55. 34.100 / 55. 34.100
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.100 / 57. 56.100
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libavresample   3.  1.  0 /  3.  1.  0
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2016-08-17T22:50:35.000000Z
  Duration: 00:00:10.11, start: 0.000000, bitrate: 38018 kb/s
    Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1696x848, 37832 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2016-08-18T00:02:24.000000Z
      handler_name    : VideoHandle
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 189 kb/s (default)
    Metadata:
      creation_time   : 2016-08-18T00:02:24.000000Z
      handler_name    : SoundHandle 

Video 2 :

ffprobe version 3.2.2 Copyright (c) 2007-2016 the FFmpeg developers
  built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.2.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
  libavutil      55. 34.100 / 55. 34.100
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.100 / 57. 56.100
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libavresample   3.  1.  0 /  3.  1.  0
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video2.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp42mp41
    creation_time   : 2017-01-06T22:30:23.000000Z
  Duration: 00:00:08.19, start: 0.000000, bitrate: 101474 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 4096x2048 [SAR 1:1 DAR 2:1], 101549 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
    Metadata:
      creation_time   : 2017-01-06T22:30:23.000000Z
      handler_name    : ?Mainconcept Video Media Handler
      encoder         : AVC Coding
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 125 kb/s (default)
    Metadata:
      creation_time   : 2017-01-06T22:30:23.000000Z
      handler_name    : #Mainconcept MP4 Sound Media Handler 

FFMpeg Command :

ffmpeg -f concat -safe 0 -i concat.txt -c copy result.mp4

concat.txt :

file '/path/to/video1.mp4'
file '/path/to/video2.mp4' 

Command result :

ffmpeg version 3.2.2 Copyright (c) 2000-2016 the FFmpeg developers
  built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.2.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
  libavutil      55. 34.100 / 55. 34.100
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.100 / 57. 56.100
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libavresample   3.  1.  0 /  3.  1.  0
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fbd8b808c00] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from 'concat.txt':
  Duration: N/A, start: 0.000000, bitrate: 38021 kb/s
    Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1696x848, 37832 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 180k tbc
    Metadata:
      creation_time   : 2016-08-18T00:02:24.000000Z
      handler_name    : VideoHandle
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 189 kb/s
    Metadata:
      creation_time   : 2016-08-18T00:02:24.000000Z
      handler_name    : SoundHandle
Output #0, mp4, to 'result.mp4':
  Metadata:
    encoder         : Lavf57.56.100
    Stream #0:0(eng): Video: h264 (Baseline) ([33][0][0][0] / 0x0021), yuv420p, 1696x848, q=2-31, 37832 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 90k tbc
    Metadata:
      creation_time   : 2016-08-18T00:02:24.000000Z
      handler_name    : VideoHandle
    Stream #0:1(eng): Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, 189 kb/s
    Metadata:
      creation_time   : 2016-08-18T00:02:24.000000Z
      handler_name    : SoundHandle
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy) 
Press [q] to stop, [?] for help
frame=  112 fps=0.0 q=-1.0 size=   12701kB time=00:00:03.70 bitrate=28092.2kbits/s speed= 7.4x
frame=  151 fps=151 q=-1.0 size=   18853kB time=00:00:05.00 bitrate=30857.5kbits/s speed=   5x
frame=  224 fps=149 q=-1.0 size=   30042kB time=00:00:07.44 bitrate=33074.8kbits/s speed=4.95x
frame=  268 fps=134 q=-1.0 size=   36596kB time=00:00:08.90 bitrate=33650.8kbits/s speed=4.44x
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fbd8a808000] Auto-inserting h264_mp4toannexb bitstream filter.9x 
[mp4 @ 0xb545d000] Non-monotonous DTS in output stream 0:1; previous: 484352, current: 445939; changing to 484353. This may result in incorrect timestamps in the output file.
[concat @ 0xb545c400] DTS 304057 < 906906 out of order
[mp4 @ 0xb545d000] Non-monotonous DTS in output stream 0:0; previous: 906906, current: 304057; changing to 906907. This may result in incorrect timestamps in the output file.
[mp4 @ 0xb545d000] Non-monotonous DTS in output stream 0:1; previous: 484353, current: 446963; changing to 484354. This may result in incorrect timestamps in the output file.
...
... like 100 DTS errors ...
...
[mp4 @ 0xb545d000] Non-monotonous DTS in output stream 0:0; previous: 907150, current: 548301; changing to 907151. This may result in incorrect timestamps in the output file.
frame=  548 fps=169 q=-1.0 Lsize=  148399kB time=00:00:18.28 bitrate=66493.7kbits/s speed=5.64x
video:148027kB audio:359kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.008622%

Thank you in advance !

9
  • it does not work --> what exactly happens? FFmpeg will allow you to concat videos with different tbn. Just that subsequent videos will effectively have a speed change applied.
    – Gyan
    Commented Jan 17, 2017 at 17:38
  • Thank you for your answer ! Indeed it was not very clear, I entirely edited my post. I added the command output. And by "does not work" I meant that the final video plays until the second video part starts. (A+B = C), C plays until the B part starts.
    – Xys
    Commented Jan 18, 2017 at 10:59
  • If needed, I can provide the 2 videos in question
    – Xys
    Commented Jan 18, 2017 at 11:33
  • Sounds more likely due to a difference in H.264 profile. Convert video A to baseline profile without rescaling or anything else and then try.
    – Gyan
    Commented Jan 18, 2017 at 12:24
  • Thanks, unfortunately I converted to baseline and it still does not play properly (frozen at second part). If you want you can find both videos here : dropbox.com/sh/v99mngn7pfhrfvx/AADKnnJ1trFEF-kPy6p7dssga?dl=0 Again, thanks a lot !
    – Xys
    Commented Jan 18, 2017 at 13:44

3 Answers 3

6

There's something funky about the H.264 bitstream in video2.mp4

The steps below (to be applied once) resolves it for me and the resulting concat plays fine with ffplay, Potplayer, VLC, WMP, Chrome and Firefox.

#1 Convert video 2 to match video 1

ffmpeg -i video2.mp4 -vf scale=1696x848 -profile:v baseline -c:a copy v2.mp4

#2 Extract to raw bitstream

ffmpeg -i v2.mp4 -c copy -f h264 v2.h264

#3 Mux it to new container

ffmpeg -i v2.h264 -i v2.mp4 -c copy -video_track_timescale 90000 -map 0 -map 1:a v2-ok.mp4

Now, you can concat v2-ok.mp4 with video1.mp4

7
  • Thank you very much ! It almost solved my problem, I can indeed see the 2 videos in the result video, but there is a gap of like 1 second between the two, do you also repro this or not ? I added the result.mp4 video in dropbox if you want to compare with yours : dropbox.com/sh/v99mngn7pfhrfvx/AADKnnJ1trFEF-kPy6p7dssga?dl=0 Again, thanks a lot ! Really..
    – Xys
    Commented Jan 19, 2017 at 14:50
  • Your result is the same as mine and plays without pause in ffplay and Potplayer but crashes in VLC. I think this is down to how each player handles a change mid-playback in stream properties.
    – Gyan
    Commented Jan 19, 2017 at 16:17
  • Okay thanks a lot. Is there any other way I can make the video2 to have the exact same properties than video1, so that the concat demuxer works ? If not I guess concat filter is the only option..
    – Xys
    Commented Jan 19, 2017 at 16:43
  • Without re-encoding? No. And if you're re-encoding, the filter is the most convenient.
    – Gyan
    Commented Jan 19, 2017 at 17:00
  • I mean can I only re-encode video2.mp4 (not video1.mp4) to have the same properties than video1.mp4, so that the concat demuxer works perfectly ?
    – Xys
    Commented Jan 20, 2017 at 13:10
1

The problem is the timebase. Ffmpeg adds an offset to the streams in the second, but otherwise doesn't change their timestamps even though it should.

I have never been able to get any timebase-related options to do anything. What did work for me: copy to mkv, which has 1/1000 timebase for video streams (ffmpeg reports this as '1k tbn'). This makes all timestamps consistent. Then concat.

For normal purposes, webm and mkv are basically the same thing, but mkv accepts H.264 while webm currently doesn't.

Note that timestamps with any slight jitter from rounding can lead to dropped or duplicated frames with poorly-designed fps-changing filters.

If there's still a gap (but no speed change or error messages), make sure the audio isn't slightly longer than the video stream, and trim it if necessary; you could do this automatically with 'ffmpeg -i video1.mp4 -i video1.mp4 -c copy -map 0:v -map 1:a -shortest trimmed.mp4'.

I'm pasting this answer to both questions, don't know if that's allowed.

1
  • 1
    Please don't post the same answer twice. If it's the same question, flag as a duplicate.
    – JJJ
    Commented May 3, 2019 at 4:56
0

In my opinion, the key to the problem is that on examination of the two videos in question they very obviously have widely different bitrates. Unlike other factors, this is a problem that can only be corrected by re-encoding.

From the data posted by the user, video 1 has a video bitrate of 37,832 kb/s, but video 2 has a very different video bitrate of 101,549 kb/s.

Whatever other measures are taken (such as matching the frame size), the video player is going to pause at the join point, because it has to adjust from accumulating and displaying data at the (low) initial bitrate, and begin instead to display a revised image at a new (much higher) bitrate.

Interestingly, this might not have been so (i.e. might not have been noticeable) had the problem been posed to the player the other way round: if video 1 had been encoded at the high bitrate, and the combined file had then dropped to a lower bitrate part way through. In that situation, the initial level of buffering would have been more than enough for both halves of the movie, as the second half would have required less buffering. The pause in playback, in my experience, is a consequence of the necessity to raise the buffering rate at the join point, time being needed to buffer additional data, and would not occur if the buffering rate was being reduced instead.

Additionally, I would also point out the extreme nature of the discrepancy. The bitrate increases by 200 percent, the new bitrate being 3 times the size per second of the initial rate. This is indeed an extreme challenge to the player, which has been pottering along at one rate and then meets an instantaneous - and permanent - tripling of the bitrate. If the jump had been modest, the pause in playback might not have been perceptible at all. But if the bitrate triples without warning the initial video buffer has little chance of being able to smooth it out, and is bound to need time to buffer additional data. It isn't even a spike in the data rate, but a permanent increase.

Little wonder that some players will crash. It says a lot for the robustness of any video software player that it merely pauses, or stutters, faced with this situation. On the whole, I would not expect a pause if the increase was under 50 percent of the initial bitrate, but a 200 percent increase is just begging for trouble.

Obviously, if you don't equalise the frame size of the image, the change will be very noticeable even if the buffering is entirely adequate. What I'm talking about is that even if both video 1 and video 2 have the same image size, a pause in playback will still occur if the bitrate leaps to a much higher value at the join point.

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