How to live stream video from webcam on Linux

Have you ever wanted to live stream a video feed from your webcam for someone else? You could use any existing video chat software such as Skype, Google+ Hangouts. However, if you do not want to rely on any third-party streaming infrastructure, you can set up your own webcam streaming server in house.

In this tutorial, I will describe how to live stream video from webcam by using VLC. VLC is an open-source, cross platform media player which can handle virtually all video and audio formats. VLC can also run as a streaming server supporting various streaming protocol such as RTP, HTTP, RTSP, etc.

Before attempting to set up webcam streaming on your own, you need to understand the huge potential security risk of streaming a webcam over the Internet. Anyone can tap into your streaming feed unless you properly access control the streaming server, and encrypt the streaming traffic. I'll briefly mention several security protections against threats towards the end of the tutorial.

Install VLC on Linux

Before setting up a streaming server with VLC, install VLC first.

To install VLC on Debian, Ubuntu or Linux Mint:

$ sudo apt-get install vlc

To install VLC on Fedora, first enable RPM Fusion's free repository, and then run:

$ sudo yum install vlc

To install VLC on CentOS or RHEL 6, first set up EPEL repository, and then use the following commands:

$ cd /etc/yum.repos.d/
$ sudo wget http://pkgrepo.linuxtech.net/el6/release/linuxtech.repo
$ sudo yum install vlc

Verify Webcam Device on VLC

Next, verify whether a webcam device is successfully detected on your Linux system and VLC.

To do so, first find out the device name of your webcam with the following command. In this example, the device name of the webcam is /dev/video0.

$ ls /dev/video*
/dev/video0

Now use the following command to test video from your webcam. Replace "video0" with your own device name.

$ vlc v4l2:///dev/video0

If your webcam is successfully detected by VLC, you should see a video stream of yourself.

Configure Webcam Streaming on VLC

Assuming that your webcam is successfully recognized by VLC, I am going to show how to configure webcam streaming. In this setup, webcam is streamed over HTTP in WMV format.

To configure VLC for webcam streaming, first launch VLC.

$ vlc

Choose "Streaming" from VLC menu.

On the screen, choose webcam/audio device name (e.g., /dev/video0 for webcam, and hw:0,0 for audio). Click on the checkbox of "Show more options". Make a note of value strings in "MRL" and "Edit Options" fields. We will use these strings later in the tutorial. Click on "Stream" button at the bottom.

