title: How to concatenate .mp4 files into one 4x4 movie using -ffmpeg-?
body: <p>How should I best concatenate 16 separate .mp4 files into one 4x4 movie using -ffmpeg-?</p>
  <p>I have workable code to create 2x2 movies (with changeable optional flag calls), with four <strong>equally-sized</strong> files, thus:</p>
  <pre><code>ffmpeg -i foo1.mp4 -i foo2.mp4 -i foo3.mp4 -i foo4.mp4 \ 
   -filter_complex \
     &quot;[0:v][1:v]hstack[t]; \
      [2:v][3:v]hstack[b]; \
      [t][b]vstack,format=yuv420p[v]; \
      [0:a][1:a][2:a][3:a]amerge=inputs=4[a]&quot; \
  -map &quot;[v]&quot; \ 
  -map &quot;[a]&quot; \
  -ac 2 -c:v libx264 \
  foo.mp4
  </code></pre>
  <p>and with four <strong>unequally-sized</strong> files, thus:</p>
  <pre><code>ffmpeg -i foo1.mp4 -i foo2.mp4 -i foo3.mp4 -i foo4.mp4 \
   -filter_complex \
     &quot;[0:v]scale=640:360[v0]; \
      [1:v]scale=640:360[v1]; \
      [2:v]scale=640:360[v2]; \
      [3:v]scale=640:360[v3]; \
      [v0][v1]hstack[t]; \
      [v2][v3]hstack[b]; \
      [t][b]vstack,format=yuv420p[v]; \
      [0:a][1:a][2:a][3:a]amerge=inputs=4[a]&quot; \
  -map &quot;[v]&quot; \
  -map &quot;[a]&quot; \
  -c:v libx264 -crf 23 \
  -c:a aac -b:a 192k \
  foo.mp4
  </code></pre>
  <p>There is a solution posted <a href="https://stackoverflow.com/questions/43550344/crop-video-into-a-4x4-grid-tiles-matrix-efficiently-via-command-line-ffmpeg">here</a> for <em>splitting</em> a single movie file into 16 4x4 pieces, but naturally I want to do the opposite! I can't quite work out in my own mind how I can knit together the necessary elements from my 2x2 code routines and the 4x4 split code into a satisfactory 4x4 solution. It may well be that the 16 individual movie files each have to be re-scaled downwards.</p>
  <p>Any ideas would be gratefully received, especially coding solutions which are readily tweakable to <em>any</em> matrix combination (e.g., 3x3, 5x5, etc).</p>
  <p>Thanks very much, Clive</p>