Monday, 22 June 2020

USB Webcam(s) with Raspberry Pi

Configuration Steps


Update the Pi


sudo rpi-update

Ensure the right modules are loaded

lsmod | grep uvcvideo
uvcvideo               94208  1
videobuf2_vmalloc      16384  2 uvcvideo,bcm2835_v4l2
videobuf2_v4l2         28672  5 bcm2835_isp,uvcvideo,bcm2835_codec,bcm2835_v4l2,v4l2_mem2mem
videobuf2_common       57344  6 bcm2835_isp,uvcvideo,bcm2835_codec,bcm2835_v4l2,v4l2_mem2mem,videobuf2_v4l2
videodev              237568  8 bcm2835_isp,uvcvideo,bcm2835_codec,videobuf2_common,bcm2835_v4l2,v4l2_mem2mem,videobuf2_v4l2
mc                     45056  8 bcm2835_isp,uvcvideo,bcm2835_codec,snd_usb_audio,videobuf2_common,videodev,v4l2_mem2mem,videobuf2_v4l2
ls -la /dev/video?
crw-rw-rw- 1 root video 81, 0 Jun 22 14:17 /dev/video0
crw-rw-rw- 1 root video 81, 1 Jun 22 14:17 /dev/video1

Add any missing modules to  /etc/modules-load.d/video.conf

e.g.:  snd-bcm2835,  i2c-dev,  bcm2835-v4l2, uvcvideo

Disable PI (non USB) camera

raspi-config

Gotchas

In order for this to work, the /dev/video0 port must exist and be accessible.

Ensure the USB video is not blacklisted

sudo rm /etc/modprobe.d/ubcvideo-blacklist.conf

Ensure the port is accessible

chmod ugo+rw /dev/video?

(this is crude - the proper thing to do is ensure that the users accessing the camera are in the 'video' group).

Programs that use the camera

uv4l - conferencing tool
motion - security camera tool
ffmpeg - video streaming

MOTION

Note that motion supports video, but not audio.  This example configures it for streaming, and disables the local snapshots.

sudo apt-get install motion

vi /etc/motion/motion.conf
daemon on
output_pictures off
output_debug_pictures off
ffmpeg_output_movies off
ffmpeg_output_debug_movies off
ffmpeg_video_codec mpeg4
camera=/etc/motion/camera1.conf
camera=/etc/motion/camera2.conf
stream_localhost off
webcam_localhost off
camera1.conf:
camera_id = 1
videodevice /dev/video0
input -1
width W
height H
framerate F
v4l2_palette P
stream_port 8091

W,H,F,P characteristics can be identified with: ffmpeg -hide_banner -f v4l2 -list_formats all -i /dev/video0
Compressed: mjpeg : Motion-JPEG : 1920x1080 1280x720 640x360 1920x1080
Raw :  yuyv422 :  YUYV 4:2:2 : 640x360
Options for P are found in the comments in motion.conf
MJPEG = 8, YUYV = 15
The next camera goes in camera2.conf - It's videodevice will be /dev/video2 (not /dev/video1).  Make sure you pick a different ID and stream port.

Enable motion daemon
# sudo vi /etc/default/motion
Start motion
# systemctl enable motion
Check log files:
tail /var/log/syslog
dmesg
/var/log/motion/motion.log

FFMPEG / MKVSERVER

Building and Installing

Setup for cross-compiling on Linux PC
sudo apt-get install build-essential git-core
sudo git clone https://github.com/raspberrypi/tools.git /opt/tools
Fetch Repositories and ready for build
git clone git://source.ffmpeg.org/ffmpeg.git
cd ffmpeg
git clone https://code.videolan.org/videolan/x264.git
git clone https://github.com/klaxa/mkvserver_mk2.git
export CCPREFIX="/opt/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-"
Build ffmpeg and libraries
# Build x264
cd x264
./configure --host=arm-linux --cross-prefix=${CCPREFIX} --enable-static --disabl
e-asm
make
cd ..
# Build ffmpeg with x264
./configure --enable-cross-compile --cross-prefix=${CCPREFIX} --arch=armel --target-os=linux --enable-gpl --enab
le-libx264 --extra-cflags="-Ix264/" --extra-ldflags="-ldl -Lx264/"
make
cd ..
Modify mkvserver/Makefile
CC=${CCPREFIX}gcc
FFMPEG=../ffmpeg
LAV_LDFLAGS = -L ${FFMPEG}/libavcodec \
-L ${FFMPEG}/libavformat \
-L ${FFMPEG}/libavutil \
-L ${FFMPEG}/libswresample \
-L ${FFMPEG}/x264 \
-lavformat -l m \
-lavcodec -pthread -lm -lx264 -pthread -lswresample \
-lavutil -pthread -lrt -lm \
-lx264 -lpthread -lm -ldl
LAV_CFLAGS = -I${FFMPEG}
Build mkvserver
cd mkvserver
make
cd ..
Transfer 'server' to raspberry pi, name it mkvserver and place it in /usr/bin

Configuring and Launching


Useful Commands

lsusb
udevadm info -n /dev/video0
arecord -L / arecord -l
raspi-config
vcgencmd get_camera
ffmpeg -hide_banner -f v4l2 -list_formats all -i /dev/video0
v4l2-ctl --all








No comments:

Post a Comment