【树莓派不吃灰】基础篇⑲ 搭建usb摄像头MJPG-streamer图片流监控,支持远程视频监控访问

news/2024/11/22 19:47:07/

在这里插入图片描述

目录

    • 1. 前言
    • 2. 识别摄像头
    • 3. MJPG-streamer方案
      • 3.1 什么是 MJPG?
      • 3.2 MJPG 的优点?
      • 3.2 MJPG 的缺点?
    • 4. 搭建usb摄像头监控
      • 4.1 开启树莓派摄像头开关
      • 4.2 查看设备文件
      • 4.3 安装必要的库
      • 4.4 下载 mjpg-streamer 安装文件
      • 4.5 切换到 /mjpg-streamer/mjpg-streamer-experimental 下编译
      • 4.6 安装
      • 4.7 开启摄像头图片流功能,并建立http服务器
      • 4.8 输入 http://树莓派ip:8080/stream_simple.html
    • 5. 设置开机自启动
      • 5.1 创建脚本
      • 5.2 测试脚本
      • 5.3 修改service文件
      • 5.4 复制文件到/etc/systemd/system目录
      • 5.5 设置服务为开机自启动
    • 6. MJPG-streamer技术细节
    • 7. 远程监控访问
      • 7.1 配置frp客户端
      • 7.2 配置frp服务器
      • 7.3 访问一下 http://公网ip:vhost_http_port 端口号/ 查看效果
    • 8. 总结

  • ❤️ 博客主页 单片机菜鸟哥,一个野生非专业硬件IOT爱好者 ❤️
  • ❤️ 本篇创建记录 2022-11-12 ❤️
  • ❤️ 本篇更新记录 2022-11-12 ❤️
  • 🎉 欢迎关注 🔎点赞 👍收藏 ⭐️留言 📝
  • 🙏 此博客均由博主单独编写,不存在任何商业团队运营,如发现错误,请留言轰炸哦!及时修正!感谢支持!
  • 🔥 Arduino ESP8266教程累计帮助过超过1W+同学入门学习硬件网络编程,入选过选修课程,刊登过无线电杂志🔥

1. 前言

家里刚好有一个闲置好几年的usb摄像头,想着如何把它也接入到树莓派中来,直接开整。

2. 识别摄像头

usb摄像头插入树莓派usb插口,输入命令:

lsusb

pi@raspberrypi:~ $ lsusb
Bus 001 Device 004: ID 13d3:784b IMC Networks XHC Camera
Bus 001 Device 003: ID 0424:ec00 Microchip Technology, Inc. (formerly SMSC) SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Microchip Technology, Inc. (formerly SMSC) SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
pi@raspberrypi:~ $ 

识别到了 IMC Networks XHC Camera 摄像头。

3. MJPG-streamer方案

github地址:https://github.com/jacksonliam/mjpg-streamer

简单地说,MJPG-Streamer 是一个 JPEG 文件的传输流。它最常用的用途就是采集摄像头的数据,然后启动 http server,用户就可以通过浏览器查看图像数据了(多个连续图片不断展示就人眼上感觉就是视频)。

3.1 什么是 MJPG?

Motion JPEG,简称 MJPG。JPEG是静态图片的编码格式。
MJPG 是动态的视频编码格式,可以简单地理解:MJPG 就是把多个 JPEG 图片连续显示出来

仔细一想,跟ESP32-CAM的视频图片流设计思路不谋而合。

3.2 MJPG 的优点?

很多摄像头本身就支持 JPEG、MJPG,所以处理器不需要做太多处理。一般的低性能处理器就可以传输 MJPG 视频流。

3.2 MJPG 的缺点?

MJPG 只是多个 JPEG 图片的组合,它不考虑前后两帧数据的变化,总是传输一帧完整的图像,传输带宽要求高

H264 等真正的视频格式,会考虑前后两帧数据的变化,只传输变化的数据,传输带宽要求低

其次,MJPG-Streamer已经多年不更新了

4. 搭建usb摄像头监控

4.1 开启树莓派摄像头开关

输入命令

sudo raspi-config

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.2 查看设备文件

