How to get a list of video capture devices (web cameras) on linux ( ubuntu )? (C/C++)
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;
}
How to get a list video capture devices NAMES (web cameras) on linux ( ubuntu )? (C/C++)
You are using the V4L1 API which is deprecated - V4L2 is the preferred API for new code.
In any case, the VIDIOC_QUERYCAP
ioctl()
is probably what you are looking for. You will want to have a look at the .card
field of the returned struct v4l2_capability
structure.
EDIT:
You could have a look at the source code for the v4l-info
utility, which does exactly what you want.
How to get a list of video capture devices (web cameras) on windows? (C++)
From the examples shown, copy the following code into dev.c. Then open the command line with all the SDK variables set. At the command line link to ole32.lib and oleaut32.lib. It will then show you all the devices.
cl dev.c ole32.lib oleaut32.lib
dev.exe will give out the list on the command line.
#include <windows.h>
#include <dshow.h>
#pragma comment(lib, "strmiids")
HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum)
{
// Create the System Device Enumerator.
ICreateDevEnum *pDevEnum;
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum));
if (SUCCEEDED(hr))
{
// Create an enumerator for the category.
hr = pDevEnum->CreateClassEnumerator(category, ppEnum, 0);
if (hr == S_FALSE)
{
hr = VFW_E_NOT_FOUND; // The category is empty. Treat as an error.
}
pDevEnum->Release();
}
return hr;
}
void DisplayDeviceInformation(IEnumMoniker *pEnum)
{
IMoniker *pMoniker = NULL;
while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
{
IPropertyBag *pPropBag;
HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
if (FAILED(hr))
{
pMoniker->Release();
continue;
}
VARIANT var;
VariantInit(&var);
// Get description or friendly name.
hr = pPropBag->Read(L"Description", &var, 0);
if (FAILED(hr))
{
hr = pPropBag->Read(L"FriendlyName", &var, 0);
}
if (SUCCEEDED(hr))
{
printf("%S\n", var.bstrVal);
VariantClear(&var);
}
hr = pPropBag->Write(L"FriendlyName", &var);
// WaveInID applies only to audio capture devices.
hr = pPropBag->Read(L"WaveInID", &var, 0);
if (SUCCEEDED(hr))
{
printf("WaveIn ID: %d\n", var.lVal);
VariantClear(&var);
}
hr = pPropBag->Read(L"DevicePath", &var, 0);
if (SUCCEEDED(hr))
{
// The device path is not intended for display.
printf("Device path: %S\n", var.bstrVal);
VariantClear(&var);
}
pPropBag->Release();
pMoniker->Release();
}
}
void main()
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (SUCCEEDED(hr))
{
IEnumMoniker *pEnum;
hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
if (SUCCEEDED(hr))
{
DisplayDeviceInformation(pEnum);
pEnum->Release();
}
hr = EnumerateDevices(CLSID_AudioInputDeviceCategory, &pEnum);
if (SUCCEEDED(hr))
{
DisplayDeviceInformation(pEnum);
pEnum->Release();
}
CoUninitialize();
}
}
Getting the v4l2 device number for a connected USB camera (webcam) from a C application (Linux)
I don't know if this specifically answers your question, but you can get useful information by globbing certain patterns under /dev or /sys, for example this will return the full device path (including PCI bus) of each video device,
#include <glob.h>
#include <unistd.h>
void list_videos() {
int i;
glob_t globbuf;
if (glob("/sys/class/video4linux/video*", 0, NULL, &globbuf) != 0) {
perror("glob");
return;
}
for (i=0; i < globbuf.gl_pathc; i++) {
char buf[256] = {};
if (readlink(globbuf.gl_pathv[i], buf, sizeof(buf)-1) > 0) {
puts(buf);
}
}
}
On one system with 2 cameras this prints,
../../devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1.1/2-1.1:1.0/video4linux/video0
../../devices/pci0000:00/0000:00:14.0/usb2/2-3/2-3:1.0/video4linux/video1
Other interesting glob strings include /dev/v4l/by-id/*
and /dev/v4l/by-path/*
.
Get web-cams list using VLC command line
AFAIK, this is not possible to do through VLC itself. You will probably have to do this through your OS. Although, there maybe a plugin that allows for this.
This blog article contains a section that discusses doing something like this on Ubuntu Linux but involves having to physically plug the camera in:
http://clusterbleep.net/blog/2010/07/13/webcam-recording-using-vlc-on-linux/
However, there is more in-depth treatment in this Stackoverflow question on how to get a list of cameras through the OS. It lists a series of other articles for each major OS:
How to get a list of video capture devices (web cameras) on linux ( ubuntu )? (C/C++)
How to let user select a video recording device (web-cam) with OpenCV?
Try using some OS functions to enumerate webcams. It might take some work, but this approach will guarantee that you get a list every time (unlike the OpenCV hack, which sometimes doesn't work, for some reason).
Listing available devices in python-opencv
This is a general problem of the OpenCV, as you can see below. It seems that only the builtin, or the first USB cam (only if you do not have a buildin cam) works in OpenCV:
How to use a camera with OpenCV
Cannot access usb webcam through OpenCV, Cygwin
OpenCV capture from USB not iSight (OSX)
Currently, there is no way to extract the number of cameras, as listed in this feature request:
https://code.ros.org/trac/opencv/ticket/935
Related Topics
C++ For-Loop - Size_Type VS. Size_T
Unsequenced Value Computations (A.K.A Sequence Points)
C++ Return Value Created Before or After Auto Var Destruction
Tools to Find Included Headers Which Are Unused
Why Is My Log in the Std Namespace
Is the Backslash Acceptable in C and C++ #Include Directives
Operator< Comparing Multiple Fields
C++ New Operator Thread Safety in Linux and Gcc 4
Trouble with Inheritance of Operator= in C++
How to Create an Array When the Size Is a Variable Not a Constant
Why Should Default Parameters Be Added Last in C++ Functions
Compare Two Float Variables in C++
C++11 Lambda Implementation and Memory Model
Does C++ Pass Objects by Value or Reference