I have a batch of mp4 video files where the audio starts out in sync but gradually gets further and further out of sync, the further into the mp4 you play, eventually having the sound about 1 second behind the video at the end of the mp4.

I gather it's due to the original recording sound card recording just slightly off the correct frequency and that the audio needs to be re-sampled ever so slightly to bring it in sync.

The output of mplayer shows the file encoding information, as follows :-

Stupid question : would the original video track have been in 23.976 fps then converted in 25 forgetting to adjust the sound track ?
If this is possible then you should not do anything to sound track but just reverse the 23.976 to 25 conversion of the video track._________________

No suggestions?
I would hope that I can script up something using ffmpeg or mplayer/mencoder where I can simply set it going and have the videos fixed next day.
I would rather not have to use a GUI._________________...Lyall

I too experienced exactly this with a mp4 movie. ( fine at start, about a second audio delay by end).
It was fine playing on VLC on my PC, but my cheap PVR always has crappy increasing-delay.
I just solved it to my satisfaction, and thought Id share.
Heres how I did it.

1. Estimate delta in time at end of movie ( 1 second in this case).
2. Extract Audio to mp3. ( in windows, it was "

Code:

ffmpeg -i <filenmae> -acodec libmp3lame <output filanme>

" )
3. Use audacity's "Effect -> Change tempo" , to change length of audio track. ..Select entire track(s), then Change-tempo slider bar to select new end time ( = old end time)
3.a Extract as mp3
4. Use ffmpeg to remap ( replace) the new audio track over the old one..
"

I didnt put too much thought into my solution. but IMHO, I think shifting the tempo of the audio is smarter than decoding / re-encoding the video ( which is CPU intensive, and can reduce quality if not done optimally). I'd be surprised if there wasnt an cmd-line program floating around on internet than could alter audio tempo appropriately... maybe audacity has cmd-line options?
If not, Id be happy to write some simple code for you that does it (eg in cmd-line language 'octave') in exchange for a slab of good beer.

The MP4Box approach does not alter, change or re-encode audio or video. It simply muxes the two tracks with slightly altered time stamps for the video track. So the process can be "reversed" without any loss of the original signal. The process should require almost no CPU time and is solely limited to I/O throughput.

The proposed solution assumes that audio and video tracks start and end at the same time. If there is an offset between the two or if they have different running times this will obviously fail. In that case there is no way to automate this process as it requires human perception to sync the audio and video tracks.