[Python] How to get list of all the available capture devices in python without using any...

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I want to retrieve a list of available webcams on Windows, without relying on external libraries such as OpenCV, PyGrabber, or Pygame.

    Although I found a code snippet that accomplishes this task, but it uses WMIC. Unfortunately, when I tested it on another Windows device, I encountered an error stating 'wmic' is not recognized as an internal or external command

    import subprocess

    def get_webcams_windows():
    try:
    # Execute the WMIC command to get a list of video capture devices
    result = subprocess.check_output(
    'wmic path win32_pnpentity where "Description like \'%Video%\'" get Name',
    shell=True,
    text=True
    )
    webcams = result.strip().split('\n')[1:] # Skip the header
    return [webcam.strip() for webcam in webcams if webcam.strip()] # Filter out empty lines
    except Exception:
    return []

    webcam_list = get_webcams_windows()
    print(webcam_list)


    Is there any other efficient method?

    Continue reading...

Compartilhe esta Página