树莓派之网络摄像头配置

安装支持库

1
$ sudo aptitude install libjpeg8-dev imagemagick libv4l-dev cmake

编译mjpg-streamer

当前用户是pi,目录是/home/pi/。

1
2
3
4
5
$ git clone https://github.com/jacksonliam/mjpg-streamer.git
$ cd mjpg-streamer/mjpg-streamer-experimental/
$ sudo make clean all
$ cd ..
$ sudo chown -R pi:pi mjpg-streamer-experimental/

编写启动脚本

1
$ sudo vi /etc/init.d/webcam

脚本内容

修改代码中第15行的DAEMON变量为mjpg-streamer的执行目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh

# /etc/init.d/webcam

### BEGIN INIT INFO
# Provides: webcam
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: mjpg_streamer for webcam
# Description: Streams /dev/video0 to http://IP/?action=stream
### END INIT INFO

DAEMON=/home/pi/mjpg-streamer/mjpg-streamer-experimental
WC_USER=user
WC_PASSWORD=password
WC_PORT=12345

f_message(){
echo "[+] $1"
}

# Carry out specific functions when asked to by the system
case "$1" in
start)
f_message "Starting mjpg_streamer"
$DAEMON/mjpg_streamer -b -i "$DAEMON/input_uvc.so" -o "$DAEMON/output_http.so -w $DAEMON/www -c $WC_USER:$WC_PASSWORD -p $WC_PORT"
sleep 2
f_message "mjpg_streamer started"
;;
stop)
f_message "Stopping mjpg_streamer…"
killall mjpg_streamer
f_message "mjpg_streamer stopped"
;;
restart)
f_message "Restarting daemon: mjpg_streamer"
killall mjpg_streamer
$DAEMON/mjpg_streamer -b -i "$DAEMON/input_uvc.so" -o "$DAEMON/output_http.so -w $DAEMON/www -c $WC_USER:$WC_PASSWORD -p $WC_PORT"
sleep 2
f_message "Restarted daemon: mjpg_streamer"
;;
status)
pid=`ps -A | grep mjpg_streamer | grep -v "grep" | grep -v mjpg_streamer. | awk '{print $1}' | head -n 1`
if [ -n "$pid" ];
then
f_message "mjpg_streamer is running with pid ${pid}"
f_message "mjpg_streamer was started with the following command line"
cat /proc/${pid}/cmdline ; echo ""
else
f_message "Could not find mjpg_streamer running"
fi
;;
*)
f_message "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
exit 0

默认登录的用户名是user,密码是password,端口是12345(可以在代码中第16,17,18行修改)。

赋予脚本执行权限

1
$ sudo chmod +x /etc/init.d/webcam

配置脚本开机启动

1
$ sudo update-rc.d webcam defaults

启动网络摄像头后台程序

1
$ sudo /etc/init.d/webcam start

查看网络摄像头运行状态

1
$ sudo /etc/init.d/webcam status

重启网络摄像头后台程序

1
$ sudo /etc/init.d/webcam restart

停止网络摄像头后台程序

1
$ sudo /etc/init.d/webcam stop

浏览器中查看网络摄像头

访问地址:http://树莓派的IP地址:自定义的端口号