Wednesday, 11 November 2009
Fixing out-of-sync downloaded Google Video MP4s with QuickTime 7 and QT Sync
Something's been bugging me for years. Google Video has a feature where you can download some videos in .mp4 container format. The problem is that these .mp4 files always have the video out-of-sync with the audio. It's especially noticeable on longer videos, but appears to apply to all videos downloadable from Google Video. Tonight, I decided I was finally going to find a solution, and it appears that I have.
First stop: Google
I was able to quickly find this Google Video Help discussion on Google Groups from 2007, where Dnarre had worked out what the problem was:
I have the same problem. I wanted to watch Dan Rather Reports: Into Africa (released 10/16/07, just a week ago) but the audio is out of sync. After a bit of research I think I see what the problem is.
Using an mp4 capable tool (mp4box) I gathered some detailed info about this video file. I find that the video and audio streams are listed as having different durations. The video is 00:53:13.633 and the audio is 00:53:10.398, a difference of over 3 seconds, which is most visible near the end of the file. I also gathered the video framerate, which is listed as being 29.969 frames per second.
Okay, now for a bit of math... If you divide the duration of the video stream (00:53:13.633) by the duration of the audio stream (00:53:10.398) you get a ratio of about 1.001014, which is how much longer the video is from the audio. If you multiply this ratio (1.001014) by the listed framerate of 29.969 you get 29.999388, which is very close to an even 30 frames per second (fps).
Technically speaking, it seems to me that the problem with these downloadable Google video files is that the video and audio streams are being encoded and/or listed at two different framerates -- one at 30 fps and the other at 29.969 fps, and so the video and audio are not being played at the same rate, leaving them out of sync.
This was the key that pointed me in the right direction. Now all I needed to do was figure out how to get the audio and video streams to be the same length, and problem solved!
Next stop: QT Sync
Another quick trip to Google led me to QT Sync, a free QuickTime player with additional syncing options built-in. I downloaded it, read the manual and leapt in. First I tried the obvious, which was setting the audio track to the same duration as the video track; but this resulted in audio that sounds like QuickTime or iTunes when you're fast-forwarding something. So then I tried the other way — setting the video track to the same length as the audio track — but the resulting video was much too slow. What I needed was to extract the individual tracks and then get them to line up.
On to QuickTime 7
In Snow Leopard, QuickTime Player X replaces QuickTime Player 7 entirely with a new version written from the ground up. This new version, however, does not yet have all the same features as the previous version — features needed to extract the video and audio tracks. Fortunately Apple has included an updated version of the previous QuickTime player as an optional install in Snow Leopard to handle all of those features. I already had it installed, so I just launched it.

Bringing up the Movie Properties window, you can see the two individual tracks, and sure enough the track lengths are different. Highlighting a track and clicking the "Extract" button creates a new untitled movie with only the extracted track. I did this to both of them, but saved only the video track to disk.
Back to QT Sync
Next, I loaded the saved video-only file in QT Sync. Clicking up to the Edit menu, I chose "Change Movie Playback Speed...", and entered the length of the audio track into the new time field and clicked OK. Then I saved the file to disk and went back, once more, to QuickTime Player 7.

Long time no see, QuickTime 7
I went back to the window with the audio track, did a Select All, copied the selection to the clipboard, then closed the window. I then opened the video-only movie with the changed playback speed in QuickTime Player 7, went up to the Edit menu and chose "Add to Movie". It pasted the soundtrack on top of that movie, and a final save to disk meant that it was done.
The great thing about this solution is that all the tools are free — QuickTime Player 7 comes with the OS and QT Sync is a free download. The only problem with this method is that there's no easy way to automate it, since each video will have a different length. But so long as you don't mind a minute of work, your Google Video downloads will be fixed.
(And if you do find a way to automate it, please share!)