piwebcam

Raspberry Pi Webcam Over the Internet Using MJPG-Streamer

View your Raspberry Pi Webcam from anywhere.  Connect to it from a unique domain name and check in on your house or pets while you are away.

Requirements For This Walkthrough

Materials

  1. Local network
  2. Mac or PC
  3. Raspberry Pi camera module, a Compatible Webcam (or the exact same one I used to write this tutorial)
  4. Raspberry Pi running Raspbian “wheezy”
  5. Domain name
  6. Router capable of supporting DynDNSnoip, or other dynamic DNS service; or a software-based dynamic DNS client (not covered in this walkthrough)
  7. lighttpd Web server running on the Pi (optional–allows your to embed a stream to a Webpage, which can be password-protected)
  8. HDMI Cable (*optional)
  9. Keyboard (*optional)
  10. Mouse (*optional)
  11. Monitor with HDMI input (*optional)
  12. *If the Raspberry Pi is set up as a headless machine, you will not need a monitor, keyboard, or mouse–just another computer, which would be used to access it remotely over the network via SSH.

Downloads

  1. MJPG-Streamer

Resources

Step-by-step Walkthrough

Short-and-sweet Version (for advanced users)

Conceptual Overview

  1. Update the Raspberry Pi and install software dependencies
  2. Download the MJPG-Streamer source code
  3. Compile the source code
  4. Capture images from the Webcam and serve them on a Webpage
  5. Set MJPG-Streamer to run at boot

Two Methods: Camera Module or USB Webcam

You can use the Raspberry Pi camera module, or any compatible USB Webcam.  I found that that the USB Webcam method is easier and better quality, but there are instructions for both.  In the walkthrough, I’ll be using a USB Webcam.  The camera module instructions can be found in the appendix.

Step-by-step Walkthrough

Check If the Pi Recognizes the Webcam

Plug in the USB Webcam and run the command:

Look for an entry relating to your Webcam.  This is a good indicator it has been recognized.

picamheadon

Update Pi and Install Dependencies

First, update the Raspberry Pi and then install some of the dependencies of MJPG-Streamer.

An adjustment needs to be made:  the videodev.h  file has been replaced with videodev2.h  but this change needs to be made manually.  To do this, just create a symbolic link.

Download and Compile MJPG-Streamer

Unfortunately, MJPG-Streamer isn’t available via apt-get , so we need to compile it from source.  This may be more complex than setting up a Webcam using Motion, but I found MJPG-Streamer to be less resource-intensive so the additional complexity of setting it up is worth it.

There are a few different ways to install MJPG-Streamer.  The easiest is via subversion, which is what I will walkthrough.  If you want directions for some other methods, look in the appendix at the bottom of the post.

Download via Subversion (svn)

Install subversion (if necessaary).

Then, download a copy of the MJPG-Streamer source code to your home folder and move into the directory when the download is complete:

There are a lot of plugins included with MJPG-Stream, but for this walkthrough, we will just compile three.  If you only plan to use a USB Webcam, you can modify the command below to suit your needs.  Just leave off the items you do not want.

  • input_uvc.so (for USB Webcams) : copies JPGs from a single input (the Webcam in this case) to one or more output plugins.  This is good for streaming larger images at a higher framerate with lower CPU usage.
  • input_file.so (for camera module): similar to the above, but copies them from a directory.
  • output_http.so: streams the files to a Webserver

Alternatively, running make , you can compile everything that comes with MJPG-Streamer.  I chose to just install what was necessary to save on storage space and resources.

Copy MJPG-Streamer to an Alternate Location

You could run MJPG-Streamer right from the folder after it is compiled ( ./mjpg_streamer ), but it might be better to give it a permanent home.

Run the following commands to copy it to a more globally-accessible area:

/usr/local/ is a common place for third-party items or things added to a system by an admin.  But you could put them wherever you like, or just leave it where it is.  You will just need to modify the paths for the rest of the walkthrough.

Export Paths