输入命令

ls /dev/video*

pi@raspberrypi:/ $  ls /dev/video*              
/dev/video0   /dev/video11  /dev/video14  /dev/video18  /dev/video22
/dev/video1   /dev/video12  /dev/video15  /dev/video20  /dev/video23
/dev/video10  /dev/video13  /dev/video16  /dev/video21  /dev/video31
pi@raspberrypi:/ $ ^C

4.3 安装必要的库

输入命令

sudo apt-get install subversion libjpeg9-dev imagemagick libv4l-dev cmake git

pi@raspberrypi:/ $ sudo apt-get install subversion libjpeg9-dev imagemagick libv4l-dev cmake git
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
git is already the newest version (1:2.30.2-1).
imagemagick is already the newest version (8:6.9.11.60+dfsg-1.3).
libjpeg9-dev is already the newest version (1:9b-1).
libv4l-dev is already the newest version (1.20.0-2).
subversion is already the newest version (1.14.1-3+deb11u1).
cmake is already the newest version (3.18.4-2+rpt1+rpi1+deb11u1).
0 upgraded, 0 newly installed, 0 to remove and 47 not upgraded.
pi@raspberrypi:/ $ 

4.4 下载 mjpg-streamer 安装文件

输入命令

sudo git clone https://github.com/jacksonliam/mjpg-streamer.git

pi@raspberrypi:/ $ sudo git clone https://github.com/jacksonliam/mjpg-streamer.git
Cloning into 'mjpg-streamer'...
remote: Enumerating objects: 2964, done.
remote: Total 2964 (delta 0), reused 0 (delta 0), pack-reused 2964
Receiving objects: 100% (2964/2964), 3.48 MiB | 59.00 KiB/s, done.
Resolving deltas: 100% (1885/1885), done.
pi@raspberrypi:/ $ 

在这里插入图片描述

4.5 切换到 /mjpg-streamer/mjpg-streamer-experimental 下编译

切换到目标目录后输入命令:

sudo make all

pi@raspberrypi:~/mjpg-streamer/mjpg-streamer-experimental $ sudo make all
[ -d _build ] || mkdir _build
[ -f _build/Makefile ] || (cd _build && cmake  -DCMAKE_BUILD_TYPE=Release ..)
make -C _build
make[1]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[2]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[  4%] Building C object CMakeFiles/mjpg_streamer.dir/mjpg_streamer.c.o
[  8%] Building C object CMakeFiles/mjpg_streamer.dir/utils.c.o
[ 13%] Linking C executable mjpg_streamer
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 13%] Built target mjpg_streamer
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 17%] Building C object plugins/input_file/CMakeFiles/input_file.dir/input_file.c.o
[ 21%] Linking C shared library input_file.so
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 21%] Built target input_file
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 26%] Building C object plugins/input_http/CMakeFiles/input_http.dir/input_http.c.o
[ 30%] Building C object plugins/input_http/CMakeFiles/input_http.dir/misc.c.o
[ 34%] Building C object plugins/input_http/CMakeFiles/input_http.dir/mjpg-proxy.c.o
[ 39%] Linking C shared library input_http.so
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 39%] Built target input_http
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 43%] Building C object plugins/input_uvc/CMakeFiles/input_uvc.dir/dynctrl.c.o
[ 47%] Building C object plugins/input_uvc/CMakeFiles/input_uvc.dir/input_uvc.c.o
[ 52%] Building C object plugins/input_uvc/CMakeFiles/input_uvc.dir/jpeg_utils.c.o
[ 56%] Building C object plugins/input_uvc/CMakeFiles/input_uvc.dir/v4l2uvc.c.o
[ 60%] Linking C shared library input_uvc.so
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 60%] Built target input_uvc
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 65%] Building C object plugins/output_file/CMakeFiles/output_file.dir/output_file.c.o
[ 69%] Linking C shared library output_file.so
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 69%] Built target output_file
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 73%] Building C object plugins/output_http/CMakeFiles/output_http.dir/httpd.c.o
[ 78%] Building C object plugins/output_http/CMakeFiles/output_http.dir/output_http.c.o
[ 82%] Linking C shared library output_http.so
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 82%] Built target output_http
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 86%] Building C object plugins/output_rtsp/CMakeFiles/output_rtsp.dir/output_rtsp.c.o
[ 91%] Linking C shared library output_rtsp.so
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 91%] Built target output_rtsp
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 95%] Building C object plugins/output_udp/CMakeFiles/output_udp.dir/output_udp.c.o
[100%] Linking C shared library output_udp.so
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[100%] Built target output_udp
make[2]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[1]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
pi@raspberrypi:~/mjpg-streamer/mjpg-streamer-experimental $ 

