简体   繁体   English

Raspberry Pi:使用VLC流媒体网络摄像头:罗技C920 [没有转码的H264视频+音频+ LED控制] - SpyCam / BabyCam

[英]Raspberry Pi : use VLC to stream webcam : Logitech C920 [H264 Video without transcoding + Audio + LED control] - SpyCam / BabyCam

I have a RaspberryPi and a Logitech C920 Webcam. 我有一个RaspberryPi和一个Logitech C920网络摄像头。 I want to use these devices to work as a surveillance / babycam, ie : Stream audio + video over HTTP (or any other protocol) without cpu intensive video transcoding 我想使用这些设备作为监视/ babycam,即: 通过HTTP(或任何其他协议)流式传输音频+视频,无需CPU密集视频转码

The C920 webcam is able to stream H264 natively, so theoretically I won't need to ask RaspberyPi+VLC to transcode the video stream. C920网络摄像头能够本地传输H264,因此理论上我不需要让RaspberyPi + VLC对视频流进行转码。

The built-in C920 Microphone stream does not seem to be included in the webcam stream. 内置的C920麦克风流似乎不包含在网络摄像头流中。 Cam and microphone are 2 separate devices. 凸轮和麦克风是2个独立的设备。

The C920 also has a built-in led indicator. C920还有一个内置LED指示灯。 I want to control that to avoid the LED to ligth up while recording. 我想控制它以避免LED在录制时发光。

How can I achieve that ? 我怎样才能做到这一点?

This solution is tested and working with versions indicated below. 该解决方案经过测试并使用下面显示的版本。 Using this method, the RaspberryPi3 is always around 5% CPU. 使用这种方法,RaspberryPi3总是大约5%的CPU。

