Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

  • How to get a list of video capture devices on linux? and special details on getting cameras NAMES with correct, tested answers
  • How to get a list of video capture devices on Mac OS? with correct, not yet tested by my answers
  • How to get a list of video capture devices on windows? with correct, tested answers
  • How to get a list video capture devices NAMES using Qt (crossplatform)?
like image 969
Rella Avatar asked Nov 27 '10 08:11

Rella


People also ask

Where is v4l utils?

The v4l-utils are a series of packages for handling media devices. It is hosted at [v4l-utils. git], and packaged on most distributions.

What is V4L2?

38. V4L2 is the second version of V4L. Video4Linux2 fixed some design bugs and started appearing in the 2.5. x kernels. Video4Linux2 drivers include a compatibility mode for Video4Linux1 applications, though the support can be incomplete and it is recommended to use Video4Linux1 devices in V4L2 mode.


2 Answers

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
like image 69
Denio Mariz Avatar answered Oct 06 '22 17:10

Denio Mariz


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.

like image 37
GreyCat Avatar answered Oct 06 '22 16:10

GreyCat