| « SQL Server Injection Cheat Sheet | Ubuntu Webcam » |
Setting up an Ubuntu webcam server
Sharing a webcam stream in Ubuntu is not the easiest thing, but it’s not too bad if you have some help. This tutorial will explain how to use the package webcam-server. It seems to work pretty well for me. I had to write the startup script myself, but I’m going to share that with you. To use webcam-server to it’s full potential, you should have Apache installed.
Follow up:
The first thing you will want to do is install the webcam-server package:
sudo apt-get install webcam-server
The webcam-server binary will be installed along with the java applet and html needed to host a live stream on a webpage.
Next, you will want to setup the startup script. This will allow you to control your webcam server as a daemon, and also start webcam-server at startup.
Open a new file in the /etc/init.d directory with your favorite editor. Nano is the easiest, so I’ll use that in the example:
sudo nano /etc/init.d/webcam-server
Write a starup script, or simply use this one:
#!/bin/sh
SERVER_BIN=webcam-server
LOCK_FILE=/var/lock/$SERVER_BIN
RTRN=0
OPTIONS='-v -g 320x240 -p 8888 -c hacktivision.com'
start() {
[ -f $LOCK_FILE ] && echo "$SERVER_BIN already started"
[ -f $LOCK_FILE ] && return
echo -n "Starting $SERVER_BIN: "
export LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so
nohup $SERVER_BIN $OPTIONS > /dev/null 2>/dev/null &
RTRN=$?
[ $RTRN -eq 0 ] && echo Started! || echo FAIL
[ $RTRN -eq 0 ] && touch $LOCK_FILE
}
stop() {
[ -f $LOCK_FILE ] || echo "$SERVER_BIN is not running"
[ -f $LOCK_FILE ] || return
echo -n "Stopping $SERVER_BIN: "
pkill -f "$SERVER_BIN $OPTIONS"
RTRN=$?
rm -f $LOCK_FILE
[ $RTRN -eq 0 ] && echo Stopped! || echo FAIL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RTRN=1
esac
exit $RTRN
Now you just need to make your startup script run when Ubuntu starts up. Use the following commands:
sudo chmod +x /etc/init.d/webcam-server
sudo update-rc.d webcam-server defaults
Let’s test our webcam server now. We will start it using our script, and then see if we can view the http image stream (we will check out the video stream later).
sudo /etc/init.d/webcam-server start
Open Firefox, or any web browser and navigate to http://localhost:8888/. You should see an image of what your webcam server is pointed at. In Firefox, if you hold down CTRL+SHIFT+R, you can almost get a stream going by constantly refreshing the image.
The rest of this post requires that Apache be installed. If Apache is not installed, install it. Basically, you want to run:
sudo apt-get install apache2
When you installed webcam-server, it put some web files on your hard drive. These files allow for a java app on a webpage to stream your webcam. We will assume that your webroot is /var/www. Replace /var/www with whatever webroot you want to use in the following code.
Copy the web files to your webroot
sudo cp /usr/share/doc/webcam-server/applet/* /var/www/
and test by going to http://localhost/webcam.html.
The java applet in the webcam.html file is, by default, configured to stream at 1 frame per second. It is also configured by default to use “localhost” as the domain. Here’s an example of a webcam.html file with a maximum FPS of 60 and hosted on hacktivision.com:
<html>
<head>
<title>WebCam</title>
</head>
<p align="center">
<a href="http://hacktivision.com" title="hacktivision.com - Ubuntu webcam server">Hacktivision</a>
</p>
<div align="center">
<APPLET CODE = "WebCamApplet.class" archive="applet.jar" WIDTH = "320" HEIGHT = "240">
<param name=URL value="http://hacktivision.com:8888">
<param name=FPS value="60">
<param name=width value="320">
<param name=height value="240">
</APPLET>
</div>
</body>
</html>
You should now be all set to show your webcam stream to the world!
Please, use the comments. Let me know if you have any problems. Call out my typos and bad grammar. Link to your site and show me how you use your webcam server.
15 comments
sudo: /etc/init.d/webcam-server: command not found
I am still somewhat new to linux and am not sure where I need to start looking to resolve this. Any help would be greatly appreciated.
Thanks,
Chris
Perhaps your /etc/init.d/webcam-server got messed up. Did you copy the code and paste it into /etc/init.d/webcam-server, or did you use the link to download the file and place it in /etc/init.d/?
If you copied and pasted the text, instead try right clicking and saving the startup script using the link with the text "use this one". Then, use the sudo command to copy the webcam-server script to /etc/init.d/.
If you don't need a startup script, simply run:
webcam-server
with no options. Then try visiting the localhost address I provided. Reply with your results.
/dev/video0: No such file or directory
unable to initialise camera
I am assuming that this is a problem with the camera but can't say that with 100% confidence. I am again at a loss for what to do. Thank you for your help and time, I look forward to your reply.
Chris
Has your webcam ever worked before? Does the file /dev/video0 actually exist on your hard drive?
You could try running:
webcam-server -d /dev/video1
or
webcam-server -d /dev/video2
if those files exist.
Chris
sudo chmod 777 /etc/init.d/webcam-server
then
sudo /etc/init.d/webcam-server start
sudo /etc/init.d/apache2 start
This post has 1093 feedbacks awaiting moderation...