title: FFMpeg Convert AVI to MP4 - Audio / Video Out of Sync
body: <p>I have an application that lets users record video from their webcams.  The users record 3 clips, which are then combined into one video for playback.</p>
  
  <p>I am having issues with the audio and video on the third clip sometimes being out of sync after converting to MP4.</p>
  
  <p>Here's how the process works now-</p>
  
  <p><strong>Combine AVI video files and insert transition videos in between using Mencoder to combine avi files.</strong></p>
  
  <pre><code>mencoder.exe -oac copy -ovc copy -idx -o test.avi c:\temp\*.avi 
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>Muxer frame buffer cannot allocate memory!
  
  1 duplicate frame(s)! 
  1 duplicate frame(s)! 
  1 duplicate frame(s)!
  1 duplicate frame(s)! 
  1 duplicate frame(s)! 
  1 duplicate frame(s)!
  </code></pre>
  
  <p>However, the video is combined to avi and this AVI plays back without any audio sync issues.</p>
  
  <p><strong>Reindex using mencoder</strong></p>
  
  <pre><code>mencoder.exe -idx TEST.AVI 
  </code></pre>
  
  <p>The video still plays fine at this point as an AVI.</p>
  
  <p><strong>Convert the AVI to MP4 using ffmpeg batch file:</strong></p>
  
  <pre><code>set FFMPEG_DATADIR=c:\Presets
  ffmpeg.exe -i "test.avi" -s 640x480 -y -strict experimental -acodec aac \
     -ab 128k -ac 2 -ar 48000 -vcodec libx264 -vpre medium -vpre ipod640 -r 24 \
     -g 48 -b 520000 -threads 64 "out.mp4"
  </code></pre>
  
  <p>This all has to be done programmatically (no video editing GUI software) using command line tools in Windows.</p>
  
  <p>Here are sample avi and mp4 files.  The audio / video sync happens around the 1:03 mark.  AVI doesn't get out of sync, while the mp4 does.</p>
  
  <p><a href="http://trtemp.s3.amazonaws.com/new.avi" rel="nofollow">http://trtemp.s3.amazonaws.com/new.avi</a></p>
  
  <p><a href="http://trtemp.s3.amazonaws.com/new.mp4" rel="nofollow">http://trtemp.s3.amazonaws.com/new.mp4</a></p>
  
  <p>Does anyone have any suggestions to fix this?</p>