118

Is there a terminal command that lists all the webcams connected to my computer including the native one? Maybe using the ffmpeg package?

1
  • <<<<< lsusb >>>>
    – Qasim
    Commented Sep 22, 2013 at 20:33

3 Answers 3

212

v4l2-ctl --list-devices

sudo apt install v4l-utils
v4l2-ctl --list-devices

Sample output with a single camera:

Integrated Camera (usb-0000:00:1a.0-1.6):
        /dev/video0

Tested on Ubuntu 16.04, Lenovo ThinkPad P51.

video1 metadata device

On Ubuntu 19.10 Lenovo Thinkpad P51 however, it lists two such devices, video0 and video1, but I only have one camera, and can only see images from video0 with ffplay. This has been asked at:

What happens is that /dev/video1 contains some kind of video metadata only and not the images as can been seen from:

sudo v4l2-ctl --device=/dev/video0 --all
sudo v4l2-ctl --device=/dev/video1 --all

which shows respectively:

        Device Caps      : 0x04200001
                Video Capture 

        Device Caps      : 0x04a00000
                Metadata Capture

How to see the camera image live

My favorite:

sudo apt install ffmpeg
ffplay /dev/video0

enter image description here

Another good one that shows only camera and nothing else:

sudo apt-get install guvcview
guvcview

Take a single picture from the command line

Take a picture from terminal

ffmpeg -f v4l2 -video_size 1280x720 -i /dev/video0 -frames 1 out.jpg

Record a video from the command line

Parameters chosen based on "How to get camera parameters like resolution" below:

ffmpeg -f v4l2 -framerate 30 -video_size 1280x720 -input_format mjpeg \
  -i /dev/video0 -c copy out.mkv

Then:

ffprobe out.mkv

contains as expected:

Stream #0:0: Video: mjpeg (Baseline), yuvj422p(pc, bt470bg/unknown/unknown), 1280x720, 30 fps, 30 tbr, 1k tbn, 1k tbc (default)

If you choose wrong parameters, the resolution might be low. The camera already outputs a specific encoded format, and the simplest way to record is to just copy that format as above:

TODO If I replace -c copy out.mkv with out.ogv to try and record directly to an open format (unlike MJPEG), I got a low resolution video.

Interactive image/video capture with preview

Picture/Video capture programs

For the minimalists, this opens a "preview window" with ffplay, and once you close that, out.mkv contains the recorded video:

ffmpeg -f v4l2 -framerate 30 -video_size 1280x720 -input_format mjpeg \
  -i /dev/video0 -c copy -f matroska - |
  tee out.mkv |
  ffplay -

For non-minimalists, Cheese is a simple and good GUI program:

sudo apt install cheese

enter image description here

TODO as of Ubuntu 23.10, the audio and video were about 0.5s out of sync which was noticeably annoying. Another program that overcame the offset was the weirdly named guvcview:

sudo apt-get install guvcview
guvcview

This one seems to also have more configuration options, but was still quite simple to use.

Record a video and microphone audio from the command line with FFmpeg

https://stackoverflow.com/questions/19828236/how-to-record-audio-with-ffmpeg-on-linux I got video and audio, but they were out of sync.

Read camera data from C/C++

https://stackoverflow.com/questions/4290834/how-to-get-a-list-of-video-capture-devices-web-cameras-on-linux-ubuntu-c

A concrete C++ example that processes images from the camera on the GPU with OpenGL and shows it live: https://stackoverflow.com/questions/13693946/image-processing-with-glsl-shaders/40641014#40641014

enter image description here

How to get camera parameters like resolution

v4l2-ctl --list-formats-ext

produces some good information:

ioctl: VIDIOC_ENUM_FMT
    Type: Video Capture

    [0]: 'YUYV' (YUYV 4:2:2)
        Size: Discrete 640x480
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 320x180
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 320x240
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 352x288
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 424x240
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 640x360
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 848x480
            Interval: Discrete 0.050s (20.000 fps)
        Size: Discrete 960x540
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 1280x720
            Interval: Discrete 0.100s (10.000 fps)
    [1]: 'MJPG' (Motion-JPEG, compressed)
        Size: Discrete 640x480
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 320x180
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 320x240
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 352x288
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 424x240
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 640x360
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 848x480
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 960x540
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)
        Size: Discrete 1280x720
            Interval: Discrete 0.033s (30.000 fps)
            Interval: Discrete 0.067s (15.000 fps)

Another thing this output suggests is that besides the MJPG compressed output, there is also a lossless YUYV natively supported by the webcam, though that cannot reach 30 fps on FHD. TODO is there a way to see which format the video is currently outputting?

How to get the corresponding ffmpeg encodings

v4l2-ctl --list-formats-ext gives the Linux kernel name of things, e.g . YUYV 4:2:2. But to do stuff with ffmpeg, you need to know the ffmpeg name sometimes. You can do it like this:

