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

这篇具有很好参考价值的文章主要介绍了【树莓派不吃灰】基础篇⑲ 搭建usb摄像头MJPG-streamer图片流监控,支持远程视频监控访问。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

  • ❤️ 博客主页 单片机菜鸟哥,一个野生非专业硬件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

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

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:/ $ 

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

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 $ 

等待一会等待编译成功。

这里有几个地方需要注意一下。
【树莓派不吃灰】基础篇⑲ 搭建usb摄像头MJPG-streamer图片流监控,支持远程视频监控访问

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

【树莓派不吃灰】基础篇⑲ 搭建usb摄像头MJPG-streamer图片流监控,支持远程视频监控访问这里博主拍到桌面上的一本书籍。

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"

【树莓派不吃灰】基础篇⑲ 搭建usb摄像头MJPG-streamer图片流监控,支持远程视频监控访问
保存成功,记得修改好文件可执行权限。

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

【树莓派不吃灰】基础篇⑲ 搭建usb摄像头MJPG-streamer图片流监控,支持远程视频监控访问
同时,记得重命名一下。

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

重启树莓派看看效果。
【树莓派不吃灰】基础篇⑲ 搭建usb摄像头MJPG-streamer图片流监控,支持远程视频监控访问正常启动,完美!

6. MJPG-streamer技术细节

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

一些参考文档:

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

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

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

7. 远程监控访问

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

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 端口号/ 查看效果

【树莓派不吃灰】基础篇⑲ 搭建usb摄像头MJPG-streamer图片流监控,支持远程视频监控访问
会提示你输入账号和密码。
效果是可以访问,但是有点大延迟(也可能我的带宽不行)。我们可以试试压缩一下图片大小。

8. 总结

主要以学习态度把家里刚好有一个闲置好几年的usb摄像头,想着如何把它也接入到树莓派中来。但目前效果看起来局域网挺好,广域网就不行,这个方案仍有待考虑。文章来源地址https://www.toymoban.com/news/detail-413677.html

到了这里,关于【树莓派不吃灰】基础篇⑲ 搭建usb摄像头MJPG-streamer图片流监控,支持远程视频监控访问的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 树莓派4B摄像头安装+Ubuntu22.04系统摄像头识别

    树莓派4B安装上摄像头后,是不能立即使用的,需要进行一些配置才能使用,本文详细说明了配置过程和测试结果。 执行以下命令,检测是否可以识别摄像头: 结果如下: 返回结果:supported=0 detected=0,说明此时摄像头硬件还没有识别出来,而且设备是不支持摄像头配件的,

    2024年02月22日
    浏览(73)
  • 【树莓派笔记】树莓派摄像头的简单使用

    这里使用的是树莓派CSI (Camera Serial Interface)摄像头。引脚面朝micoHDMI接口处。关机后安装,热插拔可能无法检测到。 终端输入   之后重启即可 命令行用 raspistill 使用摄像头进行拍照。 可以用 -e png 指定生成jpg、png、gif、bmp格式的图片,后三种无损,生成速度会慢些。 raspiy

    2024年02月15日
    浏览(54)
  • Android相机调用-CameraX【外接摄像头】【USB摄像头】

    Android相机调用有原生的Camera和Camera2,我觉得调用代码都太复杂了,CameraX调用代码简洁很多。 说明文档:https://developer.android.com/jetpack/androidx/releases/camera?hl=zh-cn 现有查到的调用资料都不够新,对于外接摄像头(USB摄像头)这类非前置也非后置摄像头的设备调用,都说是没有实

    2024年02月09日
    浏览(51)
  • 树莓派——opencv打开树莓派摄像头调试记录

    目录 一、安装包:(注意换要源,不要用系统默认源) 问题1:磁盘空间不足——把根目录扩展到整个SD卡 解决:把根目录扩展到整个SD卡 二、下载 1、先创建opencv目录: 2、下载 问题:fatal: unable to access \\\'https://github.com/opencv/opencv.git/\\\': gnutls_handshake() failed: The TLS connection was

    2024年02月11日
    浏览(45)
  • 树莓派摄像头使用方法

       打开之后,重启树莓派:sudo reboot  输入以下指令,可以使用树莓派摄像头的拍照功能:  树莓派拍摄照片保存到指定文件夹,下面的例子将照片保存在了/home/pi/httpHeadler/ 也可以通过system函数调用拍照命令。

    2024年02月14日
    浏览(43)
  • 【树莓派】摄像头的基本使用

    本次实验目标是: 1、完成树莓派使用CSI摄像头拍取照片,拍摄视频 2、使用socket进行网络拍照控制和图片传输功能。 树莓派4B 树莓派摄像头 VNC-Viewer eclipse/VS Code(Java) 将树莓派断电,并将CSI摄像头接入靠近网孔的排孔中,CSI摄像头蓝色面朝网孔方向。 终端执行指令 使能摄

    2023年04月08日
    浏览(42)
  • 树莓派连接双目摄像头教程(详)

    前言:此教程为菜鸟准备好了代码,仅仅是初试水。 软件准备:Arduino IDE软件 硬件准备:树莓派 Arduin单片机 双目摄像头 树莓派桌面:1准备环境 2 Arduino IDE 3打开摄像头  1.软件准备 下载Arduino IDE软件 Windows用户可在浏览器输入网址:https://www.arduino.cc/en/Main/Software 下载安装都是

    2023年04月09日
    浏览(87)
  • ubuntu打开usb摄像头

    记录一下解决在 Linux 下打开 usb 摄像头界面黑屏的问题。 1、保持在 ubuntu 界面,插上 usb 摄像头,将 usb 摄像头连接到虚拟机上。 2、使用 dmesg 命令可以看到设备接入时的打印信息 可以看到我们刚接入的摄像头它的 VID:PID 是 1e4e:0110,这里的 ID 号可以在 UVC 官方的文档中进一步

    2024年02月16日
    浏览(46)
  • 树莓派官方摄像头detected=0问题

    树莓派的官方摄像头在连接到树莓派时常遇到各种情况,今天在连接opencv时出现了如下的问题:  如图片所示,我先检测树莓派的状态,发现输出为1正常,但输入为0.在正常的情况下应该是: supported=1 detected=1. 然后我尝试 ls /dev/video*指令,发现没有video0设备,这是不正常的,

    2024年02月07日
    浏览(39)
  • 树莓派进行摄像头实验保姆级教程

    前言 最近做一个项目的时候需要通过进行目标检测并且对目标进行比对然后进行后续实验 于是最近花了20块买了个摄像头,先通过进行摄像头的录像照相等功能进行实现 后续可能会出一篇树莓派在ROS系统下利用该摄像头进行物体识别等等功能的实现 接下来就开始正文部分啦

    2023年04月08日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包