Webcam + Linux

Webcam in AtticIn a past post, I described creating a 1980s videophone with Linux and a Brooktree chipset capture card. This particular card has three video inputs; two composite and one S-video. Since the S-video input takes care of the videophone’s camera, why not use one of the other composite inputs for a webcam?

Parts List

  • Small Video Camera with Composite-Out
  • L-bracket and zip ties for mounting the webcam
  • Cable TV 75 Ohm Coax Cable
  • Male RCA to Female F-Connector (Analog Video to Cable TV type Adapter)
  • Video Capture Card (I have one based on the Brooktree BT878 chipset I purchased from Computer Geeks awhile back which has composite and S-Video inputs.)
  • Computer running Debian Linux (Sarge) with a 2.6 kernel

Linux Kernel Setup
I am using the 2.6.10 kernel on Debian (Sarge) Linux. When compiling the 2.6.10 kernel, I had to add module support for the following devices:

Brooktree Capture Card with BT878 chipset (the capture card I’m using):
Device Drivers - Multimedia Devices
Module: Video for Linux

Device Drivers - Multimedia Devices - Video for Linux
Module: BT848 Video for Linux
Even though the module is named “BT848,” it is really meant for BT8xx cards.
Device Drivers - I2C Support
Module: I2C device Interface

After compiling the kernel with these modules and rebooting my server, I used dselect (a menu front end to the apt-get utility in Debian) to install module-init-tools and modconf so I could easily install and remove the drivers with the modconf command. (For the 2.6 kernel, you need to use the modconf command that comes with module-init-tools.) Using the modconf command, I added the Brooktree driver (under kernel/drivers/media/video -> bttv). This will add the drivers and allow them to automatically come up at boot.

BrookTree Video Capture Card

To test the video capture card, I connected my S-Video camera to the Brooktree capture card. I used dselect to install xawtv. Xawtv is a great program for checking your video capture card. Since I always forget, the three inputs on my BT capture card are addressable as follows:

Composite0
Composite1
S-Video

I logged in as root and typed startx. I opened a terminal window, and typed xawtv -device /dev/video0 (sometimes xawtv chooses a different v4l device, so I had to specify it.). No matter which input I selected (and I should’ve seen something on the Composite0 input), nothing would come up in Xawtv. All I saw was a blue or black blank screen. I typed xawtv -hwscan (make sure you are running this command from a shell within X, otherwise do xawtv -hwscan -display :0.0 ) and saw that I had Input/Output errors reported by v4l2. Then, I could hear my geek hobby cohort Doug’s voice in the back of my mind saying, “Move the capture card to another PCI slot!” Sure enough, that was the problem. I had forgotten that particular video capture card was choosy with its PCI slot. While the server was off, moving the capture card to another PCI slot solved my error problem. Xawtv finally gave me a nice video image coming from my camera.

Webcam Software
On the Linux box, I used dselect to install webcam. Then, I needed to configure the webcam program to grab an image from the BTTV capture card and sftp it to my web server. To do this, I modified the .webcamrc file (for example’s sake I put this in my /root/ directory, but you can place it in some user’s /home/ directory.) webcam reads .webcamrc as its configuration file. Below is an example of my .webcamrc:

[ftp]
host = girasoli.org
user = xxxx
#pass = xxxx
dir = public_html/
file = webcam.jpg
tmp = uploading.jpeg
passive = 0
debug = 3
auto = 0
local = 0
ssh = 1

[grab]
device = /dev/video0
#text = "webcam %Y-%m-%d %H:%M:%S"
text = SoliCam %Y-%m-%d %H:%M:%S
#infofile = filename
fg_red = 255
fg_green = 255
fg_blue = 255
width = 320
height = 240
delay = 3
#input = S-Video
input = Composite0
brightness = 100
norm = ntsc
rotate = 0
top = 0
left = 5
bottom = -1
right = -1
quality = 75
trigger = 0
once = 1

.webcamrc is configured to capture from Composite0 and sftp webcam.jpg to the public_html directory on girasoli.org using a fictional account. (Note the ssh=1 for using sftp.) The password has been disabled as the webcam program can’t authenticate via password when using ssh. To allow webcam to authenticate, I have to set up a passwordless ssh login. This is done by generating an ssh key on the webcam server and appending it to ~/.ssh/authorized_keys on the remote web server.

A side note for users that don’t want to use ftp/sftp and copy the image file locally: there are a couple variables you can change to do so. Set the “local=” variable to 1 instead of 0. This will copy the image file to the local directory defined in “dir=”.

Generating a SSH Key for SFTP

First, generate your key on the local webcam server:
ssh-keygen -t rsa
When prompted for a password, don’t enter one; just hit the enter key. This will generate a passwordless key called id_rsa, and a public key id_rsa.pub. Rename the id_rsa.pub key, copy it over to the machine you want to ssh to, and add it to ~/.ssh/authorized_keys like so:

Local Webcam Server:
cp id_rsa.pub local.key
Then, copy local.key securely to the remote web server.

Remote Web Server:
cat local.key >> .ssh/authorized_keys

Creating a Scheduled Script to Capture an Image from the Webcam

Now that authentication has been taken care of, we can create a script to capture an image every 15 minutes. First, create the script, webcam-script (for this example I’ll put it in /root/ — after all, the script will need root permissions to access the capture card unless you do something differently.) Here is my example of webcam-script:

#!/bin/bash
/usr/bin/webcam /root/.webcamrc > /dev/null 2>&1

Then, type crontab -e as root and make this entry to have webcam-script run every 15 minutes:

*/15 * * * * /root/webcam-script

Webcam ImageYou’re all set! A picture like this one should get uploaded to your web server every 15 minutes. In my case, I put the webcam up in the attic, and used the 75 Ohm coax cable (with RCA to F adapters) to run the video line from the attic camera to my Linux server in the basement. (In the picture on the right, you can see out the slats of the gable vent out to my driveway.) The coax cable worked great to bring the video signal down to the capture card in the Linux server. (If you’re using S-Video, you can also use my S-Video to CAT5 hack for a long cabling run.)

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.