Verify the video source (e.g., v4l2:///dev/video), and click on "Next" button to go next.

On this screen, choose the destination (i.e., streaming method/target) of webcam streaming. Since we use HTTP streaming, choose "HTTP" from the drop down list, and click on "Add" button.

Next, specify port number and path of a streaming service. For port number, fill in 8080 (assuming that the port number is not occupied), and "/stream.wmv" as path. For transcoding, choose "Video - WMV + WMA (ASF)" profile from the drop down list. Click on "Next" button.

On this screen, you will see stream output string which is automatically generated. Make a note of this string, and click on "Stream" button at the bottom.

At this point, VLC should start streaming video from your webcam over HTTP. You will not see anything on VLC screen itself since streaming traffic is sent directly to localhost at TCP port number 8080.

To verify that VLC is running correctly at TCP port 8080, run the following command, and look for VLC.

$ sudo netstat -nap | grep 8080
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      11959/vlc       
tcp6       0      0 :::8080                 :::*                    LISTEN      11959/vlc       

Live Stream Webcam from the Command Line

The method described earlier is setting up webcam streaming via VLC GUI. VLC comes with a command-line utility called cvlc which allows you to use VLC without its interface. cvlc is useful when you have to configure VLC streaming on a remote headless host.

To set up webcam streaming from the command line, you need to use three string values that I told you to make note of during the above GUI-based configuration. In this test setup, those string values were:

  • "MRL": v4l2:///dev/video0
  • "Edit options": :v4l2-standard= :input-slave=alsa://hw:0,0 :live-caching=300
  • "Stream output string": :sout="#transcode{vcodec=WMV2,vb=800,scale=1,acodec=wma2,ab=128,channels=2,samplerate=44100}:http{dst=:8080/stream.wmv}"
  • Concatenate those three strings to contruct arguments for cvlc command. That is, the following command will launch webcam streaming over HTTP in WMV format.

    $ cvlc v4l2:///dev/video0 :v4l2-standard= :input-slave=alsa://hw:0,0 :live-caching=300 :sout="#transcode{vcodec=WMV2,vb=800,scale=1,acodec=wma2,ab=128,channels=2,samplerate=44100}:http{dst=:8080/stream.wmv}"

    In the above command, specific string values (e.g., audio device name) might be slightly different depending on your hardware configuration. So go through the above GUI-based VLC configuration yourself to find out the correct cvlc arguments for your system.

    Watch Streaming Video from Webcam

    Once a streaming server starts running, the webcam live feed is available at http://<ip_address_of_webcam_host>:8080/stream.wmv

    You can use VLC player or MPlayer to access the webcam feed as follows.

    $ vlc http://<ip_address_of_webcam_host>:8080/stream.wmv
    $ mplayer http://<ip_address_of_webcam_host>:8080/stream.wmv

    If you are testing the feed from the same host, use loopback address 127.0.0.1 instead.

    Security Protections for Your Webcam Feed

    As mentioned earlier, it is not a good idea to stream your webcam over the Internet without any security protection. Consider some of these measures to protect your webcam feeds.

    • Download the latest firmware from webcam vendors.
    • Stream webcam over a secure VPN
    • Use iptables to whitelist IP addresses that you trust, and block all other connections.

    Subscribe to Xmodulo

    Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

    12 thoughts on “How to live stream video from webcam on Linux

    1. When I attempt to run this from the CLI I get the following error message:

      [0x109ed08] stream_out_standard stream out error: no mux specified or found by extension
      [0x109e9d8] main stream output error: stream chain failed for `standard{mux="",access="”#transcodesamplerate=44100",dst="http{dst=:8080/stream.wmv}”"}'

      • You need to have double quotes starting from before the #transcode.. part of the generated stream output string. These are shown in the article above, but not generated by vlc. I also had to drop the :sout-keep that was at the end of this string.

    2. Here's another security protection you can add to your list. It works great with an ffmpeg stream so I can only assume it'll work with this as well.

      Use a mod_proxy. Assuming the server streaming the video is in a LAN, you can setup a proxy through apache with htaccess authentication. You can keep, in this example, 8080 behind your firewall. You'll just point to the domain, subdirectory or alternate port (depending on how you want to do it) to gain access.

      Thanks for this tutorial. VLC is way easier to set up than ffmpeg.....but as of yet I can't get the command line to work, even though it works perfectly through the GUI.

    3. Hi. This below output is what I got afterwards following guidelines. I'd appreciate any help.
      VLC media player 2.0.9 Twoflower (revision 2.0.8a-20-g902dc4c)
      [0x9fbd8f0] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
      ALSA lib pcm.c:7843:(snd_pcm_recover) overrun occurred
      [wmv2 @ 0xb550d2c0] removing common factors from framerate
      Segmentation fault

    4. Hi Dan Nanni,
      I configure my ubuntu 10.4 following your instructions but it can not play the sound from mic (though video is ok). I specified my input device as:
      :input-slave=alsa://hw:0,0
      or
      :input-slave=alsa://plughw:Intel
      or
      :input-slave=alsa://plughw:0,0
      but all of them not work (while i test with recorder tool it ok).
      My device info:
      arecord -l
      **** List of CAPTURE Hardware Devices ****
      card 0: Intel [HDA Intel], device 0: ALC269 Analog [ALC269 Analog]
      Subdevices: 1/1
      Subdevice #0: subdevice #0
      tuepd@tuepd-laptop:/dev/snd$

      How could i streaming with audio from microphone???

    Leave a comment

    Your email address will not be published. Required fields are marked *

    Current day month ye@r *