2013-10-01T05:45:43Z

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

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

335 comments

  • #201 tibb said 2014-12-28T20:28:15Z

    Hello,

    is there any easy way to protect the page with user/password? Also how to set different port for the web server than 8080? Thank You.

  • #202 Miguel Grinberg said 2014-12-29T06:25:12Z

    @tibb: You can add a "-c username:password" option to the http output plugin of mjpg-streamer (right before the -w, for example).

  • #203 Kennet F said 2015-01-02T12:16:46Z

    Hi,

    I'am still having trouble with an autostart of the camera stream. I have written the commands in the rc.local, but the stream doesn't start. When I write them manually it works just fine. I'm new to linux, so maybe I'm missing something when adding them to the rc.local file.

    /Kennet

  • #204 jem mann said 2015-01-06T16:02:24Z

    I'm very new to Linux It's probably a stupid question but do I have to run lines 8 and 9 every time I want to stream from the camera to the net? Is there a way to do it graphically in the GUI? Thanks for a very easy to follow tutorial BTW ! Jem

  • #205 Miguel Grinberg said 2015-01-06T23:26:58Z

    @jem: You have to run steps 7 and 8 each time you boot your Pi. You can put these two in a script and run it as a single command, or else you can install this script to run automatically at boot time (if you want the Pi to start streaming without you having to do anything).

  • #206 Stu H said 2015-01-27T16:58:01Z

    The sourceforge files have been changed!!!!

    The zip file is no longer there, only a tar.gz file. The content has also been updated, now I can not find the output_http.so or input_file.so, so the above method no longer works :-( I had used the above a number of times, but now I am trying to re-install for a re-flashed OS and am stumped at the first hurdle! Any help appreciated!

  • #207 Miguel Grinberg said 2015-01-28T06:29:27Z

    @StuH: I have no problem downloading the file. Check again, must have been a hiccup.

  • #208 Stu H said 2015-01-28T20:08:35Z

    Thanks for the reply, I've just downloaded again, the only 'file' in the input_file folder is input_file.c Do I have to point the 'make' cammand at this to create the .so file?

    The last try I used the command in the readme file:

    To compile and start the tool:

    tar xzvf mjpg-streamer.tgz cd mjpg-streamer make clean all

    However this did not create a input_file.so??

    Sorry but I'm a linux novice :-(

  • #209 Benjamin Corcoran said 2015-01-31T19:07:40Z

    I am getting to part 7 where we start raspistill and it starts however I am getting a recursive message "mmal: Skipping frame X to restart at frame Y" where x and y are numbers increasing with each iteration.

    any ideas?

  • #210 Miguel Grinberg said 2015-02-01T03:25:28Z

    @Stu: have you tried following my instructions above for building?

  • #211 Miguel Grinberg said 2015-02-01T03:34:56Z

    @Benjamin: Several people get that. Not sure what is it that causes it, but it indicates that some frames are being skipped. As long as some frames go through you should still have video streaming.

  • #212 SirGlitches said 2015-02-04T19:11:34Z

    when I run the raspistill command my raspberry restarts

  • #213 Miguel Grinberg said 2015-02-05T02:16:36Z

    @SirGlitches: make sure you have a sufficient power supply, and also that you have configured the camera module according to these instructions: http://www.raspberrypi.org/documentation/usage/camera/README.md

  • #214 Masno said 2015-02-11T19:59:21Z

    download link down?

  • #215 Miguel Grinberg said 2015-02-12T00:37:54Z

    @Masno: the download link works from here.

  • #216 Forrest Erickson said 2015-02-17T19:49:22Z

    How can I both start and stop a streaming session or process or what ever it should be called? I do not want to have to reboot the RPi just to turn things off.

  • #217 Miguel Grinberg said 2015-02-20T06:45:49Z

    @Forrest: to stop the streaming you can kill the raspistill and mjpg_streamer processes.

  • #218 rodriguez said 2015-03-02T17:25:23Z

    AWESOME ! worked like a charm ! just a quick question, how do you stop the stream ? i tried to kill raspistill but it didn't worked. Great tutorial anyway ! Thanks.

  • #219 AndyLicht said 2015-03-03T17:56:04Z

    Thanks for this great tutorial. But is it also possible to use two cameras and change the port?

  • #220 Miguel Grinberg said 2015-03-03T18:04:22Z

    @rodriguez: in addition to stopping raspistill, you also have to stop (or kill) mjpg_streamer.

  • #221 Miguel Grinberg said 2015-03-03T18:26:38Z

    @AndyLight: you can't have two RPi cameras connected. If you want to use a different camera as a second camera, then you need a replacement for raspistill, which will only work with the RPi camera. If you have that, then run two instances of mjpg-streamer on different ports.

  • #222 matteo said 2015-03-06T10:13:16Z

    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?

  • #223 Miguel Grinberg said 2015-03-06T20:04:17Z

    @matteo: this tutorial is for the raspberry pi camera, you will have to find a replacement for the raspistill command that generates images in the same way using your camera.

  • #224 jbrun1120 said 2015-03-15T01:12:46Z

    Keep getting a 404: Not Found error in the browser. Detail provided with message is "Invalid input plugin number". No errors when launching the streamer. Any ideas?

  • #225 Miguel Grinberg said 2015-03-15T06:37:01Z

    @jbrun1120: all the references I found to this error suggest there is something wrong with how you specified your input plugin. Did you build input_file.so?

Leave a Comment