Experiments in video capture

Example Frame From Jurassic ParkI recently reinstalled my Hauppauge Win-TV card in my server box, and have been working a bit at getting my webcam software up and running. But rather than just play with occasional image capture, I was wondering what the possibilities would be for recording live video for later playback. So, I embarked on a bit of experimentation using tidbits of software that I had lying around.

Note: My server is a 1ghz VIA Nehemiah chip, which isn’t fast enough to do software encoding of full video frames, so all experiments were performed at resolutions of 352×288 or 320×240. This is more directly comparable to ordinary VideoCD: watchable, but not archival quality. You can see an example frame on the right, at the size I normally use for video capture. Modern PDA’s can actually play back mpeg4 streams of this quality, so this might be comparable to what you’d get on one of these.

First of all, I installed ffmpeg. It has a video capture mode that works with the FreeBSD brooktree driver, so it seemed like it was a promising start. I ended up with the following script:

#!/bin/sh

BKTR_FORMAT=2    # 2 = NTSC
BKTR_DEV=1            # 1 = tuner device number... 0 is composite in...
export BKTR_FORMAT
export BKTR_DEV

# the driver doesn't know about the tuner, so you have to use this 
# separate commandline program to set the channel information.
# Channel 70 is the SciFi channel on my cable network.

bsdbktr_tvtune -s CABLEIRC -c 70

# Audio comes in on line, set it to be the recording source, but mute
# the output volume so we don't have to listen to it while recording.

mixer line 75
mixer recsrc =rec line
mixer vol 0

# use ffmpeg to write mpeg4 encoded video, with 800kb video, 128kb audio
# (encoded in mp2 format with stereo) for one hour.

ffmpeg -y -r 29.97 -s 352x288 -b 800 -vcodec mpeg4 -acodec mp2 -ab 128 -ac 2 -tvstd ntsc -t 1:0:0 output.avi

# restore volume to normal level
mixer vol 75

The net result: it works. It works rather well. The hour of Jurassic Park that I recorded off the air is of reasonable (given the relatively low resolution) quality, and takes up 468megabytes of space. The audio sync is pretty good, and doesn’t appear to drift significantly over the hour.

My next experiment was to try to write Video CD compliant MPEG files, so that they could be immediately burned onto a Video CD for playback in my DVD player. Unfortunately, this was less successful. While the video stream appeared to playback on my PC without difficulty, and vcdimager wrote the resulting cue and bin files without complaint, the resulting Video CD played back only spottily, with glitchy graphics and distorted sound.

Just for documentation, here is the ffmpeg line I used:

ffmpeg -t 1:0:0 -r 29.97 -tvstd ntsc -s 352x240 -b 1150 -ab 224 -f vcd -vcodec mpeg1video -acodec mp2 output.mpg

I also tried using mp3 format for audio, but the synchronization seems off: I suspect that the mp3 encoder has some signficant latency which is not properly accounted for.

I hope this information is useful to someone.