用树莓派搭建网络摄像头

Spoony 54.7m2020-11-021577 次点击1 人收藏
用树莓派加上相机模块,可以实现实时监控的功能,再将时时视频流导入到 Web 界面中,就变成简单的网络摄像头(Webcam)。Web 端用 Nodejs + WebSocket,既可以进行实时播放,同时又方便进功能的扩展。

### Camera module 操作指令
raspbian 中系统自带对摄像头的操作指令,分别是raspistill, raspivid & raspistillyuv(这里有官方详细文档),其中 raspivid 是获取视频流的指令。
```
# 捕捉 5s 视频并输出到 video.h264,视频尺寸为 320x240
raspivid -t 5000 -w 320 -h 240 -o video.h264

# 实时监控并输出到 stdout
raspivid -t 0 -w 320 -h 240 -o -
```
获取实时监控之后的视频流输出之后,可以通过 ffmpeg 转化视频格式,输出到监听服务器。

### 安装 Nodejs/ffmpeg
```
# 1. install nodejs
wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb
node -v

# 2. install ffmpeg
## libx264 support
git clone git://git.videolan.org/x264
cd x264
./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl
make
sudo make install

## ffmpeg
git clone git://git.ffmpeg.org/ffmpeg
cd ffmpeg
sudo ./configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree
make # 这一步可能相当相当漫长,在我这里跑了有一个多小时
sudo make install
```

### Webcam
通过 ffmpeg 转换视频格式:
```
raspivid -t 0 -w 320 -h 240 -o - | ffmpeg -i - -s 320x240 -f mpeg1video \
-b 800k -r 30 http://127.0.0.1:8082/yourpassword
```
同时创建 Node server 监听 http://127.0.0.1:8082(参考:jsmpeg):
```
git clone https://github.com/phoboslab/jsmpeg.git webcam
cd webcam && node stream-server.js yourpassword

# Listening for MPEG Stream on http://127.0.0.1:8082/<secret>/<width>/<height>
# Awaiting WebSocket connections on ws://127.0.0.1:8084/
```
然后打开stream-example.html就可以看到实时监控画面了,如果是远程调试需要稍作更改:
```
# edit stream-example.html
var client = new WebSocket( 'ws://RASPI_LOCAL_IP:8084/' );
var player = new jsmpeg(client, {‌‌‌canvas:canvas});

# @/path/to/webcam
python -m SimpleHTTPServer 8080

# in your browser
http://RASPI_LOCAL_IP:8080
```

### 参考
[Setup Node.js on Raspberry Pi 2 B](http://www.andrewconnell.com/blog/setup-node-js-on-raspberry-pi-2-b)
[Installing FFMPEG for Raspberry Pi](http://www.jeffreythompson.org/blog/2014/11/13/installing-ffmpeg-for-raspberry-pi/)
[HTML5 LIVE VIDEO STREAMING VIA WEBSOCKETS](http://phoboslab.org/log/2013/09/html5-live-video-streaming-via-websockets)

https://www.v2ex.com/t/193950
收藏 ♥ 感谢
暂无回复

登录注册 后可回复。




› 相关内容关注微信公众号