title: FFmpeg concat video and audio out of sync
body: <p>Joining multiple files using ffmpeg <a href="https://trac.ffmpeg.org/wiki/Concatenate" rel="noreferrer">concat</a> seems to result in a mismatch of the timestamps or offsets for the audio. I've tried with several videos and noticed the same problem for h.264 / MP4.</p>
  
  <p>Using <code>concat</code> <em>and encoding</em> the video seems to work fine. The audio stays in sync as ffmpeg does the full conversion calculations and seems to get everything right.</p>
  
  <p>However, simply concatenating the videos without any transformation or encoding results in a slowly increasing sync issue. Obviously, encoding the videos rather than simply joining them will result in a loss of information/quality so I would rather find a way around this problem.</p>
  
  <p>I've <a href="https://ffmpeg.org/ffmpeg.html" rel="noreferrer">tried several flags</a> to sort out this problem that appears to be based on the timestamps. None of these seem to correct the problem though.</p>
  
  <pre><code>ffmpeg -f concat -fflags +genpts -async 1 -i segments.txt test.mov
  ffmpeg -auto_convert 1 -f concat -fflags +genpts -async 1 -i segments.txt -c copy test2.mov
  ffmpeg -f concat -i segments.txt -c copy -fflags +genpts test3.mp4
  ffmpeg -f concat -fflags +genpts -async 1 -i segments.txt -copyts test4.mov
  ffmpeg -f concat -i segments.txt -copyts test5.mov
  ffmpeg -f concat -i segments.txt -copyts -c copy test6.mov
  ffmpeg -f concat -fflags +genpts -i segments.txt -copyts -c copy test7.mov
  </code></pre>
  
  <p><em>Note: all other questions that I could find on SO seem to "fix" the problem by simply encoding the videos over again. Not a good solution.</em></p>
  
  <h2>Update</h2>
  
  <p>I realized the concat wasn't the problem. The original set of clips had mis-matched timestamps. Somehow concat + encoding fixed the issue, but I don't want to re-encode the videos and loose quality each time.</p>
  
  <pre><code>ffmpeg -y -ss 00:00:02.750 -i input.MOV -c copy -t 00:00:05.880 output.MOV
  </code></pre>
  
  <p>Which resulted in the following data</p>
  
  <pre><code>ffprobe -v quiet -show_entries stream=start_time,duration output.MOV
  
  start_time=-0.247500
  duration=6.131125
  start_time=-0.257333
  duration=6.155333
  </code></pre>
  
  <p>Since then I've tried to use <code>-to</code>m and <code>-t</code> in different places along with <code>-af apad -c:v copy</code> and I've still failed to get the duration to be the same.</p>
  
  <h3><a href="https://gist.github.com/Xeoncross/ec27c8a7f12c137a85fd" rel="noreferrer">Here is the full ffprobe output</a></h3>
  
  <h3><a href="https://www.diffchecker.com/kwxdlyfy" rel="noreferrer">Here is the original (red) vs the segment (green)</a></h3>
  
  <h3>Detailed Sample Files</h3>
  
  <p>I recorded a sample video, added the commands to chop it up, then concat it. <a href="http://davidpennington.me/share/audio_sync_test_video.zip" rel="noreferrer">http://davidpennington.me/share/audio_sync_test_video.zip</a></p>