43

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.

Peter Mortensen
  • 1,029
  • 1
  • 8
  • 10
fox
  • 996
  • 2
  • 8
  • 11

7 Answers7

40

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.
Steven D
  • 45,310
  • 13
  • 119
  • 114
31

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:

  • 1
    Yeah, pretty cool. I'd like to get rid of the title bar too, though... or rename it. Nice pic BTW, very "zen". – Stefan Reich May 02 '19 at 14:11
  • @StefanReich yes, hiding the title bar would be good. Looks like GNOME does not allow it though: https://unix.stackexchange.com/questions/420452/how-to-hide-title-bar-for-a-specific-window :-( Forgot why I was so happy that day! Life is generally good though, we should be happier. – Ciro Santilli OurBigBook.com May 02 '19 at 14:19
  • 1
    Very nice, easy, smooth, works perfectly. Should be top answer! – Apollys supports Monica Aug 19 '19 at 18:52
  • I cannot achieve the highest resolution supported by my webcam (which is 1920x1080). I get 1280x720 and it's blurry. Very disappointing... any suggestions? – Yan King Yin Nov 12 '20 at 14:48
  • 1
    @YanKingYin I'm not sure about this, let me know if you find out something, and also consider opening a separate question. – Ciro Santilli OurBigBook.com Nov 12 '20 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. – Yan King Yin Dec 03 '20 at 06:53
19

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

To install, use sudo apt install ffmpeg.

Henry Wilson
  • 191
  • 1
  • 2
11

Or you can try mpv command

mpv /dev/video0
hungptit
  • 211
  • 2
  • 3
  • `/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 '20 at 08: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 '20 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)
Ben
  • 141
  • 3
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.

eri
  • 865
  • 2
  • 8
  • 16
1

Preinstalled on Ubuntu: Cheese. Worked for me.

(As Gabriel Staples pointed out in a comment.)

stolsvik
  • 121
  • 4