ffmpeg -f v4l2 -list_formats all -i /dev/video0

sample output:

[video4linux2,v4l2 @ 0x555ba7267240] Raw       :     yuyv422 :           YUYV 4:2:2 : 640x480 320x180 320x240 352x288 424x240 640x360 848x480 960x540 1280x720
[video4linux2,v4l2 @ 0x555ba7267240] Compressed:       mjpeg :          Motion-JPEG : 640x480 320x180 320x240 352x288 424x240 640x360 848x480 960x540 1280x720

This for example told us that the ffmpeg name for YUYV 4:2:2 is yuyv422.

Just having some fun at this point

python -m pip install -U yolov5==7.0.9
yolov5 detect --source 0

enter image description here

Tested on Ubuntu 22.10, Lenovo ThinkPad P51. Ludicrously low GIF resolution uploaded here to fit the 2 MB max Stack Exchange image size, raw at: https://www.youtube.com/watch?v=1MD3Wn7e6OE showing that it handles my actual camera resolution.

video2 infrared device

I was re-running parts of this answer on my newer Lenovo ThinkPad P14s with Ubuntu 23.10 when I noticed that now I had a video2 and video3 which peaked my interest. video3 was the metadata for video2 like video1 is the metadata for video0. But what was this mysterious video2? Comparing to my main camera:

diff -u <( sudo v4l2-ctl --device=/dev/video0 --all ) \
        <( sudo v4l2-ctl --device=/dev/video2 --all )

I saw that it was some kind of low resolution greyscale camera:

-       Width/Height      : 1280/720    
-       Pixel Format      : 'MJPG' (Motion-JPEG)                                                                                                                                              
+       Width/Height      : 640/360                                                            
+       Pixel Format      : 'GREY' (8-bit Greyscale)

and Googling suggested that it was infrared, which I had no idea I had! Apparently the use case is facial recognition to unlock devices so as to make it harder for someone to unlock the device with just a video of your face, and is used in particular by "Windows Hello".

ffmpeg -f v4l2 -list_formats all -i /dev/video2

gave:

[video4linux2,v4l2 @ 0x5e00d8e51ac0] Raw  : gray :  8-bit Greyscale : 640x360

and so I managed to record a video with:

ffmpeg -y -f v4l2 -framerate 30 -video_size 640x360 -input_format gray \
  -i /dev/video2 -c copy out.mkv

TODO is the framerate actually 30?

A red LED lights up while that camera is being used on this device, rather than a white LED which shows up when the regular camera is in use.

Related: Four /dev/video entries but just one camera

11
  • 6
    On 16.04.1 error: Failed to open /dev/video0: No such file or directory
    – Xaqron
    Commented Jan 3, 2017 at 16:41
  • @Xaqron also try installing libv4l-dev if not already installed. Also give computer model if you can. Cheers. Commented Jan 3, 2017 at 18:00
  • 1
    Just because I keep coming back to this answer, it's now: v4l2-ctl --list-devices. That's lower in the answer but the top in bold text doesn't have the 2.
    – ksclarke
    Commented Oct 11, 2017 at 16:40
  • 1
    $ v4l2-ctl --list-devices Failed to open /dev/video0: No such file or directory
    – Mona Jalal
    Commented Jun 21, 2018 at 21:11
  • 2
    Beware that it says 4l2, not 412. (Unrelated to other comments.)
    – dremodaris
    Commented Jul 17, 2018 at 14:01
84

To list all video devices picked up by the kernel

ls -ltrh /dev/video*

To list all devices attached to USB use lsusb ; to list all devices attached to PCI use lspci

4
  • 1
    $ ls -ltrh /dev/video* ls: cannot access '/dev/video*': No such file or directory
    – Mona Jalal
    Commented Jun 21, 2018 at 21:11
  • @MonaJalal That means it couldn’t find any devices.
    – JMY1000
    Commented Jan 25, 2019 at 22:23
  • this does not work on my surface pro 3. I can use cheese and it detects my camera just fine but lspci and the command above both do not show information about the webcam hardware. This is especially important because I want to use Kamera which asks to select from a long list. Commented Mar 12, 2019 at 0:13
  • 2
    The "-ltrh" in the "ls" command seems to be a bit of overkill "ls -l /dev/video*" should suffice. The -"t" sorts by time, the "-r" reverses the sort order and the "-h" gives you "human readable" sizes -- none of which appear very helpful here. Not harmful, but unnecessary. Commented Oct 24, 2020 at 12:36
1

For Windows you can use the pygrabber library: https://github.com/bunkahle/pygrabber

To check the user friendly names of the connected webcams:

from __future__ import print_function
from pygrabber.dshow_graph import FilterGraph

graph = FilterGraph()
print(graph.get_input_devices())

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .