Sunday, September 18, 2022

Set up a webcam with Linux

You want to use your webcam with a Linux computer? Good, then this article is for you.

If you have a notebook or a laptop with an integrated webcam, this will most probably work out of the box. If you have a USB webcam, connect it to your computer.

Then, you open a console (aka terminal) and type 

cheese

The "cheese" application will start up and show your webcam's output like this:

A capture from a webcam in the program Cheese


If this works, congratulations, you can now use your webcam with video editors, conferencing software, and so on. But:

  • cheese may not be installed
  • the drivers (aka kernel modules) for the webcam may not work
  • your Linux computer may not have a graphical user interface (or you may not want it)
  • you may want to switch between several webcams

Install Cheese

If you do not have the software cheese installed, you can install it by means of your distribution. Find out your Linux distribution, open a terminal and enter:

  • for Debian, Raspbian, Ubuntu, Kubuntu, Flubuntu, Xubuntu, GEUbuntu, Edubuntu and similar:

    sudo apt-get install cheese
    • for SUSE Linux
    yast -i cheese
    Afterwards, cheese will be installed.

    Fix the webcam drivers

    To check if your webcam's kernel modules are working, watch if the kernel can see the device. To do this, open a console and type

    ls -ltr /dev/video*
    the output should be something like this
    crw-rw----+ 1 root video 81, 0 Nov 11 09:06 /dev/video0

    If this is the case, congratulations, you have exactly one webcam recognized by the Linux kernel, and it's named video0.
    If the kernel does not see the webcam, check if it is really connected well. Because times where you had to manually load drivers for webcams should be over for long time now. Anyway, I was successful (long time ago) by commanding 
    hwinfo --usb
    

    I had a Logitech Quickcam Messenger plugged in and got:

    06: USB 00.2: 0000 Unclassified device
      [Created at usb.122]                
      UDI: /org/freedesktop/Hal/devices/usb_device_46d_8da_noserial_if2
      Unique ID: Eopr.vE+cdFBwClB                                      
      Parent ID: uIhY.uOe2OKugI8D                                      
      SysFS ID: /devices/pci0000:00/0000:00:1a.2/usb3/3-1/3-1:1.2      
      SysFS BusID: 3-1:1.2                                             
      Hardware Class: unknown                                          
      Model: "Logitech QuickCam Messanger"                             
      Hotplug: USB                                                     
      Vendor: usb 0x046d "Logitech, Inc."                              
      Device: usb 0x08da "QuickCam Messanger"                          
      Revision: "1.00"                                                 
      Driver: "snd-usb-audio"                                          
      Driver Modules: "snd_usb_audio"                                  
      Speed: 12 Mbps                                                   
      Module Alias: "usb:v046Dp08DAd0100dc00dsc00dp00ic01isc02ip00"    
      Driver Info #0:                                                  
        Driver Status: quickcam_messenger is active                    
        Driver Activation Cmd: "modprobe quickcam_messenger"
      Driver Info #1:                                                  
        Driver Status: gspca is active                                 
        Driver Activation Cmd: "modprobe gspca"
      Config Status: cfg=new, avail=yes, need=no, active=unknown       
      Attached to: #20 (Hub)

    This meant I could install and load the webcam driver like this (as I was using SUSE, I used yast -i to install the driver):

    yast -i gspcav-kmp-default
    modprobe gspca
    

    Then the kernel could "see" the device:

    ls /dev/video*
    /dev/video  /dev/video0
    

    Then I started gqcam and it showed me the webcam's output. Needless to say, today I'd not use gqcam, but cheese... but that was not available on this old Linux distro, and nowadays, the driver loads automatically anyways.

    There was also a webcam which needed the uvcvideo device driver:

    yast -i uvcvideo_kmp_default
    modprobe uvcvideo
    Good luck in trying this!

    Without graphical user interface

    But what if you are using e.g. a Raspberry Pi on a remote location to just collect surveillance pictures or videos? Then you don't want to use cheese, as it requires a graphical desktop. You want to use the console only to capture video data in this case. 

    No worries, with Linux, complicated things get easy, and easy things get complicated. This is a complicated thing, so you install the right software and it works. I assume you want to look at /dev/video0 and record to the file myvideo.avi. You can:

    • use the software mencoder:
    mencoder tv:// -tv driver=v4l2:width=320:height=240:device=/dev/video0 -nosound -ovc lavc -o myvideo.avi
    
    • or use the software streamer, example for five seconds:
    streamer -c /dev/video0 -f jpeg -F stereo -o myvideo.avi -t 0:05

    Switch between webcams

    Switching between webcams should be done in the application, or you should choose another device on the command line. Only if this is absolutely not possible, you can delete the webcam device you do not want to use.



    No comments:

    Post a Comment