等待一会等待编译成功。

这里有几个地方需要注意一下。
在这里插入图片描述

4.6 安装

输入命令

sudo make install

pi@raspberrypi:~/mjpg-streamer/mjpg-streamer-experimental $ sudo make install
make -C _build install
make[1]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[2]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 13%] Built target mjpg_streamer
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 21%] Built target input_file
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 39%] Built target input_http
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 60%] Built target input_uvc
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 69%] Built target output_file
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 82%] Built target output_http
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[ 91%] Built target output_rtsp
make[3]: Entering directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
make[3]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
[100%] Built target output_udp
make[2]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/bin/mjpg_streamer
-- Set runtime path of "/usr/local/bin/mjpg_streamer" to "/usr/local/lib/mjpg-streamer"
-- Up-to-date: /usr/local/share/mjpg-streamer/www
-- Up-to-date: /usr/local/share/mjpg-streamer/www/control.htm
-- Up-to-date: /usr/local/share/mjpg-streamer/www/java_simple.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/static_simple.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/jquery.rotate.js
-- Up-to-date: /usr/local/share/mjpg-streamer/www/java.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/favicon.ico
-- Up-to-date: /usr/local/share/mjpg-streamer/www/javascript_simple.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/example.jpg
-- Up-to-date: /usr/local/share/mjpg-streamer/www/jquery.ui.custom.css
-- Up-to-date: /usr/local/share/mjpg-streamer/www/fix.css
-- Up-to-date: /usr/local/share/mjpg-streamer/www/index.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/javascript_motiondetection.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/videolan.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/LICENSE.txt
-- Up-to-date: /usr/local/share/mjpg-streamer/www/JQuerySpinBtn.css
-- Up-to-date: /usr/local/share/mjpg-streamer/www/style.css
-- Up-to-date: /usr/local/share/mjpg-streamer/www/stream.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/favicon.png
-- Up-to-date: /usr/local/share/mjpg-streamer/www/bodybg.gif
-- Up-to-date: /usr/local/share/mjpg-streamer/www/jquery.ui.widget.min.js
-- Up-to-date: /usr/local/share/mjpg-streamer/www/stream_simple.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/jquery.js
-- Up-to-date: /usr/local/share/mjpg-streamer/www/spinbtn_updn.gif
-- Up-to-date: /usr/local/share/mjpg-streamer/www/javascript.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/jquery.ui.tabs.min.js
-- Up-to-date: /usr/local/share/mjpg-streamer/www/static.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/functions.js
-- Up-to-date: /usr/local/share/mjpg-streamer/www/JQuerySpinBtn.js
-- Up-to-date: /usr/local/share/mjpg-streamer/www/sidebarbg.gif
-- Up-to-date: /usr/local/share/mjpg-streamer/www/rotateicons.png
-- Up-to-date: /usr/local/share/mjpg-streamer/www/java_control.html
-- Up-to-date: /usr/local/share/mjpg-streamer/www/cambozola.jar
-- Up-to-date: /usr/local/share/mjpg-streamer/www/jquery.ui.core.min.js
-- Installing: /usr/local/lib/mjpg-streamer/input_file.so
-- Installing: /usr/local/lib/mjpg-streamer/input_http.so
-- Installing: /usr/local/lib/mjpg-streamer/input_uvc.so
-- Installing: /usr/local/lib/mjpg-streamer/output_file.so
-- Installing: /usr/local/lib/mjpg-streamer/output_http.so
-- Installing: /usr/local/lib/mjpg-streamer/output_rtsp.so
-- Installing: /usr/local/lib/mjpg-streamer/output_udp.so
make[1]: Leaving directory '/home/pi/mjpg-streamer/mjpg-streamer-experimental/_build'
pi@raspberrypi:~/mjpg-streamer/mjpg-streamer-experimental $ 