edit 2018-11-18: One can also see the all-in-one solution prototype on RaspiVWS project homepage (for curious people, see GitHub project ) 编辑2018-11-18:人们还可以在RaspiVWS项目主页看到一体化解决方案原型(对于好奇的人,请参阅GitHub项目

0. Preliminary checks 0.初步检查

1. Webcam video configuration 1.网络摄像头视频配置

2. Microphone identification 2.麦克风识别

3. Stream using VLC 3.使用VLC流

4. Make RaspberryPi3+ a Wifi access point (If you have no existing network to connect your Pi to) 4.使RaspberryPi3 +成为Wifi接入点 (如果您没有现有的网络连接您的Pi)

5. Script at startup or as a service 5.启动时的脚本或服务

6. [EDIT] Additional commands : infinite loop recording & split video 6. [编辑]附加命令:无限循环录制和分割视频

7. [EDIT] Program execution at a given instant 7. [EDIT]在给定时刻执行程序

8. [EDIT] TROUBLESHOOTING 8. [编辑]故障排除


0. Preliminary checks 0.初步检查

The answer is working with Raspbian 9.4 Stretch. 答案是使用Raspbian 9.4 Stretch。 Check your version with the following command : 使用以下命令检查您的版本:

lsb_release -a

You should see: 你应该看到:

No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 9.4 (stretch)
Release:        9.4
Codename:       stretch

We can rely on the following tools : 我们可以依靠以下工具:

  • v4l allows to control the webcam. v4l允许控制网络摄像头。 It offers the command v4l2-ctl which will allows us to control and config the webcam. 它提供了命令v4l2-ctl ,它允许我们控制和配置网络摄像头。
  • VLC which is not only a video player, but also has powerful streaming capabilities VLC不仅是视频播放器,还具有强大的流媒体功能

You can install them with the following commands : 您可以使用以下命令安装它们:

sudo apt-get install vlc
sudo apt-get install v4l-utils

Once everything is installed, you can configure your C920 webcam. 安装完所有内容后,即可配置C920网络摄像头。


1. Webcam video configuration 1.网络摄像头视频配置

v4l2-ctl --all lists all available devices and their config v4l2-ctl --all列出所有可用设备及其配置

pi@raspberrypi:~ $ v4l2-ctl --all
Driver Info (not using libv4l2):
    Driver name   : uvcvideo
    Card type     : HD Pro Webcam C920
    Bus info      : usb-3f980000.usb-1.5
    Driver version: 4.14.30
    Capabilities  : 0x84200001
        Video Capture
        Streaming
        Extended Pix Format
        Device Capabilities
    Device Caps   : 0x04200001
        Video Capture
        Streaming
        Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
    Width/Height      : 1920/1080
    Pixel Format      : 'H264'
    Field             : None
    Bytes per Line    : 3840
    Size Image        : 4147200
    Colorspace        : sRGB
    Transfer Function : Default
    YCbCr/HSV Encoding: Default
    Quantization      : Default
    Flags             : 
Crop Capability Video Capture:
    Bounds      : Left 0, Top 0, Width 1920, Height 1080
    Default     : Left 0, Top 0, Width 1920, Height 1080
    Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 1920, Height 1080
Selection: crop_bounds, Left 0, Top 0, Width 1920, Height 1080
Streaming Parameters Video Capture:
    Capabilities     : timeperframe
    Frames per second: 30.000 (30/1)
    Read buffers     : 0
                     brightness (int)    : min=0 max=255 step=1 default=-8193 value=128
                       contrast (int)    : min=0 max=255 step=1 default=57343 value=128
                     saturation (int)    : min=0 max=255 step=1 default=57343 value=128
 white_balance_temperature_auto (bool)   : default=1 value=1
                           gain (int)    : min=0 max=255 step=1 default=57343 value=255
           power_line_frequency (menu)   : min=0 max=2 default=2 value=2
      white_balance_temperature (int)    : min=2000 max=6500 step=1 default=57343 value=4822 flags=inactive
                      sharpness (int)    : min=0 max=255 step=1 default=57343 value=128
         backlight_compensation (int)    : min=0 max=1 step=1 default=57343 value=0
                  exposure_auto (menu)   : min=0 max=3 default=0 value=3
              exposure_absolute (int)    : min=3 max=2047 step=1 default=250 value=333 flags=inactive
         exposure_auto_priority (bool)   : default=0 value=1
                   pan_absolute (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                  tilt_absolute (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                 focus_absolute (int)    : min=0 max=250 step=5 default=8189 value=0 flags=inactive
                     focus_auto (bool)   : default=1 value=1
                  zoom_absolute (int)    : min=100 max=500 step=1 default=57343 value=100
                      led1_mode (menu)   : min=0 max=3 default=3 value=3
                 led1_frequency (int)    : min=0 max=255 step=1 default=0 value=0

The last 2 lines gives us clues to control the built-in LED indicator, for instance, to deactivate the LED indicator. 最后两行为我们提供了控制内置LED指示灯的线​​索,例如,停用LED指示灯。 The -d0 parameter indicates on which device the modifcation should be applied (if you ahve several cams or its device name changed) -d0参数指示应在哪个设备上应用修改(如果您修改了多个凸轮或其设备名称)

v4l2-ctl -d0 --set-ctrl=led1_mode=0
v4l2-ctl -d0 --set-ctrl=led1_frequency=30

2. Microphone identification 2.麦克风识别

The command arecord -l will give us the list of ALSA devices. 命令arecord -l将为我们提供ALSA设备列表。 (ALSA is the audio manager in RaspberryPi) (ALSA是RaspberryPi中的音频管理器)

pi@raspberrypi:~ $ arecord -l
**** Liste des Périphériques Matériels CAPTURE ****
carte 1: C920 [HD Pro Webcam C920], périphérique 0: USB Audio [USB Audio]
  Sous-périphériques: 1/1
  Sous-périphérique #0: subdevice #0

This means that the built-in microphone is located on hardware 1, periph 0 . 这意味着内置麦克风位于hardware 1, periph 0 You can check that in command line with alsamixer -c 1 -V capture 您可以在命令行中使用alsamixer -c 1 -V capture

3. Stream using VLC 3.使用VLC流

VLC can be launched using command line. 可以使用命令行启动VLC。 Since we do not have video and audio already mixed together in a single stream access, we need to ask VLC to do that. 由于我们没有在单个流访问中混合在一起的视频和音频,我们需要让VLC这样做。 It is the role of the transcoding feature of VLC. 它是VLC transcoding功能的作用。

  1. Stream over HTTP 通过HTTP流

We also want to stream over HTTP, VLC can also achieve that. 我们也希望通过HTTP流,VLC也可以实现。

cvlc v4l2:///dev/video0:chroma=h264 :input-slave=alsa://hw:1,0 --sout '#transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1}:standard{access=http,mux=ts,mime=video/ts,dst=:8099}'

Explanation 说明

  • v4l2:///dev/video0:chroma=h264 gives VLC input data : it grabs the video stream from /dev/video0 and that it is a h264 encoding (if your webcam is the 0th video device, it could also be another number, refer to v4l2-ctl --all command) v4l2:///dev/video0:chroma=h264给出VLC输入数据:它从/ dev / video0抓取视频流并且它是h264编码(如果你的网络摄像头是第0个视频设备,它也可能是另一个数字,请参阅v4l2-ctl --all命令)
  • :input-slave=alsa://hw:1,0 tells VLC to take another input stream with the video. :input-slave=alsa://hw:1,0告诉VLC使用视频获取另一个输入流。 It is the audio stream identified from the arecord above 它是从上面的arecord标识的音频流
  • --sout tells VLC how to handle the output stream --sout告诉VLC如何处理输出流
  • #transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1} tells VLC to convert the audio to mpga codec, 128 kbits/s, 2 channels, 44100 Hz sampling, using all 4 RaspberryPi3+ cores. #transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1}告诉VLC将音频转换为mpga编解码器,128 kbits / s,2个通道,44100 Hz采样,使用所有4个RaspberryPi3 +核心。 audiosync is optional. audiosync是可选的。 It took me some time to realize this : the webcam h264 video stream is kept as provided (no video transcoding). 我花了一些时间才意识到这一点:网络摄像头h264视频流保持不变(没有视频转码)。
  • :standard{access=http,mux=ts,mime=video/ts,dst=:8099} tells VLC to provide stream over HTTP on port 8099 with the TS muxing format. :standard{access=http,mux=ts,mime=video/ts,dst=:8099}告诉VLC使用TS muxing格式在端口8099上提供HTTP流。

On any other device, you can use VLC to access your RaspberryPi3+ VLC stream : vlc http://<raspberrypi-ip>:8099 在任何其他设备上,您可以使用VLC访问RaspberryPi3 + VLC流: vlc http://<raspberrypi-ip>:8099

It works with any VLC client : 它适用于任何VLC客户端:

  • windows 视窗
  • unix UNIX
  • mac 苹果电脑
  • confirmed with iPhone 7 (v11.2.1 (15C153)) with VLC app (3.0.3 (305)) 使用VLC app(3.0.3(305))通过iPhone 7(v11.2.1(15C153))确认

NB : Having the video already in H264 1920x1080 30fps in output of the webcam saves a lot of RaspberryPi3+ CPU. 注意:将视频已经在H264 1920x1080 30fps的网络摄像头输出中节省了大量的RaspberryPi3 + CPU。

  1. Different containers 不同的容器

You can also record to various containers, or even containers + stream, here are some examples: 您还可以记录到各种容器,甚至容器+流,这里有一些例子:

  • record to MKV cvlc v4l2:///dev/video0:chroma=h264 :input-slave=alsa://hw:1,0 --sout '#transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1}:standard{access=file,mux=mkv,dst='/home/pi/Webcam_Record/MyVid.mkv'}' 记录到MKV cvlc v4l2:///dev/video0:chroma=h264 :input-slave=alsa://hw:1,0 --sout '#transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1}:standard{access=file,mux=mkv,dst='/home/pi/Webcam_Record/MyVid.mkv'}'

  • record to MP4 cvlc v4l2:///dev/video0:chroma=h264 :input-slave=alsa://hw:1,0 --sout '#transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1}:standard{access=file,mux=mp4,dst='/home/pi/Webcam_Record/MyVid.mp4'}' 记录到MP4 cvlc v4l2:///dev/video0:chroma=h264 :input-slave=alsa://hw:1,0 --sout '#transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1}:standard{access=file,mux=mp4,dst='/home/pi/Webcam_Record/MyVid.mp4'}'

  • record + stream cvlc v4l2:///dev/video0:chroma=h264 :input-slave=alsa://hw:1,0 --sout '#transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1}:duplicate{dst=standard{access=file,mux=mp4,dst='/home/pi/Webcam_Record/MyVid.mp4'},dst=standard{access=http,mux=ts,mime=video/ts,dst=:8099}}' record + stream cvlc v4l2:///dev/video0:chroma=h264 :input-slave=alsa://hw:1,0 --sout '#transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1}:duplicate{dst=standard{access=file,mux=mp4,dst='/home/pi/Webcam_Record/MyVid.mp4'},dst=standard{access=http,mux=ts,mime=video/ts,dst=:8099}}'

    1. Format filenames, timestamps 格式化文件名,时间戳

You can also use formatted string for filenames. 您还可以使用格式化的字符串作为文件名。 Prefix command like this: 像这样的前缀命令:

cvlc --sout-file-format v4l2:///dev/video0:<...> dst='/home/pi/Webcam_Record/%F_%T_MyVid.mp4'}

It will produce a file named YYYY-MM-DD_HH:MM:SS_MyVid.mp4 ( : are authorized in unix filenames, but not in windows filenames) 它将生成一个名为YYYY-MM-DD_HH:MM:SS_MyVid.mp4的文件YYYY-MM-DD_HH:MM:SS_MyVid.mp4:在unix文件名中授权,但在Windows文件名中不授权)

4. Make RaspberryPi3+ a Wifi access point 4.使RaspberryPi3 +成为Wifi接入点

If you have no existing network to connect your Pi to: You can follow instructions from official RaspberryPi3+ website : https://www.raspberrypi.org/documentation/configuration/wireless/access-point.md 如果您没有现有的网络连接您的Pi:您可以按照官方RaspberryPi3 +网站上的说明操作: https//www.raspberrypi.org/documentation/configuration/wireless/access-point.md

Otherwise, if you already have a network you can connect to your pi using its IP. 否则,如果您已经拥有网络,则可以使用其IP连接到您的pi。 See part 3 见第3部分

On any other device, you can use VLC to access your RaspberryPi3+ VLC stream : vlc http://<raspberrypi-ip>:8099 在任何其他设备上,您可以使用VLC访问RaspberryPi3 + VLC流: vlc http://<raspberrypi-ip>:8099

5. Script at startup 5.启动时的脚本

You can put many commands into a bash file my_bash_file.sh . 您可以将许多命令放入bash文件my_bash_file.sh For instance : 例如 :

#!/bin/bash
# auto stream launch + led off
#cvlc -vvv for verbose debug

# change this value to adapt to your webcam device number
deviceNb=0

# force video format + led off
v4l2-ctl -d${deviceNb} --set-fmt-video=width=1920,height=1080,pixelformat=1 --set-ctrl=led1_mode=0

# if delay needed 
# cvlc v4l2:///dev/video${deviceNb}:chroma=h264 :input-slave=alsa://hw:1,0  :live-caching=2500  --sout '#transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1}:standard{access=http,mux=ts,mime=video/ts,dst=:8099}'

# no delay
cvlc v4l2:///dev/video${deviceNb}:chroma=h264 :input-slave=alsa://hw:1,0 --sout '#transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1}:standard{access=http,mux=ts,mime=video/ts,dst=:8099}'
  1. Basic method 基本方法

You can then make the rc.local script use your custom script to be executed at startup. 然后,您可以使rc.local脚本使用您的自定义脚本在启动时执行。 You can follow instructions from official RaspberryPi3+ website : https://www.raspberrypi.org/documentation/linux/usage/rc-local.md 您可以按照官方RaspberryPi3 +网站的说明进行操作: https//www.raspberrypi.org/documentation/linux/usage/rc-local.md

  1. Another method : Create a deamon service 另一种方法:创建一个deamon服务

We will create a "webcam-stream" service, assuming all necessary bash commands are located /home/pi/Webcam_Record/vlc_webcam_stream_service.sh 我们将创建一个“网络摄像头流”服务,假设所有必要的bash命令都位于/home/pi/Webcam_Record/vlc_webcam_stream_service.sh

cd /lib/systemd/system/
sudo nano webcam-stream.service

And write in it: 写下来:

[Unit]
Description=Custom Webcam Streaming Service
After=multi-user.target

[Service]
Type=simple
ExecStart=/home/pi/Webcam_Record/vlc_webcam_stream_service.sh
Restart=on-abort

[Install]
WantedBy=multi-user.target

Make the service file and the script executable: 使服务文件和脚本可执行:

sudo chmod 644 /lib/systemd/system/webcam-stream.service
chmod +x /home/pi/Webcam_Record/vlc_webcam_stream.sh

Allow VLC to be excuted as root: 允许VLC以root身份执行:

sudo sed -i 's/geteuid/getppid/' /usr/bin/vlc

Reload deamons and enable our service: 重新加载deamons并启用我们的服务:

sudo systemctl daemon-reload
sudo systemctl enable webcam-stream.service

Check it is recognized and working: 检查它是否被识别并正常工作:

sudo service webcam-stream status
sudo service webcam-stream start

You can check with another computer that the video is correctly streamed. 您可以与另一台计算机核实视频是否正确流式传输。 Note that the webcam won't be available while the service is running. 请注意,在服务运行时,网络摄像头将不可用。


Once you're done, you can connect to the RaspberryPi3+ wifi access point and access your video stream. 完成后,您可以连接到RaspberryPi3 + wifi接入点并访问您的视频流。


6. [EDIT] Additional commands : infinite loop recording & split video 6. [编辑]附加命令:无限循环录制和分割视频

The following bash scripts allows infinite recording of 15 s long videos with timestamped filenames and streaming 以下bash脚本允许使用带时间戳的文件名和流式传输无限录制15秒长的视频

#!/bin/bash
# auto stream launch + led off
#cvlc -vvv for verbose debug

# adapt to video device name
deviceNb=1

# loop duration
duration=15

#infinite recording
#loopOption=
loopOption=--loop

# force video format + led off
v4l2-ctl -d ${deviceNb} --set-fmt-video=width=1920,height=1080,pixelformat=1 --set-ctrl=led1_mode=0

# if delay needed :live-caching=2500
cvlc --sout-file-format --run-time=${duration} ${loopOption} v4l2:///dev/video${deviceNb}:chroma=h264 :input-slave=alsa://hw:1,0 --sout '#transcode{acodec=mpga,ab=128,channels=2,samplerate=44100,threads=4,audio-sync=1}:duplicate{dst=standard{access=file,mux=mp4,dst='/home/pi/Webcam_Record/%F_%T_Spy.mp4'}:dst=standard{access=http,mux=ts,mime=video/ts,dst=:8099}'

7. [EDIT] Program execution at a given instant 7. [EDIT]在给定时刻执行程序

EDIT 04 aug 2018 编辑04 Aug 2018

To launch the execution today at 14:00, you can use the following command: 要在今天14:00启动执行,您可以使用以下命令:

./my_vlc_webcam_script.sh | at 1400

See the at command manual for further details. 有关更多详细信息,请参阅at命令手册。


8. TROUBLESHOOTING 8.故障排除

EDIT 07 jul 2018 编辑07年7月1日

I recently ran into VLC error after a dist-upgrade: 我最近在dist-upgrade之后遇到了VLC错误:

VLC media player 2.2.6 Umbrella (revision 2.2.6-0-g1aae78981c)
[00acb230] pulse audio output error: PulseAudio server connection failure: Connection refused

The solution I found is to launch VLC in GUI mode and change the default audio device to ALSA (instead of Automatic). 我找到的解决方案是在GUI模式下启动VLC并将默认音频设备更改为ALSA(而不是自动)。 I can also be done in command line. 我也可以在命令行中完成。 See the solution found here VLC issues with PulseAudio 请参阅PulseAudio中的VLC问题解决方案

cvlc -A alsa,none --alsa-audio-device default

You need the vcodec= for video to work and deinterlace if you want that. 如果需要,您需要vcodec= for video才能工作和deinterlace

cvlc v4l2:///dev/video0:chroma=h264
                       :input-slave=alsa://hw:1,0
                       :live-caching=2500
--sout '#transcode{
                     deinterlace,
                     vcodec=mpgv,
                     acodec=mpga,
                     ab=128,
                     channels=2,
                     samplerate=44100,
                     threads=4,
                     audio-sync=1}
       :standard{
                     access=http,
                     mux=ts,
                     mime=video/ts,
                     dst=0.0.0.0:8099}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM