Skip to main content

Find the actual webcam device (`/dev/videoX`) in Linux

In Linux, V4L (Video4Linux) dictates how a video-capture device (as character node) is created in the device tmpfs filesystem (devtmpfs specifically, mounted on /dev). The webcam devices are usually created as /dev/video<index> e.g. /dev/video0. Different udev rules are then used to give these devices locations in /dev based on their ID (in /dev/v4l/by-id/), bus path (in /dev/v4l/by-path/) and so on.

Now, the problem is video4linux includes a dummy device for the metadata of the actual (hardware) device; so it's always a bit tricky to get the right /dev/video<index> device to use to capture webcam streams.

For example, on my system:

% ls /dev/video*
/dev/video0  /dev/video1  /dev/video2  /dev/video3

But which one(s) would point to the actual webcam device(s)?

Here's a tiny shell snippet to do just that -- show the video devices with capture capability (e.g. a webcam):

for device in /dev/video*; do
    udevadm info "$device" | { grep -q 'CAPABILITIES=.*:capture:' && echo "$device" ;}

As an one-liner:

for device in /dev/video*; do udevadm info "$device" | { grep -q 'CAPABILITIES=.*:capture:' && echo "$device" ;}; done

On my system:

% for device in /dev/video*; do udevadm info "$device" | { grep -q 'CAPABILITIES=.*:capture:' && echo "$device" ;}; done

I'm running Linux kernel 6.2.0 on Linux Mint 21.2, but it should work the same on any Linux kernel starting from 2.1.0.

Note: The v4l2-ctl executable comes with the v4l-utils package (sudo apt-get install v4l-utils) can also show the webcam device info, but I don't have any other use case for the package so it's only logical for me that a snippet is stitched up.


Comments powered by Disqus