Resync MPEG2 Audio
This is a command-line tool which analyses the audio/video stream timings in an MPEG2 file and produces an Avisynth script. It is not for the faint-hearted. But if you've exhausted all other options, and you know how to use Avisynth, ResyncMpeg2Audio might just help you do what it says in the title.
MPEG2 streams are inherently variable framerate. That is, the time interval between consecutive frames may change from frame to frame. To maintain sync with audio, MPEG2 streams carry timing information for each frame and each chunk of audio. The device playing back an MPEG2 stream is required to use this information to keep the audio in sync.
Avisynth is, at the time of writing, essentially a fixed framerate system. There is no way to encode varying time intervals between frames. Each clip only has a single framerate associated with it, which implies a fixed and uniform frame interval. The same is true of audio.
When using a tool like DGIndex to source MPEG2 footage into Avisynth, the frame timing information is discarded, and only the base framerate is preserved. Most MPEG2 streams have a very uniform frame rate anyway, so this doesn't result in major audio desync. A single shift is usually required, but once applied the audio remains aligned throughout the clip.
Some clips, however, contain highly variable frame rate. One example is footage captured with an analog input card directly to MPEG2, especially where the source material has "dirty" analog cuts from an old analog camcorder, and/or damaged noisy areas. In these cases the encoder may lose synchronization with the video signal for a few frames and thus output digitized frames at irregular intervals.
When processing such clips using DGIndex + Avisynth, especially long clips with more than an hour of footage, no amount of audio shift or stretch produces a satisfactory result: the audio might be 30 seconds too early at the start, 30 seconds too late in the middle and then 30 seconds too early again at the end (no exaggeration here...)
One possible solution is to just stick with formats and tools which properly support audio/video presentation timestamps to keep the streams in sync. But if you're reading this, it's probably because your tools, like pretty much all the tools I've seen, do not.
So, in order to keep the streams aligned in the average tool, one has to shift the streams relative to each other. ResyncMpeg2Audio keeps the video stream unchanged, and only shifts the audio. It does this by cutting the audio into multiple segments at appropriately chosen points, and shifting each segment to produce optimal audio/video alignment.
Starting with version 004, ResyncMpeg2Audio can analyze an MPEG2 file directly, outputting an AviSynth script which cuts the audio as computed and merges it with the video frames with proper alignment.