45

In Windows I can open "My Computer" and click on the "Webcam" icon to get a feed from my webcam. I can also take snapshots of that feed.

Can I do the same in Ubuntu? Without installing any extra applications like Photo Booth.

5
  • 6
    mplayer tv://device=/dev/video
    – Stefan
    Oct 20, 2010 at 7:13
  • @Stefan: can you? that's too simple!
    – phunehehe
    Oct 20, 2010 at 8:47
  • 1
    "Without installing any extra applications". That's a pretty weird and severe constraint. I would just install something like cheese.
    – Wim Coenen
    Oct 20, 2010 at 9:43
  • @phunene, well it works for me.... sometimes
    – Stefan
    Oct 20, 2010 at 13:40
  • @phunehehe: Yes, we can ;-)
    – echox
    Oct 20, 2010 at 17:36

7 Answers 7

42

Since you want an answer "without installing any extra applications like Photobooth," I've tried to give a solution that doesn't depend on very much. Also I'm assuming that your webcam uses "Video4Linux2" and that it is /dev/video0. If this is a modern webcam and if you only have one, these are pretty good assumptions.

From the command line:

$ gst-launch-0.10 v4l2src device=/dev/video0 ! xvimagesink

Note that "v4l2src" contains a lowercase L and not the number 1. On your system the command may be gst-launch or something starting with gst-launch but with a different version number. Tab completion should help you find the exact command name. This tool is in the gstreamer0.10-tools package on my Ubuntu system, which is a dependency of libgstreamer, which is a dependency of a large number of the apps on my Ubuntu system and is likely present in the default installation.

Other Applications

If you don't mind installing other applications, here is how you can do this in a few other applications. All of them can easily be installed via apt-get or another package manager of your choosing:

  • VLC: $ vlc v4l2:///dev/video0 Also, you can do this from the VLC GUI by going to File->Open Capture Device
  • mplayer: mplayer tv://device=/dev/video01 (from Stefan in the comments)
  • Cheese: This is a photobooth-like app that is very simple to use.
4
  • VLC is so flexible
    – Andrew
    Nov 28, 2018 at 4:45
  • 2
    Note that Cheese is super easy to use and comes pre-installed with Ubuntu I believe, which is nice. One can just search for it in the programs menu and launch it from there. Jan 30, 2019 at 18:11
  • Neither gst-launch nor vlc work for me, getting various errors. Answer below using guvcview was super simple and worked perfectly. Aug 19, 2019 at 18:53
  • Note that Cheese is buggy and tends to just grab a camera rather than honor its -d / --device= argument Jan 24, 2021 at 0:58
32

guvcview

This program is ideal for screencasts, as it can show just the camera on a window and nothing else:

sudo apt-get install guvcview
guvcview

Then just use any screen recorder to make a feed. recordmydesktop works fine.

Tested on Ubuntu 18.04.

Related questions:

6
  • 1
    Yeah, pretty cool. I'd like to get rid of the title bar too, though... or rename it. Nice pic BTW, very "zen". May 2, 2019 at 14:11
  • @StefanReich yes, hiding the title bar would be good. Looks like GNOME does not allow it though: unix.stackexchange.com/questions/420452/… :-( Forgot why I was so happy that day! Life is generally good though, we should be happier. May 2, 2019 at 14:19
  • 1
    Very nice, easy, smooth, works perfectly. Should be top answer! Aug 19, 2019 at 18:52
  • 1
    @YanKingYin I'm not sure about this, let me know if you find out something, and also consider opening a separate question. Nov 12, 2020 at 14:57
  • 1
    @CiroSantilli郝海东冠状病六四事件法轮功 I just bought a new HD web cam which is supported by Linux (did a web search to find a list of them). Looks like there isn't much I could do with the old one, on Linux. So I just donated it to the sharing shelf in the neighborhood. Dec 3, 2020 at 6:53
21

ffplay /dev/video0 is one of the simpler methods, and will work provided you have ffmpeg installed.

To install, use sudo apt install ffmpeg.

1
11

Or you can try mpv command

mpv /dev/video0
2
  • /dev/video0 does not exist on my notebook. I guess, I have to activate the webcam first somehow? guvcview does that automatically.
    – mxmlnkn
    Mar 20, 2020 at 8:30
  • You need to make sure that your webcam is working. I do not know your webcam model and the used Linux distribution so I cannot give you a detail suggestion.
    – hungptit
    Mar 20, 2020 at 14:19
4

A slightly different syntax worked for me using mplayer:

mplayer -tv device=/dev/video1 tv://

I am using a plugged-in webcam (not the built-in). So I changed /dev/video0 to /dev/video1. But Stefan's syntax above seemed to default to the built-in because of an argument parsing error. See marked lines in the output:

baxelrod@it6598 ~ $ mplayer tv://device=/dev/video1
MPlayer 1.2.1 (Debian), built with gcc-5.3.1 (C) 2000-2016 MPlayer Team
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
Playing tv://device=/dev/video1.
The filename option must be an integer: dev/video1     <--
Struct tv, field filename parsing error: dev/video1    <--
TV file format detected.
...
Selected device: Integrated Camera                     <--
...

When I use the syntax I posted, I don't get the error lines, and I get this instead:

Selected device: UVC Camera (046d:081b)
2

Preinstalled on Ubuntu: Cheese. Worked for me.

(As Gabriel Staples pointed out in a comment.)

1

To stream camera over network

cat << EOF > index.html
<html><head><script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script></head>
<body><video id="video"></video>
<script>
  var video = document.getElementById('video');
  var videoSrc = '/playlist.m3u8';
  if (Hls.isSupported()) {
    var hls = new Hls();
    hls.loadSource(videoSrc);
    hls.attachMedia(video);
    hls.on(Hls.Events.MANIFEST_PARSED, function() {
      video.play();
    });
  }
  else if (video.canPlayType('application/vnd.apple.mpegurl')) {
    video.src = videoSrc;
    video.addEventListener('loadedmetadata', function() {
      video.play();
    });
  }
</script></body></html>
EOF

gst-launch-1.0 v4l2src device=/dev/video0 ! videoconvert ! x264enc ! mpegtsmux ! hlssink max-files=5 &
python3 -m http.server --port 8080

On other side

vlc http://192.168.0.2:8080/playlist.m3u8

or open in browser http://192.168.0.2:8080/index.html, then right button click on image -> play.

You must log in to answer this question.

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