Skip to content

Instantly share code, notes, and snippets.

@docPhil99
Last active August 5, 2024 14:34
Show Gist options
  • Save docPhil99/21e5d4a04d1ab1aed8be7e7be373e25a to your computer and use it in GitHub Desktop.
Save docPhil99/21e5d4a04d1ab1aed8be7e7be373e25a to your computer and use it in GitHub Desktop.
Linux webcams

To get the list of webcams v4l2-ctl --list-devices

Example outout for 2 cameras

vi-output, imx219 7-0010 (platform:54080000.vi:0):
        /dev/video0

B525 HD Webcam (usb-70090000.xusb-2.2):
        /dev/video1

List the capabilites of a specific camera

v4l2-ctl --list-formats-ext --device /dev/video0

Example output

ioctl: VIDIOC_ENUM_FMT
        Index       : 0
        Type        : Video Capture
        Pixel Format: 'RG10'
        Name        : 10-bit Bayer RGRG/GBGB
                Size: Discrete 3264x2464
                        Interval: Discrete 0.048s (21.000 fps)
                Size: Discrete 3264x1848
                        Interval: Discrete 0.036s (28.000 fps)
                Size: Discrete 1920x1080
                        Interval: Discrete 0.033s (30.000 fps)
                Size: Discrete 1640x1232
                        Interval: Discrete 0.033s (30.000 fps)
                Size: Discrete 1280x720
                        Interval: Discrete 0.017s (60.000 fps)

Display the webcam video feed

gst-launch-1.0 -v v4l2src device=/dev/video0 ! xvimagesink

With specfic parameters

gst-launch-1.0 -v v4l2src device=/dev/video0 ! video/x-raw,framerate=30/1,width=1280,height=720 ! xvimagesink

See https://gstreamer.freedesktop.org/documentation/video4linux2/v4l2src.html?gi-language=c for the options

Multiple Cameras

Multiple cameras will be listed as /dev/video0, /dev/video1, etc. The problem is that the order is fixed an might change withe reboot. There a few option.

  1. Parse the output of v4l2-ctl --list-devices
  2. Make a udev rule that creates a specific /dev/ file when the camera is plugged in using it device ID.
  3. Look in /dev/v4l/by-path which has a file for each camera and use this instead of /dev/videoN. This is probably the easiest option.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment