RaspberryPi: Fast mjpg-streaming for robotics

Because the Raspberry Pi webcam server presented in my last article is to slow for most applications regarding robotics, I want to go one step further in this article using mjpg-streaming.

For this article I use a Raspberry Pi model B connected to a Logitech c170 webcam (with an usb hub as external power supply). There is no additional webserver like apache necessary.

1. First of all make sure, that your webcam is listed correctly.

lsusb

2. To start you’ve got to install the following packages:

apt-get install libv4l-dev
apt-get install libjpeg8-dev
apt-get install subversion
apt-get install imagemagick

libv4l-dev and libjpeg8-dev are required dependencies. Subversion and ImageMagick will be needed to get and to compile the code.

3. Now you are ready to download and to compile the code you will need for mjpg streaming.

svn co https://mjpg-streamer.svn.sourceforge.net/svnroot/mjpg-streamer mjpg-streamer
cd mjpg-streamer/mjpg-streamer
make USE_LIBV4L2=true clean all
make DESTDIR=/usr install

4. You are now ready to start the mjpg streamer. Using the following line you start a mjpg stream at port 8090 with a resoultion of 320×240 px and 10 frames per second.

mjpg_streamer -i "/usr/lib/input_uvc.so -d /dev/video0  -r 320x240 -f 10" -o "/usr/lib/output_http.so -p 8090 -w /var/www/mjpg_streamer"

Input modifiers of mjpg_streamer are:
-d: device
-r: resolution
-f: frame rate
-y specifies YUYV format, rather than MJPEG format if MJPEG ist not supported by your webcam.

Output modifiers are:
-p: port
-w: web serving directory.
See the documentation for more details.

5. To display the image stream just add this line to a html-file. You can replace “raspberrypi” with the ip adress of your Raspberry PI.

img src="http://raspberrypi:8090/?action=stream" width="752"
If this article was useful to you, help me and share!

    14 thoughts on “RaspberryPi: Fast mjpg-streaming for robotics

      1. Sebastian Post author

        It should probably work without external power supply. When I testet it I just had a charger of my cellphone, that’s why I used the USB hub as additional supply.
        Regards,
        Sebastian

        Reply
    1. Pingback: Raspberry Pi Roboter mit Echtzeit-Webcam via mjpg-streamer | Robotik, Technik und andere Spielerein | Robosphere

    2. skidoobond

      How can I tell if mjpg-streamer is streaming from the webcam? I followed the above and nothing is shown on my webpage. I know the camera works as motion takes jpgs from the camera.

      Reply
      1. Sebastian Post author

        when you startet the mjpg server successfully, the terminal should show something like:
        o: www-folder-path…: /var/www/mjpeg_streamer
        o: HTTP TCP PORT… 8090

        otherwise it will show you an error. Did you make sure that you replaced ‘raspberrypi’ with the right IP adress?

        Regards,
        Sebastian

        Reply
    3. Thomas Eldredge

      Wow, thanks a lot for this post. I’d found info on ‘motion’ and ‘gstreamer’ but were far too slow for my needs. This method gives me great results for framerate and quality.
      Thanks again!

      Reply
    4. Pingback: How to Make pcDuino as a Video Surveillance Server: mjpg-streamer | pcDuino: mini PC+Arduino

    5. Gary

      What I’d like to do:
      When my wife pulls in to the carport I like to go great her and help her bring things in.
      I can hear her pull in but can’t distinguish her car from one pulling into the adjoining apartments.
      I want to browse to a web page (raspberry pi server) or refresh an open web page to get a single photo.
      I do not want to run a constantly running image, just a single photo when I refresh.
      Can I set up mjpg_streamer to do that or do you recommend a different approach?

      Reply
    6. Gary

      I might also mention that I have “motion” running but it dumps a lot of photos in my /tmp/motion folder and I have to go in there and delete them ever so often (not the mention all the extra writes to my SD card). Maybe motion could be configured to just deliver a single photo without streaming and without motion detection.

      Reply
    7. Jeffrey

      I followed all the step, but it won’t install.
      When i type: make DESTDIR=/usr install
      It says:
      install –mode=755 mjpg-streamer /usr.bin
      install: cannot creat regular file `/usr/bin/mjpg-streamer` : Permission denied
      make: *** [install] Error 1
      How can I fix this?

      Reply
    8. saurabh

      Hi amazing work..
      any idea why when I open the stream in chrome or vlc the lag in the video and frame rate starts to drop within few minutes ? When I reopen the stream in the browser again it starts off fast and slows down again.

      Please help

      Reply

    Leave a Reply

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


    3 + = seven

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>