这里面可以关注几个信息:

  • html、js、css等等文件。意味着我们可以自定义效果。这里其实就是构造了一个http web服务器。
  • /usr/local/lib/mjpg-streamer/ 下面包括了输入和输出文件。

4.7 开启摄像头图片流功能,并建立http服务器

输入命令:

./mjpg_streamer -i "./input_uvc.so -r 1920 x 1080 -f 30 " -o “./output_http.so -w ./www”

MJPG-Streamer 将图像的来源视为输入,将图像的展示视为输出。
每一种输入或输出是都抽象为一个插件
上面的例子中:-i 指定输入的插件为 input_uvc.so,此插件会从摄像头中采集图像。-r 指定分辨率(可以根据自己摄像头的参数来调整,但是外网来说分辨率越大数据流量越大,需要考虑)、-f 指定 fps。-o 指定输出的插件为 output_http.so,此插件会启动一个 http server。

输入参数:

mjpg_streamer -i 'input_uvc.so [options]'---------------------------------------------------------------
Help for input plugin..: UVC webcam grabber
---------------------------------------------------------------
The following parameters can be passed to this plugin:[-d | --device ].......: video device to open (your camera)
[-r | --resolution ]...: the resolution of the video device,can be one of the following strings:QSIF QCIF CGA QVGA CIF VGA SVGA XGA SXGA or a custom value like the followingexample: 640x480
[-f | --fps ]..........: frames per second(activates YUYV format, disables MJPEG)
[-m | --minimum_size ].: drop frames smaller then this limit, usefulif the webcam produces small-sized garbage framesmay happen under low light conditions
[-e | --every_frame ]..: drop all frames except numbered
[-n | --no_dynctrl ]...: do not initalize dynctrls of Linux-UVC driver
[-l | --led ]..........: switch the LED "on", "off", let it "blink" or leaveit up to the driver using the value "auto"
---------------------------------------------------------------[-t | --tvnorm ] ......: set TV-Norm pal, ntsc or secam
---------------------------------------------------------------Optional parameters (may not be supported by all cameras):[-br ].................: Set image brightness (auto or integer)
[-co ].................: Set image contrast (integer)
[-sh ].................: Set image sharpness (integer)
[-sa ].................: Set image saturation (integer)
[-cb ].................: Set color balance (auto or integer)
[-wb ].................: Set white balance (auto or integer)
[-ex ].................: Set exposure (auto, shutter-priority, aperature-priority, or integer)
[-bk ].................: Set backlight compensation (integer)
[-rot ]................: Set image rotation (0-359)
[-hf ].................: Set horizontal flip (true/false)
[-vf ].................: Set vertical flip (true/false)
[-pl ].................: Set power line filter (disabled, 50hz, 60hz, auto)
[-gain ]...............: Set gain (auto or integer)
[-cagc ]...............: Set chroma gain control (auto or integer)
---------------------------------------------------------------

输出参数:

mjpg_streamer [input plugin options] -o 'output_http.so [options]'---------------------------------------------------------------
The following parameters can be passed to this plugin:[-w | --www ]...........: folder that contains webpages in flat hierarchy (no subfolders)
[-p | --port ]..........: TCP port for this HTTP server
[-l ] --listen ]........: Listen on Hostname / IP
[-c | --credentials ]...: ask for "username:password" on connect
[-n | --nocommands ]....: disable execution of commands
---------------------------------------------------------------

执行命令:

