title: ffmpeg selects shortest movie but leaves full length audio
body: <p>I've created a tool to create a grid of movies, like in the Brady Bunch opening. It's a cool, funny effect.</p>
  
  <p><a href="https://www.youtube.com/watch?v=e6bhi60xOig&amp;feature=youtu.be" rel="noreferrer">Here's a sample movie showing the grid.</a> </p>
  
  <p><a href="https://i.stack.imgur.com/O6vGD.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/O6vGD.jpg" alt="Grid of movies in ffmpeg output"></a></p>
  
  <p>The problem is that ffmpeg is choosing the <em>shortest</em> movie to determine the maximum length of overlaid videos, but still using the <em>full</em> length of the first audio track to determine the overall output movie length.</p>
  
  <p>So all my videos stop moving when the shortest one ends, but the audio plays on for full length of the first movie.</p>
  
  <p>How can I either (1) set the length of the output movie to the <em>longest input movie</em> or (2) match the audio length to the shortest movie length as well?</p>
  
  <p><a href="https://gist.github.com/wanderingstan/5ec0a6802d87f3934796" rel="noreferrer">Gist of my script</a>.</p>
  
  <p>Based on <a href="https://trac.ffmpeg.org/wiki/Create%20a%20mosaic%20out%20of%20several%20input%20videos" rel="noreferrer">this ffmpeg example usage</a> which exhibits the same audio problem:</p>
  
  <pre><code>ffmpeg
      -i 1.avi -i 2.avi -i 3.avi -i 4.avi
      -filter_complex "
          nullsrc=size=640x480 [base];
          [0:v] setpts=PTS-STARTPTS, scale=320x240 [upperleft];
          [1:v] setpts=PTS-STARTPTS, scale=320x240 [upperright];
          [2:v] setpts=PTS-STARTPTS, scale=320x240 [lowerleft];
          [3:v] setpts=PTS-STARTPTS, scale=320x240 [lowerright];
          [base][upperleft] overlay=shortest=1 [tmp1];
          [tmp1][upperright] overlay=shortest=1:x=320 [tmp2];
          [tmp2][lowerleft] overlay=shortest=1:y=240 [tmp3];
          [tmp3][lowerright] overlay=shortest=1:x=320:y=240
      "
      -c:v libx264 output.mkv
  </code></pre>