A Webcam setup with tcl scripts

by Theo Verelst

Running a Web Server ( www.theover.org [L1 ]) with Fedora 8/64 on a fairly fast machine I made some Tcl scripts as cgi´s.

On this page I record how I put up a webcam based on a good quality video cam (an old digital Sony above average consumer model with separate microphone) and a digitizer box (pinnacle 6mbit/S mpeg-2 TVDeLuxe USB, years old) which I interface on Linux with Dune.

The webcam is based on a Tcl script which is a cgi, placed in the cgi directory with the code on top to load the tcl interpreter from sh. That main script runs as apache user, and connects to a transfer script to get access to the main digitizer script (all three tcl) which starts dune on request, and must (as it is) run as root.

The latter is started from /etc/rc by adding:

   tclsh  /path/servcam.tcl > /path/mess 2>&1 &

Where servcam.tcl is:

set ss [socket -server servvid 1234]

proc servvid {s i p} {
    global argv
    puts "connection from $i"
    flush stdout
    fconfigure $s -translation binary
     set f [ open "| /usr/local/bin/dunerec -t dvd -i 1 -R - 2> /path/messdune" r]
    fconfigure $f -translation binary
    fileevent $s readable "puts \"read: \[gets $s\]\" ; flush stdout
        if \[eof $s\] {
        close $s
        close $f
        puts {End of file.}} "
    after 2000
    fileevent $s writable "fcopy $f $s -size 1024
        if \[eof $s\] {
        close $s
        close $f
        puts {End of file.}} "
}

vwait forever

Which is an adapted script I use for local network TV distribution.

The in-between script is socktostdout3b.tcl:

set s [socket localhost 1234]
fconfigure $s -translation binary -buffering full
fconfigure stdout -translation binary -buffering full
fileevent $s readable "puts -nonewline stdout \[read $s 2048\]; flush stdout"

# after 90 { flush stdout }

after 13000 exit

vwait forever

This script is supposed to stop transferring data from the dune script to the apache cgi script after a little over 10 seconds because I make 10 second movies on request, which are encoded and downloadable after these scripts have been run.

The main Tcl cgi script is:

#!/bin/sh
# \
exec tclsh "$0" "$@"
#

puts stdout "Content-type: text/html\n"
puts stdout "<html>"
puts stdout "<a href=\"http://www.theover.org/cgi-bin/web10sec.tcl\">This page</a> lets you create a mp4 file on my server of 10 seconds of video and sound, which you can after encoding has finished download from this page."
puts stdout "<P>Click on the below link for the previous capture"
puts stdout "<P>capturing and mp4 encoding 10 seconds of video and sound ..."
puts stdout "<P>"
flush stdout


# catch { exec tclsh /var/www/cgi-bin/socktostdout3b.tcl | ffmpeg -i - -b 390 -acodec libfaac -ab 64 -s 640x480 -ac 2 -y -t 10  /Fc6/home/theo/Bind/Docroot/Bird/cam.mp4  >& /tmp/out }
catch { exec tclsh /var/www/cgi-bin/socktostdout3b.tcl | ffmpeg -i - -acodec libfaac -ab 192 -qscale 2  -ac 2 -y -t 10  /Fc6/home/theo/Bind/Docroot/Bird/cam.mp4  >& /tmp/out }

puts stdout "done, result in <a href=\"http://www.theover.org/Bird/cam.mp4\">www.theover.org/Bird/cam.mp4</a>"
puts stdout "Reload this page (once only!) to start a new capture."
puts stdout "<P><a href=\"http://www.theover.org\">www.theover.org</a>"
puts stdout "</html>"
flush stdout
exit

Not the pinnacle of cgi´s of course but the whole setup works, the server and the cam and encoder can be switched off and on, and the whole thing works, via this url one gets a page which loads in 10 seconds to make a little movie:

   http://www.theover.org/cgi-bin/web10sec.tcl

Image TV Bird bird3.jpg

after which the download link appears to a mp4 movie playable with players which can handle those (like standard QuickTime). Via http://www.theover.org/cgi-bin/web10secmob.tcl a smaller, non VBR movie is made which can be viewed from a mobile phone, which I tried, and in fact even 10 seconds of a TV channel can be made viewable that way.

TV 2008-03-26: I'm making a page about apache server with tcl cgi scripts running on safe user as an experiment description, where the root script from here is replaced by another user, and an extra startup Tcl script is used which communicates over a socket to the main tcl cgi script.

BTW does anyone know whether Tcl and preferably Tk can run on a Nokia E90 Communicator? I installed python on one, but can´t find Tcl/Tk for it. Sources are cool, too.