pi@raspberrypi:~/mjpg-streamer/mjpg-streamer-experimental $ ./mjpg_streamer -i "./input_uvc.so -r 1920x1080 -f 30 " -o "./output_http.so -w ./www"
MJPG Streamer Version: git rev: 310b29f4a94c46652b20c4b7b6e5cf24e532af39i: Using V4L2 device.: /dev/video0i: Desired Resolution: 1920 x 1080i: Frames Per Second.: 30i: Format............: JPEGi: TV-Norm...........: DEFAULTi: The specified resolution is unavailable, using: width 640 height 480 instead 
UVCIOC_CTRL_ADD - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Mode: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Frequency: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Disable video processing: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Raw bits per pixel: Inappropriate ioctl for device (25)o: www-folder-path......: ./www/o: HTTP TCP port........: 8080o: HTTP Listen Address..: (null)o: username:password....: disabledo: commands.............: enabled

这里告诉我了默认端口是 8080.

4.8 输入 http://树莓派ip:8080/stream_simple.html

在这里插入图片描述这里博主拍到桌面上的一本书籍。

5. 设置开机自启动

我们看看安装目录有什么文件?

pi@raspberrypi:~/mjpg-streamer/mjpg-streamer-experimental $ ls -al
total 336
drwxr-xr-x  7 root root  4096 Nov 12 09:40 .
drwxr-xr-x  4 root root  4096 Nov 12 07:22 ..
drwxr-xr-x  4 root root  4096 Nov 12 09:40 _build
drwxr-xr-x  2 root root  4096 Nov 12 07:22 cmake
-rw-r--r--  1 root root  2701 Nov 12 07:22 CMakeLists.txt
-rw-r--r--  1 root root   596 Nov 12 07:22 Dockerfile
-rw-r--r--  1 root root   116 Nov 12 07:22 docker-start.sh
-rwxr-xr-x  1 root root 17976 Nov 12 09:40 input_file.so
-rwxr-xr-x  1 root root 18656 Nov 12 09:40 input_http.so
-rwxr-xr-x  1 root root 50920 Nov 12 09:40 input_uvc.so
-rw-r--r--  1 root root 17987 Nov 12 07:22 LICENSE
-rwxr-xr-x  1 root root   510 Nov 12 07:22 makedeb.sh
-rw-r--r--  1 root root   772 Nov 12 07:22 Makefile
-rwxr-xr-x  1 root root 18344 Nov 12 09:40 mjpg_streamer
-rw-r--r--  1 root root 15782 Nov 12 07:22 mjpg_streamer.c
-rw-r--r--  1 root root  3695 Nov 12 07:22 mjpg_streamer.h
-rw-r--r--  1 root root   285 Nov 12 07:22 mjpg_streamer@.service
-rwxr-xr-x  1 root root 18800 Nov 12 09:40 output_file.so
-rwxr-xr-x  1 root root 40036 Nov 12 09:40 output_http.so
-rwxr-xr-x  1 root root 13324 Nov 12 09:40 output_rtsp.so
-rwxr-xr-x  1 root root 17808 Nov 12 09:40 output_udp.so
drwxr-xr-x 17 root root  4096 Nov 12 07:22 plugins
-rw-r--r--  1 root root    57 Nov 12 07:22 postinstall.sh
-rw-r--r--  1 root root  1350 Nov 12 07:22 README.md
drwxr-xr-x  2 root root  4096 Nov 12 07:22 scripts
-rwxr-xr-x  1 root root  5117 Nov 12 07:22 start.sh
-rw-r--r--  1 root root   330 Nov 12 07:22 TODO
-rw-r--r--  1 root root  4659 Nov 12 07:22 utils.c
-rw-r--r--  1 root root  5137 Nov 12 07:22 utils.h
drwxr-xr-x  2 root root  4096 Nov 12 07:22 www
pi@raspberrypi:~/mjpg-streamer/mjpg-streamer-experimental $ 

里面有一个很重要的mjpg_streamer@.service。如果看过前面章节的同学潜意识估计就知道它是做什么的了。
查看一下该文件:

[Unit]
Description=A server for streaming Motion-JPEG from a video capture device
After=network.target[Service]
User=mjpg_streamer
ExecStart=/usr/bin/mjpg_streamer -i 'input_uvc.so -d /dev/%I' -o 'output_http.so -w /usr/share/mjpg_str>[Install]
WantedBy=multi-user.target

