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

news2024/12/22 16:53:41

在这里插入图片描述

目录

    • 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 following
                         example: 640x480
[-f | --fps ]..........: frames per second
                         (activates YUYV format, disables MJPEG)
[-m | --minimum_size ].: drop frames smaller then this limit, useful
                         if the webcam produces small-sized garbage frames
                         may 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 leave
                         it 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: 310b29f4a94c46652b20c4b7b6e5cf24e532af39
 i: Using V4L2 device.: /dev/video0
 i: Desired Resolution: 1920 x 1080
 i: Frames Per Second.: 30
 i: Format............: JPEG
 i: TV-Norm...........: DEFAULT
 i: 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........: 8080
 o: HTTP Listen Address..: (null)
 o: username:password....: disabled
 o: 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: 310b29f4a94c46652b20c4b7b6e5cf24e532af39
 i: Using V4L2 device.: /dev/video0
 i: Desired Resolution: 1920 x 1080
 i: Frames Per Second.: 30
 i: Format............: JPEG
 i: TV-Norm...........: DEFAULT
 i: 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........: 8080
 o: HTTP Listen Address..: (null)
 o: username:password....: disabled
 o: 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.coloradmin.cn/o/5249.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于51单片机的温度甲烷监测报警串口上报proteus仿真原理图PCB

功能介绍: 0.本系统采用STC89C52作为单片机 1.LCD1602液晶实时显示当前温度和甲烷浓度 2.超过甲烷浓度阈值,蜂鸣器报警 3.按键可更改甲烷浓度阈值上限和启动/暂停检测 4.020%浓度,绿色LED点亮 20%~阈值上限,黄色LED点亮&#xff0…

C#操作modbus

modbus使用范围广泛,广泛应用于各类仪表,PLC等。 modbus的好处是免费,属于应用层协议,底层硬件基于485/以太网。 modbus协议本质还是自定义协议。 modbus调试软件: mthings: modbuspoll: 主站/从站,客…

前端静态页面基本开发思路(一)

有不少刚入门前端的同学经常问我前端布局的问题,总是跟我说在面对学校布置的作业或者想自己搭建博客的时候不知道怎么下手,不知道怎么去写静态的页面,每当我解决了一个又一个同学的问题的时候,又有新的同学来问,故思来…

nginx 配置防盗链(了解)

一 防盗链 1.1 防盗链概念 网站上页面的一些静态资源,不想让本站点的静态资源被他人盗取访问。使用nginx判断请求连接的头部refer中是否含有内容以及合法性来进行处理。 referer表示第二次资源的来源地址 1.2 配置规则 valiad_referers none|blocked|server_na…

Spring--IOC基于XML管理bean

IOC容器 IOC思想 IOC:Inversion of Control 即反转控制 获取资源的传统方式 自己做饭:买菜、洗菜、择菜、改刀、炒菜,全过程参与,费时费力,必须清楚了解资源创建整个过程中的全部细节且熟练掌握。 在应用程序中的组件…

Antd表格性能优化

今天来分享一个实际项目的性能优化的内容。 文章目录一、背景介绍二、性能问题原因及解决方案一、背景介绍 国内React项目大多数人选择配套的UI库的时候都会选择Antd。如果是非常简单的页面用Antd的话其实是完全没问题的,性能感觉不到什么瓶颈,样式也还…

计算机网络

目录 介绍 组成部分 工作方式 功能组成 网络范围分类 传输技术: 总结 标准化工作 RFC文档的作用: 速率,带宽 有容量和速率的区别 带宽 吞吐量 时延,时延带宽积 1.发送时延: 2.传播时延: ​编辑 3.…

Vant组件库 引入 阿里矢量图 添加自己喜欢的 ICON

📃目录跳转一.矢量图下载💨使用CDN方式🎉下载本地(推荐)二.Vant引入Icon🗺️🎃 使用方式🚀 运行效果:一.矢量图下载 💨使用CDN方式 当然你也可以使用官方的C…

【编程题】【Scratch三级】2021.12 分身术

分身术 1. 准备工作 (1)删除小猫角色、添加角色“Monkey”,Money位于舞台的中心; (2)添加背景Light; (3)新建变量“编号”。 2. 功能实现 (1)程序开始时,Monkey说:“我会分身术!变!!!”2秒; (2)每隔2秒克隆出一个位置随机、大小随机、颜色随机的Monke…

python easygui修改窗口位置

