title: Problems with FFMPEG concat and/or MP4BOX
body: <p>I've been searching for a reliable way to concat a large (>30) number of mp4 files.</p>
  
  <p>My methods so far have been trying the MP4BOX concat CLI, and FFMPEG's concat filter (such as <code>ffmpeg -auto_convert 1 -y -f concat -i files.txt -c copy out.mp4</code>). </p>
  
  <p>Both tools produce the same problem: joining 2 or 3 files seems to work great, but any greater number produces a corrupted mp4 file (such as <a href="https://drive.google.com/file/d/0B_mWxBuwYcSgQjQtUnpQeUxVTm8/view" rel="nofollow noreferrer">https://drive.google.com/file/d/0B_mWxBuwYcSgQjQtUnpQeUxVTm8/view</a>). I can create a workaround where I join only 2 videos at a time, until all videos have been joined, but this does not work for an odd number of joins (i.e. 13). </p>
  
  <p>Why do these methods work on some files, but not others?</p>
  
  <p>FFPROBE produces errors as you would expect on these corrupted files:</p>
  
  <pre><code>error while decoding MB x y, bytestream z
  Reference 2 &gt;= 2
  top block unavailable for requested intra mode
  </code></pre>
  
  <p>example FFMPEG output:</p>
  
  <pre><code>ffmpeg version N-80901-gfebc862 Copyright (c) 2000-2016 the FFmpeg developers
    built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
    configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --mandir=/usr/share/man --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libfreetype --enable-gnutls --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvidstab
    libavutil      55. 28.100 / 55. 28.100
    libavcodec     57. 48.101 / 57. 48.101
    libavformat    57. 41.100 / 57. 41.100
    libavdevice    57.  0.102 / 57.  0.102
    libavfilter     6. 47.100 /  6. 47.100
    libavresample   3.  0.  0 /  3.  0.  0
    libswscale      4.  1.100 /  4.  1.100
    libswresample   2.  1.100 /  2.  1.100
    libpostproc    54.  0.100 / 54.  0.100
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x36b3a80] Auto-inserting h264_mp4toannexb bitstream filter
  Input #0, concat, from 'twitter-files.txt':
    Duration: N/A, start: 0.000000, bitrate: 415 kb/s
      Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 284 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc
      Metadata:
        handler_name    : VideoHandler
      Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 130 kb/s
      Metadata:
        handler_name    : SoundHandler
  [mp4 @ 0x36b7dc0] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead.
      Last message repeated 1 times
  Output #0, mp4, to 'twitter-tmp.mp4':
    Metadata:
      encoder         : Lavf57.41.100
      Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1280x720, q=2-31, 284 kb/s, 25 fps, 25 tbr, 12800 tbn, 12800 tbc
      Metadata:
        handler_name    : VideoHandler
      Stream #0:1(und): Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, 130 kb/s
      Metadata:
        handler_name    : SoundHandler
  Stream mapping:
    Stream #0:0 -&gt; #0:0 (copy)
    Stream #0:1 -&gt; #0:1 (copy)
  Press [q] to stop, [?] for help
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x3733160] Auto-inserting h264_mp4toannexb bitstream filter
      Last message repeated 1 times
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x3733140] Auto-inserting h264_mp4toannexb bitstream filter
  [mp4 @ 0x36b7dc0] Packet with invalid duration -512 in stream 1
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x3733140] Auto-inserting h264_mp4toannexb bitstream filter
      Last message repeated 2 times
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x36ba960] Auto-inserting h264_mp4toannexb bitstream filter
  [mp4 @ 0x36b7dc0] Packet with invalid duration -512 in stream 1
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x36ba960] Auto-inserting h264_mp4toannexb bitstream filter
      Last message repeated 4 times
  [mp4 @ 0x36b7dc0] Non-monotonous DTS in output stream 0:0; previous: 306957, current: 306445; changing to 306958. This may result in incorrect timestamps in the output file.
  [mp4 @ 0x36b7dc0] Non-monotonous DTS in output stream 0:0; previous: 306958, current: 306957; changing to 306959. This may result in incorrect timestamps in the output file.
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x36ba960] Auto-inserting h264_mp4toannexb bitstream filter
      Last message repeated 1 times
  frame=  750 fps=0.0 q=-1.0 Lsize=    1088kB time=00:00:30.04 bitrate= 296.6kbits/s speed= 366x 
  </code></pre>
  
  <p>OSX output:</p>
  
  <pre><code>ffmpeg version 3.0 Copyright (c) 2000-2016 the FFmpeg developers
    built with Apple LLVM version 7.3.0 (clang-703.0.31)
    configuration: --prefix=/usr/local/Cellar/ffmpeg/3.0 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libxvid --enable-libvorbis --enable-libvpx --enable-vda
    libavutil      55. 17.103 / 55. 17.103
    libavcodec     57. 24.102 / 57. 24.102
    libavformat    57. 25.100 / 57. 25.100
    libavdevice    57.  0.101 / 57.  0.101
    libavfilter     6. 31.100 /  6. 31.100
    libavresample   3.  0.  0 /  3.  0.  0
    libswscale      4.  0.100 /  4.  0.100
    libswresample   2.  0.101 /  2.  0.101
    libpostproc    54.  0.100 / 54.  0.100
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c49808c00] Auto-inserting h264_mp4toannexb bitstream filter
  Input #0, concat, from 'twitter-files.txt':
    Duration: N/A, start: 0.000000, bitrate: 415 kb/s
      Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 284 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc
      Metadata:
        handler_name    : VideoHandler
      Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 130 kb/s
      Metadata:
        handler_name    : SoundHandler
  Output #0, mp4, to 'tmp-twitter.mp4':
    Metadata:
      encoder         : Lavf57.25.100
      Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1280x720, q=2-31, 284 kb/s, 25 fps, 25 tbr, 12800 tbn, 12800 tbc
      Metadata:
        handler_name    : VideoHandler
      Stream #0:1(und): Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, 130 kb/s
      Metadata:
        handler_name    : SoundHandler
  Stream mapping:
    Stream #0:0 -&gt; #0:0 (copy)
    Stream #0:1 -&gt; #0:1 (copy)
  Press [q] to stop, [?] for help
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c4a005400] Auto-inserting h264_mp4toannexb bitstream filter
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c4a800000] Auto-inserting h264_mp4toannexb bitstream filter
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c49800000] Auto-inserting h264_mp4toannexb bitstream filter
  [mp4 @ 0x7f9c4a000c00] Packet with invalid duration -512 in stream 1
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c49800000] Auto-inserting h264_mp4toannexb bitstream filter
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c4a800000] Auto-inserting h264_mp4toannexb bitstream filter
      Last message repeated 1 times
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c49800000] Auto-inserting h264_mp4toannexb bitstream filter
  [mp4 @ 0x7f9c4a000c00] Packet with invalid duration -512 in stream 1
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c49800000] Auto-inserting h264_mp4toannexb bitstream filter
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c4a800000] Auto-inserting h264_mp4toannexb bitstream filter
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c4a005400] Auto-inserting h264_mp4toannexb bitstream filter
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c4a800e00] Auto-inserting h264_mp4toannexb bitstream filter
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c49800000] Auto-inserting h264_mp4toannexb bitstream filter
  [mp4 @ 0x7f9c4a000c00] Non-monotonous DTS in output stream 0:0; previous: 306957, current: 306445; changing to 306958. This may result in incorrect timestamps in the output file.
  [mp4 @ 0x7f9c4a000c00] Non-monotonous DTS in output stream 0:0; previous: 306958, current: 306957; changing to 306959. This may result in incorrect timestamps in the output file.
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c49800000] Auto-inserting h264_mp4toannexb bitstream filter
  [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f9c49010e00] Auto-inserting h264_mp4toannexb bitstream filter
  frame=  750 fps=0.0 q=-1.0 Lsize=    1146kB time=00:00:30.04 bitrate= 312.4kbits/s speed= 192x  
  </code></pre>