没错,就是关于服务启动相关的。我们需要对它进行修改。我们再看看我们上面的启动命令:

> ./mjpg_streamer -i "./input_uvc.so -r 1920 x 1080 -f 30 " -o "./output_http.so -w ./www"

我们得想办法(写脚本)把它写在service里面。

5.1 创建脚本

sudo touch mjpg_start.sh
sudo nano mjpg_start.sh

输入脚本内容:

#!/bin/sh
cd /home/pi/mjpg-streamer/mjpg-streamer-experimental
./mjpg_streamer -i "./input_uvc.so -r 1920x1080 -f 30 " -o "./output_http.so -w ./www"

在这里插入图片描述
保存成功,记得修改好文件可执行权限。

sudo chmod 777 mjpg_start.sh

5.2 测试脚本

pi@raspberrypi:~/mjpg-streamer/mjpg-streamer-experimental $ sh mjpg_start.sh 
MJPG Streamer Version: git rev: 310b29f4a94c46652b20c4b7b6e5cf24e532af39i: Using V4L2 device.: /dev/video0i: Desired Resolution: 1920 x 1080i: Frames Per Second.: 30i: Format............: JPEGi: TV-Norm...........: DEFAULTi: The specified resolution is unavailable, using: width 640 height 480 instead 
UVCIOC_CTRL_ADD - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Mode: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Frequency: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Disable video processing: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Raw bits per pixel: Inappropriate ioctl for device (25)o: www-folder-path......: ./www/o: HTTP TCP port........: 8080o: HTTP Listen Address..: (null)o: username:password....: disabledo: commands.............: enabled

可以成功运行,说明脚本可用。

5.3 修改service文件

把service文件修改为:

[Unit]
Description=A server for streaming Motion-JPEG from a video capture device
After=network.target[Service]
User=pi
ExecStart=/home/pi/mjpg-streamer/mjpg-streamer-experimental/mjpg_start.sh
Restart=always[Install]
WantedBy=multi-user.target

5.4 复制文件到/etc/systemd/system目录

执行命令:

sudo cp /home/pi/mjpg-streamer/mjpg-streamer-experimental/mjpg_streamer@.service /etc/systemd/system

在这里插入图片描述
同时,记得重命名一下。

cd /etc/systemd/system
sudo mv mjpg_streamer@.service mjpg_streamer.service

5.5 设置服务为开机自启动

sudo systemctl enable mjpg_streamer.service

pi@raspberrypi:/etc/systemd/system $ sudo systemctl enable mjpg_streamer.service
Created symlink /etc/systemd/system/multi-user.target.wants/mjpg_streamer.service → /etc/systemd/system/mjpg_streamer.service.
pi@raspberrypi:/etc/systemd/system $ ^C

重启树莓派看看效果。
在这里插入图片描述正常启动,完美!

6. MJPG-streamer技术细节

直接翻看源码
在这里插入图片描述

一些参考文档:

  • Mjpeg-streamer源码分析(一)
  • 五分钟拆解流媒体入门项目 MJPG-Streamer

在这里插入图片描述
在这里插入图片描述
工作原理:

除了 main 线程,还会起 2 个线程。输入插件的线程 A 负责从摄像头采集一帧数据,暂存在 buffer 中。输出插件的线程 B 负责实现 http server,当有 http client 要求访问时图像时,从 buffer 中读一帧数据,然后发送给 http client。

7. 远程监控访问

在这里插入图片描述

7.1 配置frp客户端

往frpc.ini文件添加http穿透配置:

[web]
type = http
local_port = 8080
custom_domains = 43.xxx.xxx.xxx 
http_user = dpjcn1990
http_pwd = xxxxxxxx
  • [web]
    代理名称,随意命名
  • type = http
    协议类型,此处为http,还可以配置tcp,udp等
  • custom_domains = 43.xxx.xxx.xxx
    公网的IP也可以是域名
  • local_port = 8080
    代理的本地端口,就是你访问本地项目时的端口
  • http_user/http_pwd
    所有客户端共用一个 frps 的 HTTP 服务端口,任何知道你的域名和 URL 的人都能访问到你部署在内网的服务,但是在某些场景下需要确保只有限定的用户才能访问。frp 支持通过 HTTP Basic Auth 来保护你的 web 服务,使用户需要通过用户名和密码才能访问到你的服务。