If you did try to run mjpg_streamer  now, it probably returned an error about search paths.  This is because right now, the system doesn’t know where to find the files.  This is an easy fix.  Just append the following line to ~/.bashrc (assuming you ran the commands above to copy the files to /usr/local/ .  This will make it a permanent change so that you don’t need to do this every time you log in:

Now you can either log out and back in again, but it is easier to run the source  command to apply the change (this has the same effect as logging in):

Now, you can simply call mjpg_streamer  no matter what directory you are in.  We are almost ready to start capturing images.

Using input_uvc.so to Capture Images

Start running mjpg_streamer  with the command:

This command options are as follows:

  • -i : uses input_uvc.so (the USB Webcam) as input
  • -o : output_http.so for the output (sending the images to a Web server
  • -w : the directory, which has the HTML, CSS, and JS files:   /usr/local/www

You can cancel the stream by pressing Ctrl+C.  If you want to experiment with some other options, some are listed below:

  • -b : runs in the background
  • -p : set a default port instead of the default 8080
  • -c : prompts for username:password
  • -y YUYV : enables YUYV format and disables MJPEG mode
  • -f : framerate (in seconds)

If you used the -b  option, you will get your prompt back.  So how do you stop it from running?  After running the command, you will see a line like:

If you want to stop the stream, just run:

which just kills the process ID (PID) of MJPG-Streamer.

View the Webcam Live-stream From a Browser

(while connected to your local network)

Even if you have already set up a Webserver, you can still run this without problem because it is accessed on a different (default) port: 8080 .  So when you navigate to it in a browser, just append :8080 after the IP address:

  • locallyhttp://localhost:8080
  • from another device on the networkhttp://<raspberry_pi's_ip_address>:8080

catcam

Accessing Your Pi Over the Internet

Viewing your Webcam while connected to your local network is cool, but I’ll show you how to access your Webcam from anywhere by going to a URL such as: myDomain.com:8080 .  This stream can be password-protected, but it really isn’t very secure, so be careful if you make this available online.

pioverinternet

Pre-requisites

Once the steps above are complete, you will be able to access your Webcam stream from any browser by navigating to http://myDomainName.org:8080 .  But there is no password yet!  Anyone on the Internet will be able to see your Webcam.

We can tell MJPG-Streamer to use a password with the -c  option.  This password is not very secure (it is only base 64 encoded), but it can offer a first line of defense.   Someone could easily sniff and decode the password, but if you are on a trusted network then it is not as much of a problem.

The command would look like this:

The best way to secure any online resource is with a layered-defense model.   So before you would want this out on the Internet, you might want to be protected using other tools.

Embed the Stream into a Web Page

For simplicity, just make the page in the same directory that is serving up the HTML files ( /usr/local/www/  in this walkthrough).  This makes it easier to make sure the page works.  After you know its working, you can move it somewhere else, but you will just need to adjust the paths.  Enter the following HTML code (adjusting to your setup) and it is ready to go.

If you want, you can also use lighttpd to password-protect your embedded-stream.

Now, just navigate to your page to see it:

  • http://192.168.1.100:8080/catcam.html (on the local network)
  • http://myDomainName.org:8080/catcam.html  (over the Internet)

Run MJPG-Streamer as a Daemon (Background Service)

Until now, we have just been launching MJPG-Streamer on an as-needed basis by running a command with our options.  If you want to make this more permanent and have the Raspberry Pi start the Webcam stream when it boots up, we need to tell it to do so.

There are two scripts below: a very basic one that just runs the command, and an advanced script that will allow you to use the service  command to control it:

Simple Script

A very simple script to do this is below:

Save the script.  Make it executable:

This next command makes sure it is executed during boot:

Advanced Script

Below is a script that should load MJPG-Streamer at boot and allow use of the service  command:

Once saved, run the same commands as above:

Success!

Now your Webcam is available over the Internet and starts up when you turn on your Raspberry Pi.

Appendix:

Download MJPG-Streamer via curl or wget

Finish running the commands from the Via subversion section.

Download MJPG-Streamer via scp

Download MJPG-Streamer on another device and then use scp to copy it over to the Raspberry Pi.

Finish running the commands from the Via subversion section.

Using input_file.so to Capture Images

For this method, you need to set the camera to start recording images using the command raspistill .  You will also need a folder to store the files in; for now, just make it in /tmp :

Then, enable the camera with a command similar to the following:

The options of the command are explained below, so adjust them as you see fit:

  • --nopreview : Do not display a preview window
  • -w and -h : image with and height (in pixels)
  • -q : quality <1-100>
  • -o : write out to a file (named pic.jpg)
  • -tl : time (in ms) before it takes a picture and shuts down and link latest complete image to filename
  • -t : time (9999999ms)
  • -th : set thumbnail parameters (x:y:quality)
  • & : sends the job to the background (not part of raspistill )
  • http://botmayank.wordpress.com mayankjoneja

    Really good job of compiling this article. I’ve used mjpg-streamer and I know how much of a pain it can be to set it up without resources like this post to help you out 😀

    Quick question, haven’t explored it yet, but what’s the easiest way to stream as well as process a webcam stream? Say, using OpenCV?

    Also, capturing stills with mjpg-streamer is as simple as linking a button action to the still image page in the default page hosted when the server is launched right? I haven’t tried it yet so just wanted to know if there’s anything else involved.

    • http://www.jacobsalmela.com Jacob Salmela

      Unfortunately, I am not familiar with OpenCV, so I can’t say much on that topic.

      Yes, as far as I know, you can just link to the static page and you could save it from there.

  • Muhammad Sadiq odho

    I have installed it but there is an error when i try

    mjpg_streamer -i “/usr/local/lib/input_uvc.so” -o “/usr/local/lib/output_http.so -w /usr/local/www”

    error:
    MJPG Streamer Version: svn rev: 3:172 i: Using V4L2 device.: /dev/video0 i: Desired Resolution: 640 x 480 i: Frames Per Second.: 5 i: Format…………: MJPEG Unable to set format: 1196444237 res: 640×480 Init v4L2 failed !! exit fatal i: init_VideoIn failed

    there is no ouput on localhost too.
    What should I do?

    • http://www.jacobsalmela.com Jacob Salmela

      Try launching mjpg_streamer with the additional option: -y YUYV, which enables YUYV format and disables MJPEG mode. The error might be because your Webcam does not support the mjpg format.

  • Muhammad Sadiq odho

    I have faced problem when i want to ./mjpg-streamer.sh start
    faild to start

    I am using
    Bus 001 Device 006: ID 041e:4034 Creative Technology, Ltd Webcam Instant

    mjgp-streamer.log
    ————————–
    MJPG Streamer Version: svn rev: 3:160M
    i: Using V4L2 device.: /dev/video0
    i: Desired Resolution: 640 x 480
    i: Frames Per Second.: 4
    i: Format…………: YUV
    i: JPEG Quality……: 80
    i: The format asked unavailable, so the width 352 height 288
    The input device does not supports YUV mode
    Init v4L2 failed !! exit fatal
    i: init_VideoIn failed
    MJPG Streamer Version: svn rev: 3:160M
    i: Using V4L2 device.: /dev/video0
    i: Desired Resolution: 640 x 480
    i: Frames Per Second.: 4
    i: Format…………: MJPEG
    i: The format asked unavailable, so the width 352 height 288
    The inpout device does not supports MJPEG mode
    You may also try the YUV mode (-yuv option), but it requires a much more CPU power
    Init v4L2 failed !! exit fatal
    i: init_VideoIn failed
    MJPG Streamer Version: svn rev: 3:160M
    i: Using V4L2 device.: /dev/video0
    i: Desired Resolution: 320 x 240
    i: Frames Per Second.: 5
    i: Format…………: MJPEG
    i: The format asked unavailable, so the width 176 height 144
    The inpout device does not supports MJPEG mode
    You may also try the YUV mode (-yuv option), but it requires a much more CPU power
    Init v4L2 failed !! exit fatal
    i: init_VideoIn failed
    MJPG Streamer Version: svn rev: 3:160M
    i: Using V4L2 device.: /dev/video0
    i: Desired Resolution: 320 x 240
    i: Frames Per Second.: 5
    i: Format…………: MJPEG
    i: The format asked unavailable, so the width 176 height 144
    The inpout device does not supports MJPEG mode
    You may also try the YUV mode (-yuv option), but it requires a much more CPU power
    Init v4L2 failed !! exit fatal
    i: init_VideoIn failed

    mjpg-streamer.sh
    —————————–
    #!/bin/bash

    VIDEO_DEV=”/dev/video0″
    FRAME_RATE=”4″
    RESOLUTION=”640×480″
    PORT=”8080″
    YUV=”true”

    MJPG_STREAMER_DIR=”$(dirname $0)”
    MJPG_STREAMER_BIN=”mjpg_streamer”
    LOG_FILE=”${MJPG_STREAMER_DIR}/mjpg-streamer.log”
    RUNNING_CHECK_INTERVAL=”2″ # how often to check to make sure the server is running (in seconds)
    HANGING_CHECK_INTERVAL=”3″ # how often to check to make sure the server is not hanging (in seconds)

    function running() {
    if ps aux | grep ${MJPG_STREAMER_BIN} | grep ${VIDEO_DEV} >/dev/null 2>&1; then
    return 0

    else
    return 1

    fi
    }

    function start() {
    if running; then
    echo “already started”
    return 1
    fi

    export LD_LIBRARY_PATH=”${MJPG_STREAMER_DIR}:.”

    INPUT_OPTIONS=”-r ${RESOLUTION} -d ${VIDEO_DEV} -f ${FRAME_RATE}”
    if [ “${YUV}” == “true” ]; then
    INPUT_OPTIONS+=” -y”
    fi

    OUTPUT_OPTIONS=”-p ${PORT} -w www”

    ${MJPG_STREAMER_DIR}/${MJPG_STREAMER_BIN} -i “input_uvc.so ${INPUT_OPTIONS}” -o “output_http.so ${OUTPUT_OPTIONS}” >> ${LOG_FILE} 2>&1 &

    sleep 1

    if running; then
    if [ “$1″ != “nocheck” ]; then
    check_running & > /dev/null 2>&1 # start the running checking task
    check_hanging & > /dev/null 2>&1 # start the hanging checking task
    fi

    echo “started”
    return 0

    else
    echo “failed to start”
    return 1

    fi
    }

    function stop() {
    if ! running; then
    echo “not running”
    return 1
    fi

    own_pid=$$

    if [ “$1″ != “nocheck” ]; then
    # stop the script running check task
    ps aux | grep $0 | grep start | tr -s ‘ ‘ | cut -d ‘ ‘ -f 2 | grep -v ${own_pid} | xargs -r kill
    sleep 0.5
    fi

    # stop the server
    ps aux | grep ${MJPG_STREAMER_BIN} | grep ${VIDEO_DEV} | tr -s ‘ ‘ | cut -d ‘ ‘ -f 2 | grep -v ${own_pid} | xargs -r kill

    echo “stopped”
    return 0
    }

    function check_running() {
    echo “starting running check task” >> ${LOG_FILE}

    while true; do
    sleep ${RUNNING_CHECK_INTERVAL}

    if ! running; then
    echo “server stopped, starting” >> ${LOG_FILE}
    start nocheck
    fi
    done
    }

    function check_hanging() {
    echo “starting hanging check task” >> ${LOG_FILE}

    while true; do
    sleep ${HANGING_CHECK_INTERVAL}

    # treat the “error grabbing frames” case
    if tail -n2 ${LOG_FILE} | grep -i “error grabbing frames” > /dev/null; then
    echo “server is hanging, killing” >> ${LOG_FILE}
    stop nocheck
    fi
    done
    }

    function help() {
    echo “Usage: $0 [start|stop|restart|status]”
    return 0
    }

    if [ “$1″ == “start” ]; then
    start && exit 0 || exit -1

    elif [ “$1″ == “stop” ]; then
    stop && exit 0 || exit -1

    elif [ “$1″ == “restart” ]; then
    stop && sleep 1
    start && exit 0 || exit -1

    elif [ “$1″ == “status” ]; then
    if running; then
    echo “running”
    exit 0

    else
    echo “stopped”
    exit 1

    fi

    else
    help

    fi

    kindly guide me i have tried a lot.

    • http://www.jacobsalmela.com Jacob Salmela

      Based off of your log file, it looks like your Webcam does not support either format–see the bolded lines:

      i: The format asked unavailable, so the width 352 height 288
      The input device does not supports YUV mode
      Init v4L2 failed !! exit fatal
      i: init_VideoIn failed
      MJPG Streamer Version: svn rev: 3:160M
      i: Using V4L2 device.: /dev/video0
      i: Desired Resolution: 640 x 480
      i: Frames Per Second.: 4
      i: Format…………: MJPEG
      i: The format asked unavailable, so the width 352 height 288
      The inpout device does not supports MJPEG mode
      You may also try the YUV mode (-yuv option), but it requires a much more CPU power

      I also noticed the Instant Webcam is not listed on the list of supported devices.

      If you wanted to be sure you could install a utility to check:
      sudo apt-get install uvcdynctrl
      uvcdynctrl -f

      You will probably need to get a different Webcam.

  • Muhammad Sadiq Odho

    it works fine when i use motion instead of mjpg-streamer. I don’t know what is the video format there. It means webcam works fine. isn’t?

    • http://www.jacobsalmela.com Jacob Salmela

      I don’t believe so. motion uses

      • jpg files
      • ppm format files
      • mpeg video sequences

      I don’t know all the technical differences on the formats, but I still think you need a different Webcam based off your log file. The only other thing I could think to try is to lower the resolution…

  • https://plus.google.com/107912183133133503620 Roland Gallinera

    Nice tutorial, I have been using mjpeg-streamer with Pogoplugs and dockstars using Logitech Quickcam Pros, streaming on the web at 10fps and recording 1 image per second, and have a cron job running every two hours, generating an timelapse AVI file) and was planning on using my Raspberry pi with Pi Cam for my outside of the house security cam. But when I saw the smooth stream from Raspivid, I became dis-illusioned with the streams from mjpeg-streamer as at most I can get from the quickcam 9000 was 10 fps at 960×720 (vs. 1080p from Raspicam). However I can never get both livestreaming and recording to work at the same time (except if I get a PC to record the livestream off the raspberry Pi) so I guess I’m stuck with mjpeg-streamer.

    I’ve seen some tutorials involving a Beaglebone Black with a Logitech C920 that has beautiful recordings, but the C920 is way too expensive vs the Raspicam…

    I was wondering if you know how I can get HD video streams with recordings with Raspberry?

    • http://www.jacobsalmela.com Jacob Salmela

      Sorry, I don’t.

      I tried a few different Webcams while writing this tutorial, along with other pieces of software (like motion) but came out with similar results as you. The Pi might just not have enough power to do HD live streams. I have considered trying the Hummingboard since it seems to be a little bit more powerful.

      If I do find something out, I’ll post it here.

      • https://plus.google.com/107912183133133503620 Roland Gallinera

        I have a Beaglebone Black and a Cubox-i4 Pro and have considered these,as my live streamers, but I found that I still need an expensive webcam for them if I want HD Live streams. Hopefully, there is something out there that uses Raspi cam which is priced reasonably…

        • http://www.jacobsalmela.com Jacob Salmela

          I guess there is a tradeoff for HD or low-cost. Can you get HD using your expensive Webcams with the Pi? Or do you need the extra power as well?

  • patrick

    Thank you for this work you done works very well

    • http://www.jacobsalmela.com Jacob Salmela

      I’m glad it works!

  • Glen Page

    Thanks for the tutorial. I am pretty sure I followed your instructions but below is what I get when I enter
    mjpg_streamer -i “/usr/local/lib/input_uvc.so” -o “/usr/local/lib/output_http.so -w /usr/local/www”

    MJPG Streamer Version: svn rev: 3:172
    i: Using V4L2 device.: /dev/video0
    i: Desired Resolution: 640 x 480
    i: Frames Per Second.: 5
    i: Format…………: MJPEG
    ERROR opening V4L interface: No such file or directory
    Init v4L2 failed !! exit fatal
    i: init_VideoIn failed

    I am using a RaspberryPi B+ and a Pi Camera module. What do I try next?

    TIA, Glen

    • http://www.jacobsalmela.com Jacob Salmela

      Was the camera recognized when you ran lsusb? You could also try sudo apt-cache search v4l and then install whatever seems appropriate. Unfortunately, I do not have the B+ or the camera module so I can’t test. I also found a comment that might give you some more clues.

      • Glen Page

        Here is the output from lsusb
        Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
        Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
        Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
        Bus 001 Device 004: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter
        Bus 001 Device 005: ID 046d:c313 Logitech, Inc. Internet 350 Keyboard

        sudo apt-cache search v41
        returns
        xserver-xorg-video-radeon – X.Org X server — AMD/ATI Radeon display driver

        Will look a the comment you linked in a few minutes – have to do some real work (the stuff they pay me for).

        • http://www.jacobsalmela.com Jacob Salmela

          I haven’t tested this, but maybe these utilities will help?
          sudo apt-get install v4l-utils v4l-conf

  • Anonymous

    For the Pi camera and Pi-Noir you need the UV4L driver.
    http://www.linux-projects.org/modules/sections/index.php?op=viewarticle&artid=14

    • http://www.jacobsalmela.com Jacob Salmela

      Nice, thanks!

  • https://plus.google.com/+EstebanDanielFonsecaMoreno Esteban Daniel Fonseca Moreno

    Can I change the frames perr second in the streamer?, Because is very slow, I think……..

    • http://www.jacobsalmela.com Jacob Salmela

      Yes. Use the -f option in the input_uvc.so section of the command. (i.e. mjpg_streamer -i "/usr/local/lib/input_uvc.so -f 30" -o "/usr/local/lib/output_http.so -w /usr/local/www")

  • http://Wright Matt

    Hey Jacob,

    Thanks for the guide. Question for you, hoping you might be able to help. Currently I have gotten Mjpg-streamer to work using the YUV format. However, Mjpeg format only outputs a white box (i.e. no video on the webpage). I know the camera supports Mjpeg (Logitech HD-3000) and have also tried several other cameras.

    Do you know a way to further troubleshoot this issue, maybe with some sort of logs?

    Thanks for any help as I have been banging my head against the wall for weeks on this issue.

    • http://www.jacobsalmela.com Jacob Salmela

      Yes, the first place I would look are the log files. I don’t have my Pi set up for this anymore so I don’t know where the log file is. Most logs are in /var/logs, so check there. I think you can also see some stuff in /var/log/messages.

  • http://none thomas

    Hey Jacob,

    I tried to follow your tuto but I’ve got some problems testing it.
    I’m using the Pi camera on a raspberry B model, and I followed successfully every steps until the capture images step.
    So here I’m stuck with the below message when I ran “raspistill –nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &”

    mmal: Skipping frame 2 to restart at frame 13
    mmal: Skipping frame 14 to restart at frame 36
    mmal: Skipping frame 37 to restart at frame 58
    mmal: Skipping frame 59 to restart at frame 80
    mmal: Skipping frame 81 to restart at frame 103
    etc..

    I’ve already set up the lighttp server, but I can see the image on it. I’m on 172.16.62.221:8888, which is a port number that I defined by myself.
    And my camera led is on during the all time I get the mmal message.

    I might need to send the captured images on output_http.so but I don’t know how to do it.
    Thanks for any help.

  • Richard van Eckendonk

    Somehow my mjpg_streamer will not start as a service/deamon
    I did the export and update-rc.d command.
    I guess it has to do with the export of the path somehow what is not recognized in the environment of the deamon.

    • http://www.jacobsalmela.com Jacob Salmela

      Did you try logging out and back in or rebooting?

  • krunal solanki

    After completion, M getting message like boundarydonotcross content-type:image/jpeg content-Length 0 X-timestamp 296.098931….
    It repeats with different time stamps values..
    Please help me.. Reply me on my mail id Solanki.krunal7@gmail.com if possible.

    • jacobsalmela

      I never ran into that when working with my setup. Were you able to try any of the other formats? Or perhaps reducing the framerate. Or maybe your images are not being capture as it says Content-Length 0? Just a guess. Let me know what happens when you use the different options.

      • krunal solanki

        Thanks for reply, I tried with YUV format using -y in terminal window but getting the same results. My camera works well. I can have pictures through it. what main problem is I cant start mjpg streamer.

        Should I cross compile it ?
        please let me know, How?

        What to do if mjpg streamer is not taking pictures as content lengh is zero.?
        I tried with fps 1 too as shown below.

        pi@raspberrypi ~/mjpg-streamer-r63 $ mjpg_streamer -i “/usr/local/lib/input_uvc.so -f 1″ -o “/usr/local/lib/output_http.so -w /usr/local/www”
        MJPG Streamer Version.: 2.0
        i: Using V4L2 device.: /dev/video0
        i: Desired Resolution: 640 x 480
        i: Frames Per Second.: 1
        i: Format…………: MJPEG
        o: www-folder-path…: /usr/local/www/
        o: HTTP TCP port…..: 8080
        o: username:password.: disabled
        o: commands……….: enabled

        • jacobsalmela

          What happens when you start the streamer and then navigate to the page? http://localhost:8080 or http://IP_of_Pi:8080?

          Also, are you sure your Webcam is being detected? Try running lsusb to see if it is listed.

          Also, make sure you have all the dependencies installed: libjpeg8-dev imagemagick libv4l-dev.

          Finally, just try to restart and reboot everything.

          • krunal solanki

            I installed all required libraries, I rebooted many times but still getting the same error.
            Please help me what to do?
            M streaming through the http://localhost:8080/?action=stream.

          • jacobsalmela

            You did not post the results of the lsusb command. Does the Webpage show up but you just can’t get images from the Webcam?

          • krunal solanki

            Yes exactly, lsusb command shows the camera, using different tools without mjpg-streamer, I can take pictures too. But I can’t have live streaming over Web page.

          • jacobsalmela

            Does the actual Webpage load, though? If the page loads, but you just can’t see a steam, then it is probably an issue with the camera, or a setting pertaining to it.

          • krunal solanki

            Yes, actual Web page loads, I am using Microsoft lifecam vx800 camera, I can have pictures too on my pi board using this same camera, but instead of live streaming, I am getting this content length, content type message.

          • jacobsalmela

            My guess is that your camera is not supported, or does not work with any of the formats. mjpeg_streamer is running fine since you can get to the Webpage, so it seems to me that your Webcam is the problem.

          • krunal solanki

            It’s Ok, but if camera have problem then I wouldn’t have images on my pi board. Don’t know what’s going wrong.

          • http://www.conoscenzacollettiva.it/ ConoscenzaCollettiva

            Send this command on a terminal, maybe the webcam is set to operate in another mode.

            v4l2-ctl –list-formats

          • jacobsalmela

            That’s what I meant: the Webcam works, but just not in mjpg_streamer. Try ConoscenzaCollettiva’s suggestion.

  • Angel

    Hello Jacob,

    Glad you had time to share your time and knowledge. I follow your tutorial but when running the command

    svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/ mjpg-streamer

    I get the following error

    svn: E170000: URL ‘https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/mjpg-streamer’ doesn’t exist

    So I decide to download the source code directly from

    http://sourceforge.net/projects/mjpg-streamer/files/mjpg-streamer/Sourcecode/

    Could you confirm if this is the right source code? or better if you have a update download link it will be appreciated.

    • jacobsalmela

      It has been a while since making this article. I think the URL you have works, but I also found this one:

      http://sourceforge.net/code-snapshots/svn/m/mj/mjpg-streamer/code/mjpg-streamer-code-182.zip

      • Angel

        Hello Jacob,

        Thanks so much for the help. I was able to complete the entire tutorial without any issues.

        One quick question. I will like to make my Raspberry pi a stand alone streaming device but I need a way to have internet on the Go! Is there a way to use a GSM Usb modem to use a cell phone sim card and have internet on the road?

        • jacobsalmela

          I have never done this myself so I cannot say for certain, but I will try point you in the right direction.

          Since a USB modem is basically just another network interface, you could probably modify /etc/network/interfaces to get a network address. With the setup in this tutorial, everything is pretty automatic since you are just plugging the Pi into the router, so you never really had to mess around with those settings.

          Hope this helps. Good luck!

    • giorgiots

      the “svn: E170000″ is due to a little sintax error in the short tutorial:

      svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/mjpg-streamer
      it should be:
      svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/ mjpg-streamer

      it lacks of a space between the svn repository and the last dir “mjpg-streamer”

      • jacobsalmela

        Thanks. I fixed the code on the page. Hopefully, you won’t get that error now.

        • giorgiots

          Thanks to you for the this tutorial :)

  • Anthony ODonnell

    Got this working, thank you. But how would I include this on my main webpage served by my raspberry pi? It is on 192.168.0.6. camera works on 192.168.0.6:8080. but I want to embed the stream on my main page doesn’t do it, any idea anyone?

    • jacobsalmela

      I guess you could try putting the full URL in the img src. My Pi is being used for some other projects now, so I can’t test it.

  • Abhijeet Ramgir

    Hey, Great tutorial!

    I used the code which makes the streamer start at boot and i can use the service commands
    But it webcam dosent start at boot, i get this error on the boot screen:
    [+] Strating mjpg_streamer
    /etc/init/d/livestream.sh: 20: /etc/init.d/livestream.sh: mjpg_streamer: not found
    [+] mjpg_streamer started

    But the service commands work

    Could you please help me out

    Thanks,
    Abhijeet.

    • jacobsalmela

      It’s saying mjpg_streamer is not found? Try putting the full path name in and see if that fixes it.

      • Abhijeet Ramgir

        Hey Thanks for the reply!

        Could you please tell me the full path name and where to put it in the code? Line 20 is it?

        Thanks again!
        Abhijeet.

        • jacobsalmela

          If you followed the tutorial exactly, it should be in /usr/local/bin/.

          Try that full path on every line that begins with mjpg_streamer.

          • Abhijeet Ramgir

            Hey,

            Im really sorry, i didnt understand your post, could you please explain a bit in detail?

            Thanks
            Abhijeet

          • jacobsalmela

            Whatever line of code begins with mjpg_streamer need to be changed to /usr/local/bin/mjpg_streamer.

          • Abhijeet Ramgir

            Hey,

            Thanks a lot, its working!

            Thanks again!
            Abhijeet.

          • http://www.jacobsalmela.com Jacob Salmela

            Great. I am glad it works. I will update the code on the page so others do not run into the same issue.

          • Abhijeet Ramgir

            Hey,

            Could you tell me one last thing? I tried changing the port unsuccessfully, could you just give me the entire command of the mjpeg streamer including text to change the port, i would really appreciate it.

            Thanks!
            Abhijeet

  • Abhijeet Ramgir

    I used this tutorial a month back and it worked great, but now i used the tutorial to start at boot, the webcam starts but i can’t see the stream on the mjpeg streamer website

    Plz help!
    Abhijeet

    PS: when i ran the command to run the mjpeg streamer it works, only when i set it up to start at boot, it doesn’t

    Thanks!

  • Abhijeet Ramgir

    Hey,

    Please could you update the tutorial, I have tried using the tutorial a million times! and its just not working.

    Thanks
    Abhijeet.

    • jacobsalmela

      It seems to work for most people, but it is possible the guide is outdated. I am going to re-visit all my tutorials once my Raspberry Pi 2 arrives. Unfortunately, blogging is just a side gig after work.

      • Abhijeet Ramgir

        When i used the tutorial without running sudo apt-get upgrade, it worked!!

        When i used that command the webcam streaming didn’t work

        • jacobsalmela

          Nice! I suppose updating isn’t always the best thing to do. I’m glad you figured it out!

      • Insertfake Namehere

        I will buy you a couple of them if you can help me get this running! I am pulling the hair out of my bald head!…BUT, great tutorial. I really appreciate your time in making it. I am on the right path, just lost in the weeds.

    • http://www.jacobsalmela.com Jacob Salmela

      It seems to work for most people, but it is possible the guide is outdated. I am going to re-visit all my tutorials once my Raspberry Pi 2 arrives. Unfortunately, blogging is just a side gig after work.

  • Matteo Lioi

    ei jacob i have a problem with microsoft lifecam cinema… start the web site but there aren’t the stream and image… the led on cam is on when start the command /usr/local/bin/mjpg_streamer -i “/usr/local/lib/input_uvc.so” -o “/usr/local/lib/output_http.so -w /usr/local/www” but no stream…

    thank in advance

    • jacobsalmela

      Does your Webcam show images without using mjpg_streamer? That would be the first step: verify the Webcam works and is supported. If it does, than there is probably some configuration with mjpg_streamer that needs to be adjusted.

      • Matteo Lioi

        yes the webcam works

      • Matteo Lioi

        could you help me?

        • jacobsalmela

          I need more information like log files and whatnot.

  • Matteo Lioi

    i’ve installed all the package but when i do to the webpage i see the text without the stream .. my cam is a microsoft livecam cinema. why this?

  • http://kit.im/ Kit F

    I’m getting this error:
    “404: Not Found!
    Could not open file”
    When I try to go to http://:8080

    I’m like 99% sure I copied everything correctly.

    • jacobsalmela

      A 404 means the file or page doesn’t exist. Were you able to start mjpg_streamer OK?

  • antony

    HI,

    I am having the same logitech camera as the picture. but cannot make it work in mjpg

    v4l2-ctl –list-formats

    ioctl: VIDIOC_ENUM_FMT

    Index : 0
    Type : Video Capture
    Pixel Format: ‘YUYV’
    Name : YUV 4:2:2 (YUYV)

    Index : 1
    Type : Video Capture
    Pixel Format: ‘MJPG’ (compressed)
    Name : MJPEG

    does anyone have a clue?

    • jacobsalmela

      are you using the -y YUYV option?

  • ahmed kelfaoui

    i have problem

    • jacobsalmela

      It seems to be an error relating to the driver for the camera concerning the pan/tilt functionality. You should see if there is another driver to use…

      http://stackoverflow.com/questions/13437244/mjpg-streaming-with-a-raspberry-pi-and-a-webcam

      • ahmed kelfaoui

        it is oky now thnx i just add -y YUYV

        • jacobsalmela

          Great! Glad it works now.

          • keroberos324

            I’m having the same issue, but when I try to add -y YUYV it reports back the -y is an unrecognized option. Any suggestions?

          • jacobsalmela

            The syntax is a little weird. If I remember, I think it needs to be inside the double quotes of the -i section, so something like this:

            /usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so -y YUYV" -o "/usr/local/lib/output_http.so"...and so on...

          • keroberos324

            Got it, thanks!

  • Chris Johnstone

    So it looks like there’s a patch available to update mjpg-streamer. I was following this tutorial but always came up with a blank screen (no image) until I found the patch.

    Immediately before the

    “make mjpg_streamer input_file.so input_uvc.so output_http.so”

    command, I manually patched mjpg-streamer using this:http://www.raspberrypi.org/forums/viewtopic.php?t=97983 and now I have it working.

    • jacobsalmela

      Just to verify, did you follow the instructions from mirak123? If so, I will add them to the walkthrough.

      Download mjpg_streamer
      svn checkout svn://svn.code.sf.net/p/mjpg-streamer/code/ mjpg-streamer-code
      Change directories to the folder
      cd mjpg-streamer-code/mjpg-streamer
      Create a patch file
      nano input_uvc_patch
      Paste the following code into the file
      --- plugins/input_uvc/input_uvc.c (revision 174)+++ plugins/input_uvc/input_uvc.c (working copy)
      @@ -405,9 +405,13 @@
      if(pcontext->videoIn->formatIn == V4L2_PIX_FMT_YUYV) {
      DBG("compressing frame from input: %dn", (int)pcontext->id);
      pglobal->in[pcontext->id].size = compress_yuyv_to_jpeg(pcontext->videoIn, pglobal->in[pcontext->id].buf, pcontext->videoIn->framesizeIn, gquality);
      + /* copy this frame's timestamp to user space */
      + pglobal->in[pcontext->id].timestamp = pcontext->videoIn->buf.timestamp;
      } else {
      DBG("copying frame from input: %dn", (int)pcontext->id);
      - pglobal->in[pcontext->id].size = memcpy_picture(pglobal->in[pcontext->id].buf, pcontext->videoIn->tmpbuffer, pcontext->videoIn->buf.bytesused);
      + pglobal->in[pcontext->id].size = memcpy_picture(pglobal->in[pcontext->id].buf, pcontext->videoIn->tmpbuffer, pcontext->videoIn->tmpbytesused);
      + /* copy this frame's timestamp to user space */
      + pglobal->in[pcontext->id].timestamp = pcontext->videoIn->tmptimestamp;
      }

      #if 0
      @@ -418,8 +422,6 @@
      prev_size = global->size;
      #endif

      - /* copy this frame's timestamp to user space */
      - pglobal->in[pcontext->id].timestamp = pcontext->videoIn->buf.timestamp;

      /* signal fresh_frame */
      pthread_cond_broadcast(&pglobal->in[pcontext->id].db_update);
      Index: plugins/input_uvc/v4l2uvc.c
      ===================================================================
      --- plugins/input_uvc/v4l2uvc.c (revision 174)
      +++ plugins/input_uvc/v4l2uvc.c (working copy)
      @@ -450,6 +450,8 @@
      */

      memcpy(vd->tmpbuffer, vd->mem[vd->buf.index], vd->buf.bytesused);
      + vd->tmpbytesused = vd->buf.bytesused;
      + vd->tmptimestamp = vd->buf.timestamp;

      if(debug)
      fprintf(stderr, "bytes in used %d n", vd->buf.bytesused);
      Index: plugins/input_uvc/v4l2uvc.h
      ===================================================================
      --- plugins/input_uvc/v4l2uvc.h (revision 174)
      +++ plugins/input_uvc/v4l2uvc.h (working copy)
      @@ -28,6 +28,7 @@

      #include
      +#include
      #include
      #include
      #include
      @@ -105,6 +106,8 @@
      int framecount;
      int recordstart;
      int recordtime;
      + uint32_t tmpbytesused;
      + struct timeval tmptimestamp;
      };

      /* context of each camera thread */
      Apply the patch
      patch -p0 < input_uvc_patch
      Compile the patch
      make USE_LIBV4L2=true clean all
      Install the patch
      sudo make DESTDIR=/usr/local install

      • Chris Johnstone

        I followed those instructions up until:

        $ patch -p0 < input_uvc_patch

        After that I returned to your guide and it worked from there.

        • jacobsalmela

          Interesting. Thanks. I’ll add that to the original post at some point!

  • ltakemoto

    i have a need to display my web content locally, as take a webcam, run it into Pi, then hook into hdmi to monitor. thats it. doing demos to classrooms small groups of kids clustered around widescreenTV. whats the best way to do this?

    • jacobsalmela

      Couldn’t you just boot up into the GUI and display it that way? I’m sure there is some app that lets you view the Webcam that way.

  • Soham Shah

    hi i m using raspberry pi b+ and logitech c170 camera

    i followed your tutorial and i tried this command

    /usr/local/bin/mjpg_streamer -i “/usr/local/lib/input_uvc.so -d /dev/video0 -n” -o “/usr/local/lib/output_http.so -w /usr/local/www”

    ( coz i was getting error in following command

    /usr/local/bin/mjpg_streamer -i “/usr/local/lib/input_uvc.so” -o “/usr/local/lib/output_http.so -w /usr/local/www”)

    it shows this

    MJPG Streamer Version: svn rev: 3:172

    i: Using V4L2 device.: /dev/video0

    i: Desired Resolution: 640 x 480

    i: Frames Per Second.: 5

    i: Format…………: MJPEG

    o: www-folder-path…: /usr/local/www/

    o: HTTP TCP port…..: 8080

    o: username:password.: disabled

    o: commands……….: enabled

    and there is no error but when i open the page it is not showing any image please help me

    • jacobsalmela

      The simplest thing to try is switching formats using the -y YUYV option. Alternatively, there has been success installing a patch for mjpg_streamer. I will add these instructions to the how-to when I make time for it,

      • Soham Shah

        thanx for reply

        i tried this /usr/local/bin/mjpg_streamer -i “/usr/local/lib/input_uvc.so -y YUYV” -o “/usr/local/lib/output_http.so -w /usr/local/www”

        and it shows

        MJPG Streamer Version: svn rev: 3:172
        i: Using V4L2 device.: /dev/video0
        i: Desired Resolution: 640 x 480
        i: Frames Per Second.: 5
        i: Format…………: YUV
        i: JPEG Quality……: 80
        ERROR opening V4L interface: No such file or directory
        Init v4L2 failed !! exit fatal
        i: init_VideoIn failed

        so pls help me to solve this
        also i followed your instruction for installing a patch and

        when i tried

        svn checkout svn://svn.code.sf.net/p/mjpg-streamer/code/ mjpg-streamer-code

        it shows

        Checked out revision 182.

        is this some kind of error ?

        • Soham Shah

          hey thanx well i uninstalled all the things and then again followed your steps including installing a patch and
          its working welll
          thank you so much

          • jacobsalmela

            Great! Glad it worked!

  • mordechai malstrom

    Thanx a lot for the great howto! Works fine with RPi2 and surveillance station 7.
    Greetz,
    MM

    • jacobsalmela

      Glad you found it useful!

  • Marco vd V.

    Hello,
    I have similar problem by getting it work.
    I have followed the tut. and also set the -y YUYV option, but still get following error:

    I’m using the Logitech C270 webcam which is also detected by the USB port.

    Some one an Idea?

    Thanks in advance,

    • jacobsalmela

      You can try seeing what formats are supported with:

      v4l2-ctl --list-formats

      You will probably have luck installing the patch as described here. I mean to add it into the original post at some point, but just haven’t made time to do so.

    • H.M

      Hi,

      I also had this issue but fixed,

      The solution is:

      /usr/local/bin/mjpg_streamer -i “/usr/local/lib/input_uvc.so -d /dev/video0 -r 100×100 -f 100 -n -y” -o “/usr/local/lib/output_http.so -w /usr/local/www”

      ” Add = -d /dev/video0 ”

      P.S; Jacob Salmela can you help me “look my post”.

  • Edih Pere Elijah

    hi, Nice Post, I got mine up by following this post and adding the yuv option . I would like to be able to create a page in my apache folder /var/www and be able to access the feed from there. any ideas on how to ?

    • jacobsalmela

      You can embed as a stream to a Webpage, as described. You might also be able to use mod_redirect to redirect to the main mjpg_streamer page.

  • Abhijeet Ramgir

    Hey ! ran through the tutorial, installed the patch but stil getting a blank screen??

    Here’s the log I get when i run the command to start capturing images::

    pi@raspberrypi ~/mjpg-streamer-code/mjpg-streamer $ /usr/local/bin/mjpg_streamer -i “/usr/local/lib/input_uvc.so” -o “/usr/local/lib/output_http.so -w /usr/local/www”

    MJPG Streamer Version: svn rev: 3:172

    i: Using V4L2 device.: /dev/video0

    i: Desired Resolution: 640 x 480

    i: Frames Per Second.: 5

    i: Format…………: MJPEG

    Adding control for Pan (relative)

    UVCIOC_CTRL_ADD – Error: Inappropriate ioctl for device

    Adding control for Tilt (relative)

    UVCIOC_CTRL_ADD – Error: Inappropriate ioctl for device

    Adding control for Pan Reset

    UVCIOC_CTRL_ADD – Error: Inappropriate ioctl for device

    Adding control for Tilt Reset

    UVCIOC_CTRL_ADD – Error: Inappropriate ioctl for device

    Adding control for Pan/tilt Reset

    UVCIOC_CTRL_ADD – Error: Inappropriate ioctl for device

    Adding control for Focus (absolute)

    UVCIOC_CTRL_ADD – Error: Inappropriate ioctl for device

    mapping control for Pan (relative)

    UVCIOC_CTRL_MAP – Error: Inappropriate ioctl for device

    mapping control for Tilt (relative)

    UVCIOC_CTRL_MAP – Error: Inappropriate ioctl for device

    mapping control for Pan Reset

    UVCIOC_CTRL_MAP – Error: Inappropriate ioctl for device

    mapping control for Tilt Reset

    UVCIOC_CTRL_MAP – Error: Inappropriate ioctl for device

    mapping control for Pan/tilt Reset

    UVCIOC_CTRL_MAP – Error: Inappropriate ioctl for device

    mapping control for Focus (absolute)

    UVCIOC_CTRL_MAP – Error: Inappropriate ioctl for device

    mapping control for LED1 Mode

    UVCIOC_CTRL_MAP – Error: Inappropriate ioctl for device

    mapping control for LED1 Frequency

    UVCIOC_CTRL_MAP – Error: Inappropriate ioctl for device

    mapping control for Disable video processing

    UVCIOC_CTRL_MAP – Error: Inappropriate ioctl for device

    mapping control for Raw bits per pixel

    UVCIOC_CTRL_MAP – Error: Inappropriate ioctl for device

    o: www-folder-path…: /usr/local/www/

    o: HTTP TCP port…..: 8080

    o: username:password.: disabled

    o: commands……….: enabled

    Any help would be appreciated!!

    Thanks

    • jacobsalmela

      Follow these instructions to install a patch that should fix this.

      • Abhijeet Ramgir

        Thanks a lot! got it working!

        I have kept coming back to this tutorial for the last two years, thanks a lot!

        • jacobsalmela

          No problem. I have been meaning to add those instructions into my post–I just haven’t made time to do it…