How to get a list of video capture devices (web cameras) on linux ( ubuntu )? (C/C++)
Asked Answered
H

5

37

So all I need is simple - a list of currently avaliable video capture devices (web cameras). I need it in simple C or C++ console app. By list I mean something like such console output:

1) Asus Web Camera
2) Sony Web Camera

So It seems simple but I have one requirement - use of native OS apis as much as possible - no external libs - after all - all we want is to print out a a list - not to fly onto the moon!)

How to do such thing?


also from this series:

Hermaherman answered 27/11, 2010 at 8:58 Comment(2)
Can you get it from /proc/bus/input/devicesKurbash
The question is how exactly to do it from code?Hermaherman
A
17

This is a code snippet I had laying around. Probably from a book. I guess you could just iterate over all /dev/videoN nodes and get the info.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev.h>

int main(){
    int fd;
    struct video_capability video_cap;
    struct video_window     video_win;
    struct video_picture   video_pic;

    if((fd = open("/dev/video0", O_RDONLY)) == -1){
        perror("cam_info: Can't open device");
        return 1;
    }

    if(ioctl(fd, VIDIOCGCAP, &video_cap) == -1)
        perror("cam_info: Can't get capabilities");
    else {
        printf("Name:\t\t '%s'\n", video_cap.name);
        printf("Minimum size:\t%d x %d\n", video_cap.minwidth, video_cap.minheight);
        printf("Maximum size:\t%d x %d\n", video_cap.maxwidth, video_cap.maxheight);
    }

    if(ioctl(fd, VIDIOCGWIN, &video_win) == -1)
        perror("cam_info: Can't get window information");
    else
        printf("Current size:\t%d x %d\n", video_win.width, video_win.height);

    if(ioctl(fd, VIDIOCGPICT, &video_pic) == -1)
        perror("cam_info: Can't get picture information");
    else
        printf("Current depth:\t%d\n", video_pic.depth);

    close(fd);
    return 0;
}
Arithmetician answered 27/11, 2010 at 9:9 Comment(3)
and ho to iterate thrue N? (we do not know N do we?)Hermaherman
Right we have to guess a big enough N. Use the accept() function to check what /dev/video nodes with n from 0..N exist. See the accepted answer over here #230562Arithmetician
This answer is ridiculously overcomplicated in light of @GregyCat's below.Atmosphere
L
60

You can use the following bash command:

v4l2-ctl --list-devices

In order to use the above command, you must install package v4l-utils before. In Ubuntu/Debian you can use the command:

sudo apt-get install v4l-utils
Latifundium answered 8/8, 2014 at 20:20 Comment(2)
For those on Arch Linux, v4l2-ctl is provided in the v4l-utils package.Eyetooth
I found using v4l2-ctl --list-devices to be the best answer for my particular problem. I ended up using it for a simple python configurator. Gist here.Zodiac
J
40

It's easy by just traversing sysfs devices by a given class. The following command-line one liner would do so:

for I in /sys/class/video4linux/*; do cat $I/name; done

You can do the same thing in C/C++ application, by just opening up /sys/class/video4linux directory, it will have symlinks to all your web cameras as video4linux devices:

$ ls -al /sys/class/video4linux                          
drwxr-xr-x  2 root root 0 Ноя 27 12:19 ./
drwxr-xr-x 34 root root 0 Ноя 26 00:08 ../
lrwxrwxrwx  1 root root 0 Ноя 27 12:19 video0 -> ../../devices/pci0000:00/0000:00:13.2/usb2/2-5/2-5:1.0/video4linux/video0/

You can follow every symlink to a directory of every device and read full contents of name file in that directory to get the name.

Joint answered 27/11, 2010 at 9:23 Comment(3)
@Kabumbus the 1-liner above is quite good, and easy to transform into a c code. +1 from meThatch
One-liner perfect for me. Thanks! Easy peasy to insert into any code.Become
Worked on adb shell as root as well.Haemophilic
A
17

This is a code snippet I had laying around. Probably from a book. I guess you could just iterate over all /dev/videoN nodes and get the info.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev.h>

int main(){
    int fd;
    struct video_capability video_cap;
    struct video_window     video_win;
    struct video_picture   video_pic;

    if((fd = open("/dev/video0", O_RDONLY)) == -1){
        perror("cam_info: Can't open device");
        return 1;
    }

    if(ioctl(fd, VIDIOCGCAP, &video_cap) == -1)
        perror("cam_info: Can't get capabilities");
    else {
        printf("Name:\t\t '%s'\n", video_cap.name);
        printf("Minimum size:\t%d x %d\n", video_cap.minwidth, video_cap.minheight);
        printf("Maximum size:\t%d x %d\n", video_cap.maxwidth, video_cap.maxheight);
    }

    if(ioctl(fd, VIDIOCGWIN, &video_win) == -1)
        perror("cam_info: Can't get window information");
    else
        printf("Current size:\t%d x %d\n", video_win.width, video_win.height);

    if(ioctl(fd, VIDIOCGPICT, &video_pic) == -1)
        perror("cam_info: Can't get picture information");
    else
        printf("Current depth:\t%d\n", video_pic.depth);

    close(fd);
    return 0;
}
Arithmetician answered 27/11, 2010 at 9:9 Comment(3)
and ho to iterate thrue N? (we do not know N do we?)Hermaherman
Right we have to guess a big enough N. Use the accept() function to check what /dev/video nodes with n from 0..N exist. See the accepted answer over here #230562Arithmetician
This answer is ridiculously overcomplicated in light of @GregyCat's below.Atmosphere
A
0

V4L2 documentation says that there can be 64 allowed devices for each type. For instance for path /dev/video there can be 64 devices addresssed as /dev/video0, /dev/video1, /dev/video2 ...

Iterate over 64 devices until the ioctl retuens ENIVAL which specifies end of the tree.

Assuming answered 27/5, 2013 at 6:2 Comment(0)
I
0

You also need to ignore metadata devices like /dev/video1 (Ubuntu >= 19.10)

At that Ubuntu version, the relevant Linux kernel/modules now have a /dev/video1 device even if you have just one camera, and this device contains metadata for /dev/video0.

If you have a second camera, then /dev/video2 would be the camera, and /dev/video3 its metadata and so on.

We can check this with:

sudo v4l2-ctl --device=/dev/video0 --all
sudo v4l2-ctl --device=/dev/video1 --all

which shows respectively:

        Device Caps      : 0x04200001
                Video Capture 

        Device Caps      : 0x04a00000
                Metadata Capture

TODO: find out how to query that from /sys to make it cleaner to detect from C/C++. Perhaps the "only use even devices" rule also works.

Related:

Here's a demo of using the video data from C/C++: Image Processing with GLSL shaders?

Incidental answered 18/4, 2024 at 14:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.