使用custom_domains 指定的地址或者域名,端口为服务端的 vhost_http_port 指定的端口,此处为8080, 访问后就相当于访问到了本地的端口 8080
此处为: xxx.xxx.xxx.xxx:8080
代理方式相当于: xxx.xxx.xxx.xxx:8080 --> 127.0.0.1:8080

7.2 配置frp服务器

服务器相对简单。修改/etc/frp/frps.ini:

[common]
bind_port = 7000
vhost_http_port = 7001

记得配置云服务安全端口。

然后重启一下。

7.3 访问一下 http://公网ip:vhost_http_port 端口号/ 查看效果

在这里插入图片描述
会提示你输入账号和密码。
效果是可以访问,但是有点大延迟(也可能我的带宽不行)。我们可以试试压缩一下图片大小。

8. 总结

主要以学习态度把家里刚好有一个闲置好几年的usb摄像头,想着如何把它也接入到树莓派中来。但目前效果看起来局域网挺好,广域网就不行,这个方案仍有待考虑。


http://www.ppmy.cn/news/539069.html

相关文章

音视频开发系列(7):完成本地摄像头直播推流

今天把读取本地摄像头将视频流推流到nginx服务器的直播代码学习完了,这里对代码的流程做一下记录,以便以后进行复习。 这边用到了opencv和ffmpeg的开源库(PS:在前面有进行分享),配置环境在之前也有进行分享。 第一步&#xff1a…

树莓派+官方摄像头模块+VLC串流实时输出网络视频流

sudo apt-get update sudo apt-get install vlc sudo raspivid -o - -t 0 -w 640 -h 360 -fps 25|cvlc -vvv stream:///dev/stdin --sout #standard{accesshttp,muxts,dst:8090} :demuxh264 在电脑端,无论是Windows,Linux还是OSX,或者安卓机器…

10 种分布式系统必备模式

在当今的技术领域中,分布式系统已成为许多大型应用程序和平台的核心。构建高性能、可伸缩和可靠的分布式系统是一个复杂的挑战,需要合理的架构设计和模式选择。本文将介绍10个必备的分布式系统模式,帮助您更好地理解和应用这些模式以提升系统…

公众号自动回复消息添加跳转小程序链接

业务说明: 是要实现在小程序授权公众号收发消息功能 解决方案: 在用户关注后自动回复消息(因为要实现授权前提是用户必须先关注了公众),在消息中添加可跳转小程序的链接,就可以将小程序的参数传到授权页面进…

1500个常用计算机单词

1.file,n.文件;v.保存文件 [faɪl] 2.command,n.命令,指令 [kәˈmnd] 3.use,v.使用,用途 [jus] 4.program,n.程序 [ˈproʊgrm] 5.line,n.(数据,程序)行,线路 [laɪn] 6.if,conj.如果 [ɪf] 7.display,vt.显示&#x…

前端vue

1、父传子(属性传值):在父组件的子组件标签上,绑定一个自定义属性,然后子组件通过prop来接收,props可以写成数组的形式,然后直接把事件名直接加上引号放进数组["事件名"],也可以写成一…

[前端基础] CSS3 篇

CSS3 使用了层叠样式表技术,可以对网页布局、字体、颜色、背景灯效果做出控制。CSS3 作为 CSS 的进阶版,拆分和增加了盒子模型、列表模块、语言模块 、背景边框 、文字特效 、多栏布局等等。CSS3 的改变有很多,增加了文字特效,丰富…

【Datawhale动手学数据分析笔记】第二章 数据清洗

数据清洗 第二章:数据清洗及特征处理 数据清洗简述 我们拿到的数据通常是不干净的,所谓的不干净,就是数据中有缺失值,有一些异常点等,需要经过一定的处理才能继续做后面的分析或建模,所以拿到数据的第一…