如何在 linux ( ubuntu ) 上获取视频捕获设备(网络摄像头)的列表?(C/C++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4290834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a list of video capture devices (web cameras) on linux ( ubuntu )? (C/C++)
提问by Rella
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:
所以我需要的很简单 - 当前可用的视频捕获设备(网络摄像机)的列表。我需要在简单的 C 或 C++ 控制台应用程序中使用它。通过列表,我的意思是类似这样的控制台输出:
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!)
所以这看起来很简单,但我有一个要求 - 尽可能多地使用本机操作系统 api - 没有外部库 - 毕竟 - 我们想要的只是打印出一个列表 - 不要飞上月球!)
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 NAMESwith 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)?
- 如何在 linux 上获取视频捕获设备列表?以及使用正确、经过测试的答案获取摄像机名称的特殊细节
- 如何获取 Mac OS 上的视频捕获设备列表?正确的,尚未通过我的答案测试
- 如何获取 Windows 上的视频捕获设备列表?有正确的、经过测试的答案
- 如何使用 Qt(跨平台)获取列表视频捕获设备名称?
采纳答案by BlueDog
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.
这是我放置的代码片段。大概是出自一本书。我想您可以遍历所有 /dev/videoN 节点并获取信息。
#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;
}
回答by GreyCat
It's easy by just traversing sysfs devices by a given class. The following command-line one liner would do so:
只需按给定的类遍历 sysfs 设备就很容易。以下命令行单行代码会这样做:
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:
你可以在 C/C++ 应用程序中做同样的事情,只需打开/sys/class/video4linux
目录,它就会有你所有网络摄像头的符号链接作为 video4linux 设备:
$ 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.
您可以按照每个符号链接到每个设备的目录并读取name
该目录中文件的完整内容以获取名称。
回答by tejusadiga2004
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 ...
V4L2 文档说每种类型可以有 64 个允许的设备。例如对于路径 /dev/video 可以有 64 个设备地址为 /dev/video0、/dev/video1、/dev/video2 ...
Iterate over 64 devices until the ioctl retuens ENIVAL which specifies end of the tree.
迭代 64 个设备,直到指定树末端的 ioctl 返回 ENIVAL。
回答by Denio Mariz
You can use the following bash command:
您可以使用以下 bash 命令:
v4l2-ctl --list-devices
In order to use the above command, you must install package v4l-utilsbefore. In Ubuntu/Debian you can use the command:
为了使用上述命令,您必须先安装包v4l-utils。在 Ubuntu/Debian 中,您可以使用以下命令:
sudo apt-get install v4l-utils