title: How do I fix the "PES packet size mismatch" error in FFmpeg?
body: <p>How to fix the error <strong>PES packet size mismatch</strong> in FFmpeg -</p>
  
  <p>I'm going to answer my own question, because the phrase <em>PES packet size mismatch</em> comes up regularly in posts relating to ffmpeg, but I've nowhere seen a satisfactory solution to it.  </p>
  
  <p>It usually figures in a problem involving .TS transport stream files: either in relation to concatenating such files, or relating to re-muxing them (from .ts to .mp4). Somewhere in the output from ffmpeg, the deadly phrase <em>packet size mismatch</em> will suddenly start repeating.</p>
  
  <p>A solution is to concatenate them as .ts files (i.e. in their <em>original</em> format), then take the output .ts file, split it into a video file (.ts) and an audio file (.ts), then remux them (to either .ts or .mp4) using the "itsoffset" option.  Even if <strong>stream copy</strong> is used, outputting to .mp4 will often give worse picture quality than retaining the .ts format.</p>
  
  <p>The following code does that, outputting either .mp4 video or .ts video (copy the code into a simple .bat <strong>batch file</strong>, then run that file) -</p>
  
  <p>.  </p>
  
  <pre><code>:: Program Location
  SET ffmpeg="C:\Program Files\FFmpeg\ffmpeg.exe" -hide_banner  
  
  
  ::  STEP 1 -
  
  ::  Create File List
  IF EXIST mylist.txt DEL mylist.txt
  FOR %%i IN (*.ts) DO ECHO file '%%i'&gt;&gt; mylist.txt
  
  ::  Concatenate Files : TS
  %ffmpeg%  -f concat  -safe 0  -i mylist.txt  -c copy  -threads 1  out.ts
  
  
  ::  STEP 2 -
  
  ::  Extract Video stream
  %ffmpeg%  -i out.ts  -vcodec copy  -an  -sn  -threads 1  video.ts
  
  ::  Extract Audio stream
  %ffmpeg%  -i out.ts  -acodec copy  -vn  -sn  -threads 1  audio.ts
  
  
  ::  STEP 3 -
  
  ::  Combine Video and Audio streams (with .MP4 options)
  SET mapping=-i video.ts -itsoffset -0  -i audio.ts   -map 0:v -map 1:a
  SET options=-flags global_header  -movflags faststart  -threads 1
  %ffmpeg%  %mapping%  -c:v copy -c:a copy  %options%  output.mp4
  
  ::  Combine Video and Audio streams (with .TS options)
  SET mapping=-i video.ts -itsoffset -0  -i audio.ts  -map 0:v -map 1:a
  SET options=-threads 1
  %ffmpeg%  %mapping%  -c:v copy -c:a copy  %options%  output.ts
  </code></pre>
  
  <p>.  </p>
  
  <p><strong>Addendum</strong> : </p>
  
  <p>There seems to be some dispute about my suggested solution, as detailed in the Comments below. It seems to be being said that my solution is ignoring the fact that data is missing in the source files.</p>
  
  <p>I think the least I can do is admit that since ffmpeg is reporting an error in the source files, with its 'packet size mismatch' warning, the objection raised in the Comments might be valid.</p>
  
  <p>However, even if data is missing, my suggested routine will at least give you a file which will play in most media players. In many cases, there will not even be an audible or visual fault at the join point specified in the reported error. </p>
  
  <p>It's difficult to see how the missing data might be restored, but do please chip in with suggestions. There must be scope for improving my script, because so little attention has been paid to this type of fault previously.</p>
  
  <p>Happily, it seems that this type of error will NOT cause the sound to lose synchronisation with the picture. So the audio after the join-point will not go out-of-sync, even if some data is missing at the join.</p>