Search code examples
c#libvlcsharp

LibVLCSharp how to get list of webcams


VLC GUI shows the list of available webcams, like v4l2:///dev/video0 and v4l2:///dev/video1, I am wondering is there a way to get a list of available webcams? what about their default resolution?

enter image description here

I tried this but md.MediaList is empty.

var mds = libVlc.MediaDiscoverers(MediaDiscovererCategory.Devices);
if (mds.Any(x => x.LongName == "Video capture"))
{
    var devices = mds.First(x => x.LongName == "Video capture");
    var md = new MediaDiscoverer(libVlc, devices.Name);
    foreach (var media1 in md.MediaList)
    {
       // Nothing ...
    }
}

Solution

  • Your MediaDiscoverer is empty because you never call md.Start(). For more info, I found this really helpful: /LibVLCSharp/MediaDiscoverer.cs

    That being said, I had no success using MediaDiscoverer to look for webcams myself.

    If you don't insist on using LibVLC, you can list all camera devices without any third party software: How can I get a list of camera devices from my PC C#

    from Francesco Bonizzi:

    public static List<string> GetAllConnectedCameras()
    {
        var cameraNames = new List<string>();
        using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE (PNPClass = 'Image' OR PNPClass = 'Camera')"))
        {
            foreach (var device in searcher.Get())
            {
                cameraNames.Add(device["Caption"].ToString());
            }
        }
    
        return cameraNames;
    }