How to build and run MJPG-Streamer on the Raspberry Pi

Posted by
on under

It's been a while since I wrote the article on streaming video from the Raspberry Pi using MJPG-Streamer. Since I published that article I have received several comments and questions regarding issues building MJPG-Streamer, so in this short post I'm giving you revised build instructions.

1. Install build dependencies

The following command installs the three libraries that MJPG-Streamer uses:

$ sudo apt-get install libjpeg8-dev imagemagick libv4l-dev

2. Add missing videodev.h

The videodev.h header file that MJPG-Streamer needs has been replaced with a videodev2.h. To make MJPG-Streamer happy you have to create a symbolic link:

$ sudo ln -s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h

3. Download MJPG-Streamer

The source code for MJPG-Streamer is available at sourceforge.net, but it is tricky to find the direct download link:

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

Note that sometimes the link above fails to work. If that is the case, you can also download from your web browser by opening this page: http://sourceforge.net/p/mjpg-streamer/code/HEAD/tarball.

4. Unzip the MJPG-Streamer source code

The source code download is a compressed zip file. Put the file in your home directory (or a temporary folder, if you prefer) and run the following to extract the files:

$ unzip mjpg-streamer-code-182.zip

5. Build MJPG-Streamer

MJPG-Streamer comes with several plugins, but only a couple of them are needed to stream video according to the method I explained in my previous article. The command below only builds what's needed:

$ cd mjpg-streamer-code-182/mjpg-streamer
$ make mjpg_streamer input_file.so output_http.so

6. Install MJPG-Streamer

I did not discuss installation in the previous article, and that confused many readers. The following commands copy all the needed files into system directories:

$ sudo cp mjpg_streamer /usr/local/bin
$ sudo cp output_http.so input_file.so /usr/local/lib/
$ sudo cp -R www /usr/local/www

7. Start the camera

We are almost there. Now it is time to start the camera module:

$ mkdir /tmp/stream
$ raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &

Of course, you can use different options to raspistill if you like.

8. Start MJPG-Streamer

The camera is now writing images, so all that is left is to start MJPG-Streamer:

LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /tmp/stream -n pic.jpg" -o "output_http.so -w /usr/local/www"

9. Watch the Stream!

Now you can connect with your web browser and watch the stream live. If you want to watch from within the same Raspberry Pi you can enter http://localhost:8080 in the browser's address bar. If you want to watch from another computer in your network use http://<IP-address>:8080.

10. Cleanup

After you verify that everything is working you can remove the source code:

$ cd ../../
$ rm -rf mjpg-streamer-182

Conclusion

I hope these instructions clarify all the aspects of setting up the streaming server on the Pi. If you still have questions let me know below!

Miguel

Become a Patron!

Hello, and thank you for visiting my blog! If you enjoyed this article, please consider supporting my work on this blog on Patreon!