EasyGui是一个十分简单的Python图形界面库,支持窗口文本显示、图片显示、按钮、文本框、选项栏、文件选择等等必要的组件且操作十分简单。但也因如此,导致EasyGui甚至不能手动调整窗口位置、按钮位置、名字等,因为EasyGui是基于Tkinter编写的…

VMware三种网络模式详解

VMware三种网络模式 linux重启网络服务命令: service network restart 一、桥接模式 原理:VMware和宿主机,处于同一网段、两者地位平等。(无需虚拟网卡) 1.1、使用方法 虚拟网络编辑器 虚拟机设置-适配器 上面两…

<Linux> 编译器与调试器—gcc/g++/gdb 的使用

< Linux > 编译器与调试器—gcc/g/gdb 的使用 文章目录< Linux > 编译器与调试器—gcc/g/gdb 的使用一、Linux编译器 - gcc/g 使用1. 编译程序的四个过程背景知识预处理编译汇编链接2. 链接方式与函数库2.1 动态链接与静态链接2.2.函数库2.3.动态库与静态库3. gcc/…

干货分享:谷歌主动搜索开发客户的万能公式

大家在用谷歌(Google)开发是不是也有以下常见问题&#xff1a; 关键词不准&#xff0c;搜索到的都是零售商&#xff0c;B2C平台搜索到的客户太大&#xff0c;已经被开发多次&#xff0c;开发信不回复搜索到的客户找不到邮箱搜索到的客户与工厂不匹配&#xff0c;无法合作 其实…

车路协同 智能路侧设备网络安全接入技术要求

1 范围 本文件包含智能路侧设备网络安全接入技术要求&#xff0c;包括智能路侧设备连接要求、接入要求、证书管理要求。 本文件适用于智能路侧设备网络安全接入的设计与开发。 2 规范性引用文件 下列文件中的内容通过文中的规范性引用而构成本文件必不可少的条款。其中&…

Simulink永磁同步电机控制仿真:过调制及电流重构

在一些高功率密度的应用场景中&#xff0c;追求极致的电压利用率&#xff0c;这个时候要用到过调制技术&#xff1b;当svpwm工作在过调制区域时&#xff0c;逆变电桥会在一个基波周期内多次达到100%占空比&#xff0c;且较多时间处于较高的占空比&#xff0c;这个时候下桥臂电流…

数据结构系列学习(七) - 链栈(Chain_Stack)

目录 引言&#xff1a; 学习&#xff1a; 代码实现&#xff1a; 头文件&#xff08;Chain_Stack.h&#xff09;&#xff1a; 设置链栈中的元素范型&#xff1a; 链栈的结构体设计&#xff1a; 所有功能函数的声明&#xff1a; 源文件&#xff08;Chain_Stack.cpp&#…

性能测试场景设计之 阶梯性能场景(负载测试场景)

「负载测试&#xff1a;」 逐步增加并发用户数。看服务器的最大拐点区间在哪里。再缩小拐点区间&#xff0c;找出最大并发用户数。 使用方式&#xff1a; 安装 jpgc插件 添加线程组 每次递增10个并发 This group will start&#xff1a;给定当前负载的并发用户数First, wait …

机器学习-集成算法

文章目录集成算法1. 定义2. 具体模型2.1. Bagging2.2. Boosting2.3. Stacking3. 随机森林3.1. 树模型结构3.2. 随机森林的优点3.3. 分类与回归问题3.4. 树模型个数问题3.5. 参数问题(特征重要性)3.6. 可视化展示问题4. 集成基本思想4.1. 硬投票策略步骤4.2. 软投票策略步骤5. B…

【SpringMVC】基础、环境搭建、注解搭建、 and so on……

文章目录SpringMVC【原理】&#xff1a;一、why?&#xff1a;二、环境搭建&#xff08;配置方式&#xff09;&#xff1a;▶1.导入jar包▶2.创建简单测试----创建Controller类继承AbstractController▶3.如何读取Controller文件&#xff0c;创建SpringMVC.xml配置文件&#xf…

使用SPACEDESK时iPad显示Connected-Display OFF解决方法

使用SPACEDESK时iPad显示Connected-Display OFF1. SPACEDESK 概述2. 使用SPACEDESK时iPad显示Connected-Display OFF解决方法结束语1. SPACEDESK 概述 SPACEDESK 是一款网络显示多显示器软件&#xff0c;适用于电脑桌面复制&#xff08;镜像或克隆&#xff09;&#xff0c;能将…