Skip to content

video for linux

Frank Bauernöppel edited this page Nov 17, 2017 · 20 revisions

Video for Linux API version 2, or short v4l2 is part of the multimedia framework in Linux.

The v4l utilities may already be present in your image or can be build on the build host in a bitbake shell

bitbake v4l-utils

or included in the microSD card image.

load video capture source driver

for the CSI camera module

pi@raspberrypi:~ $ modprobe bcm2835-v4l2

Now, the device /dev/video0 can be used.

This can be done instead of or in addition to the CSI camera and should plug-and-play. After connecting, another v4l2 video capture device shall appear: ls /dev/video*

print device info

pi@raspberrypi:~ $ v4l2-ctl --device=0 --all

check supported video formats

pi@raspberrypi:~ $ v4l2-ctl --list-formats-ext

enable/disable preview

pi@raspberrypi:~ $ v4l2-ctl --overlay=1
pi@raspberrypi:~ $ v4l2-ctl --overlay=0

record MJPEG file

pi@raspberrypi:~ $ v4l2-ctl --set-fmt-video=width=2592,height=1944,pixelformat=MJPEG
pi@raspberrypi:~ $ v4l2-ctl --stream-mmap=3 --stream-count=1 --stream-to=testfile.jpg

test video frame rate

H.264 640x480 @ 90 fps, see https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=142536

pi@raspberrypi:~ $ v4l2-ctl -p 90
pi@raspberrypi:~ $ v4l2-ctl --set-ctrl video_bitrate=10000000
pi@raspberrypi:~ $ v4l2-ctl --set-fmt-video=width=640,height=480,pixelformat=H264
pi@raspberrypi:~ $ v4l2-ctl --stream-mmap=3 --stream-count=1000 --stream-to=/dev/null

check driver info

pi@raspberrypi:~ $ modinfo bcm2835_v4l2 

remove driver

pi@raspberrypi:~ $ modprobe -r bcm2835-v4l2

use with gstreamer

The CSI Camera module v4l2 driver can be successfully tested as follows:

gst-launch-1.0 -v -e v4l2src ! video/x-raw, format=I420, width=1280, height=720, framerate=30/1  ! omxh264enc  ! filesink location=test.h264

But it seems to be only of limited use because the pipelines often stall or simply don't link.

live streaming using v4l2rtspserver

see https://github.com/mpromonet/v4l2rtspserver for download and build instructions under Raspbian.

CSI camera plus audio input (if any) works fine:

pi@raspberrypi:~ $ v4l2rtspserver /dev/video0,plughw:1,0

An USB UVC webcam might work either by supporting H.264 or re-coding (maybe slow software re-coding). E.g. Microsoft Corp. LifeCam Studio supports raw and MJPG formats only, which could not be streamed.

live streaming using vlc

USB UVC webcam which can output MJPG (motion JPEG) without recoding:

cvlc -vv v4l2:///dev/video0:width=640:height=480:chroma=MJPG :sout="#rtp{sdp=rtsp://:8554/}"

TODO: mux with alsa audio from webcam