How to fix the error PES packet size mismatch in FFmpeg -
I'm going to answer my own question, because the phrase PES packet size mismatch comes up regularly in posts relating to ffmpeg, but I've nowhere seen a satisfactory solution to it.
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 packet size mismatch will suddenly start repeating.
A solution is to concatenate them as .ts files (i.e. in their original 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 stream copy is used, outputting to .mp4 will often give worse picture quality than retaining the .ts format.
The following code does that, outputting either .mp4 video or .ts video (copy the code into a simple .bat batch file, then run that file) -
.
:: 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'>> 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
.
Addendum :
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.
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.
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.
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.
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.