2

I'm writing a script that must use a particular camera plugged in to my system.

$ lsusb|grep C930e
Bus 001 Device 011: ID 046d:0843 Logitech, Inc. Webcam C930e

How can one programatically discover the /dev/video* device corresponding to a given device ID from lsusb output?

It seems like this should be easy, but apparently I am missing a keyword. :-D

2
  • 2
    /sys/class/video4linux is probably more information rich than lsusb. But I can't think of examples to give you to make it a real answer
    – infixed
    Mar 15, 2016 at 20:12
  • 1
    You are correct. I found a clue here: stackoverflow.com/a/4290924/218732
    – lysdexia
    Mar 15, 2016 at 20:27

2 Answers 2

3

I decided on what I think is a much better answer, despite requiring the installation of another package. Installing v4l-utils (debian) gives one the handy v4l2-ctl command:

$ v4l2-ctl --list-devices
HPigh Definition Webcam (usb-0000:00:14.0-11):
    /dev/video2

UVC Camera (046d:0821) (usb-0000:00:14.0-13):
    /dev/video0

Logitech Webcam C930e (usb-0000:00:14.0-9):
    /dev/video1 1.0MP H

. . . which can be accessed thusly:

def find_cam(cam):
    cmd = ["/usr/bin/v4l2-ctl", "--list-devices"]
    out, err = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate()
    out, err = out.strip(), err.strip()
    for l in [i.split("\n\t") for i in out.split("\n\n")]:
        if cam in l[0]:
            return l[1]
    return False

Gist here.

0

As mentioned by @infixed above, the /sys/class/video4linux directory contains what I needed. This is a brittle example:

#!/usr/bin/env python
import sys
import os

def find_cam_dev(cam):
    v4l2path = "/sys/class/video4linux"
    for base, subs, filenames in os.walk(v4l2path , followlinks=True):
        for filename in filenames:
            if filename == "name":
                pth = os.path.join(base, filename)
                with open(pth, "r") as f:
                    name = f.read()
                    if cam in name:
                        return os.path.split(base)[1]

if __name__ == "__main__":
    cam = "C930e"
    print(find_cam_dev(cam))

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .