Monday 5 October 2009

A Realtime Headache

UPDATE: The solution in this post was actually flawed in that the audio and video in the compressed videos ended up out of sync. See this post for my, hopefully, final solution.

I've spent a lot of time over the last few days banging my head against an annoying computer problem. I seem to have finally figured it out and so I thought I'd blog about it in case anyone else has a similar problem.

I've been slowly transferring a whole bunch of old VHS tapes on to the computer using a USB MPEG-2 encoder (specifically a Dazzle USB capture device). This produces large MPEG-2 files which are ideal for burning as a normal DVD. Sometimes though I'll have a bunch of short clips that are not really suited to putting on a DVD. As the MPEG-2 file format is relatively uncompressed even short clips can be quite large and so further compression is a good idea.

Now for reasons that are too complex to go into here I've been converting the MPEG-2 files to Windows Media Video files (actually to the VC-1 standard, using the WMV9 codec, which is the basis of the encoding on HD-DVD and Blu-ray disks) using a command line interface to the Windows Media Encoder. This has worked flawlessly for well over a year and then mysteriously broke about a fortnight ago.

Living in the UK the videos are in the PAL format and so I've been producing 25fps MPEG-2 files and keeping the same framerate when compressing further. When the encoding finishes the command line script produces a summary of what it has done, which includes the expected and actual framerate. They should both be 25fps -- 25 frames coming in each second and 25 frames going out. Strangely about a fortnight ago the actual framerate started fluctuating wildly. I tried encoding the same file a number of times and saw framerates varying from 8 to 19 but nothing approaching 25. Such weird framerates meant that the resulting videos were completely unwatchable.

I assumed that the problem was with the encoding of the video given that I could watch the original MPEG-2 file without any problems. So I reinstalled the Windows Media codecs and the encoder software all to no avail. I tried reinstalling the DVD software as I know that is used to read the MPEG-2 files. Still no joy. In the end I decided I'd do a complete format and reinstall of Windows. Annoyingly that still didn't work.

It turns out that the codec I was using to decode the MPEG-2 file works in realtime. That is it drops frames to make sure the video and audio are in sync. Now I can play a DVD (and hence MPEG-2 files) on the computer without any problems. But when encoding the files I'm both playing and encoding at once and that pushes the CPU usage high enough for frames to be dropped. What I can't figure out is why it suddenly became a problem. I'm assuming that something had switched the codecs being used but from what and why I don't know.

Anyway the fix was to find a non-realtime MPEG-2 codec. Fortunately the free ffdshow codecs will decode MPEG-2 fully irrespective of CPU load. So finally after about two weeks of banging my head against the problem I can get back to encoding videos again at the correct framerate.

The only problem seems to be that if I open an MPEG-2 file in Windows Media Player it no longer respects the aspect ratio and so plays wide screen footage as 4:3. Maybe there is a setting in ffdshow that I'm missing but it's a small price to pay especially as I can watch the files using normal DVD software at the correct aspect-ratio.

Post a Comment