338 comments
  • #76 Miguel Grinberg said

    @Joseph: I can't say for sure, but often when the Pi starts doing weird things it is a power problem, you may want to try a better power supply.

  • #77 Fabian said

    Thanks for this tutorial, works great. I get some Stream and added an start-script to the rc.local. Now booting the pi means, getting a stream via http-> great!
    But....if I run this mjpg-raspistill team for a while, the stream stops and I get an "could not open file for reading: No such file or directory" error.
    Has anyone also tried to stream for a longer time period (hours, days?)
    Best regards,
    Fabian

  • #78 jens said

    Hello,

    I have one question: can you please give me te explanation how i can use this for a USB camera because I searched for weeks, but I never found an up to date version.

    Please help me!!!!!!!!!!

    THANKS

  • #79 Miguel Grinberg said

    @jens: you can't. The "raspistill" utility in which this method is based works with the RPi camera only, as far as I know.

  • #80 Hugo Coolens said

    Because I got error messages like these:
    mmal: Skipping frame 31 to restart at frame 36
    I changed your one liner to this:
    raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &> /dev/null &

    Thanks for the tutorial
    hugo

  • #81 chris said

    Worked like a champ! The only thing I would suggest is moving the "stream" folder to a permanent location that will exist across reboots, with out that folder existing it throws my Pi into a fit when the camera app is launched. Thanks for the writeup!

  • #82 Jack Kirk said

    I'm afraid that this is all I get when I enter raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 & :

    mmal: Skipping frame 1746 to restart at frame 1751

    I get this message for not all but many frames. Can someone tell me what's wrong please?

  • #83 Yacked2 said

    Hello, thank you for this tutorial, but when I run this command:
    raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &

    I get errors:
    mmal: Skipping frame XX to restart at frame YY

    Do you have any idea how to fix that ?

  • #84 Miguel Grinberg said

    @Yacked2: I don't know, there are others that are also getting this. Are you using a reliable power source?

  • #85 Miguel Javier said

    Hello Miguel (from another Miguel):

    First and foremost thank you for the guide(tutorial) it has been very helpful. I used it to add a camera to my garage door opener. I do however have an issue where my mjpg-streamer stops working after a a or so. any ideas about this? This is what I have:
    raspistill --nopreview -w 640 -h 480 -q 50 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &

    And:

    LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /tmp/stream -n pic.jpg" -o "output_http.so -w /usr/local/www" &

    I have created a couple of scripts to run it at boot and and works well except that little issue.

    Any help will be highly appreciated.

  • #86 Miguel Grinberg said

    @Miguel: If you plan on leaving the system running long term you have to wrap raspistill in a script that reruns it when it exists, the -t 9999999 argument keeps it running just for a little less than 3 hours.

  • #87 Miguel Javier said

    Thanks for the reply. Yes, the plan is to leave it permanently broadcasting. I'm still new with linux, would you have an example for such a script? Thanks!

  • #88 Colin Jones said

    Wonderful blog, very useful. I marvel at the efforts of such unselfish experts to contribute to the open source community. Much appreciated Miguel :-)

  • #89 Miguel Grinberg said

  • #90 seif said

    thank you for your article,it was very useful for me,I want to ask can I use usb web cam instead of the camera module?what different steps shall I do? cosidiring that i want to decrease the delay as possible as I will use it in an ROV thank you in advance

  • #91 Miguel Grinberg said

    @seif: To use another camera you will need a tool that replaces raspistill, which only works with the Pi Camera. If you have a tool that can take pictures with your web cam and write them as a jpg on disk, then you can use mjpg_streamer to stream them.

  • #92 Anthony said

    Can I use cgi with this. I dont't see a way to add controls to the page for any thing outside of camera options. I need to take other input on my page too.

    For example i set this at my door and want to store the image when the door bell pushes. or i want to turn on a light in a room i'm viewing. I don't see a way to get parameters from forms or buttons etc,

  • #93 Miguel Grinberg said

    @Anthony: there is nothing magical about the web site that mjpg_streamer uses. The picture is just a motion jpeg stream, which most browsers can display natively. You don't have to use the server from mjpg_stream, you can build your own web app and put a reverse proxy server (nginx for example) that talks to the mjpg_streamer server for the mjpg stream and to your app for everything else.

  • #94 james wolf said

    hey I am trying your install instructiond on Raspbian (on the RPi) and I hit an error :
    "E: Unable to locate package libv41-dev"
    I tried this to see if there was anything close"
    "apt-cache search libv41"
    but it comes up empty, I am too new to linux to see what I am missing, can you help point me in the right direction?

  • #95 Miguel Grinberg said

    @James: do you have a recent version of Raspbian?

  • #96 jdlenton said

    HI Miguel
    i have the same problem when trying to install both libjpeg8-dev and libv4l-dev I get "E: Unable to locate package libv41-dev""
    Im new to Linux too thanks to Santa : ) but I know enough to say my pi is running a clean os and the only things i've done to is run apt-get update and apt-get upgrade. So I know I'm fully up to date.
    thanks in advance for your advice.

    James

  • #97 jdlenton said

    Hi Please diregard my last comment it was my miss reading of the font that was the reason the install would work Dyslexia and this type face dont seem to mix very well.

    kind regards

    J

  • #98 Stephan said

    Hello Miguel, thanks for the tutorial, it works fine. But the link to the zip file doesn't work. Maybe you have to update the link..

  • #99 Kris said

    Hi Miguel i have followed your instructions and works perfectly for me!

    I'm planning to set 2 cameras with my RPI. Can "mjpg streamer" stream 2 cameras? like video 1 with PiCam and video 2 with a USB camera? Been searching and i can't find a combination of USB and PiCam. Hope you can help me

  • #100 Miguel Grinberg said

    @Kris: You can probably achieve this by running two instances of mjpg_streamer listening on different ports. Now you have two web servers streaming images, all that is left is to add a front facing web server such as nginx to consolidate the two into one. Good luck!

Leave a Comment