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

  • #226 Jameson said 2015-03-17T01:46:47Z

    Is there any way this can be done with a usb webcam instead of the camera module?

  • #227 Miguel Grinberg said 2015-03-17T03:38:08Z

    @Jameson: the raspistill utility does not work with USB webcams, you need to find a replacement tool that can write jpegs to disk in the same way as raspistill.

  • #228 Ageel Shatry said 2015-03-18T04:25:14Z

  • #229 Miguel Grinberg said 2015-03-19T04:55:51Z

    @Ageel: the link works for me.

  • #230 Sam B said 2015-04-03T17:34:44Z

    great tutorial, works great! One question though, the line "mmal: Skipping Frame (number) to reatart at frame (number)" keeps appearing and filling the entire screen, is there a fix for this and how do i safely stop the program? Thanks in advance

  • #231 Miguel Grinberg said 2015-04-03T18:25:01Z

    @Sam: you have to stop two processes, raspistill and mjpg_streamer. You can use kill to stop them. A more elegant solution would be to create wrappers that allow you to start and stop them as standard Linux services, but I haven't done that myself.

  • #232 stud said 2015-04-07T10:38:58Z

    Thank you very much. Last week i had no problem with your tutorial, but now your link "wget http://sourceforge.net/code-snapshots/svn/m/mj/mjpg-streamer/code/mjpg-streamer-code-182.zip" seems to be unaccessible. ERROR 404 on source forge. Did you remove it?

  • #233 Miguel Grinberg said 2015-04-09T02:17:28Z

    @stud: I am not the owner of this project, maybe the site was offline, seems fine now.

  • #234 DaisyDane said 2015-04-17T01:56:25Z

    @Miguel, Another great tutorial. I've done a few you've authored while trying to learn Python and Flask. Any insight into this error when I try and start the stream up? bind: Address already in use o: server_thread(): bind(8080) failed

  • #235 Miguel Grinberg said 2015-04-19T02:49:33Z

    @DaisyDane: maybe you have some other server running on port 8080?

  • #236 Joaquim Rosa said 2015-04-22T09:12:38Z

    @Daddadhad: The following error appears if the directory /tmp/stream does not exist. Thus, you have probably forgot to do "mkdir /tmp/stream" (from step 7).

    mmal: main: Error opening output file: /tmp/stream/pic.jpg~ No output file will be generated

  • #237 naqib naqiuddin said 2015-04-27T18:11:28Z

    i had problem with your link "wget http://sourceforge.net/code-snapshots/svn/m/mj/mjpg-streamer/code/mjpg-streamer-code-182.zip" seems to be unaccessible. ERROR 404 on source forge. Did you remove it?

  • #238 Miguel Grinberg said 2015-04-28T14:21:46Z

    @naqib: see comment #233 above.

  • #239 Andrew said 2015-04-29T16:57:45Z

    Hi Miguel

    Thanks alot for the guide ! everything Works great ! Quick question though, Is it possible to stream this to separate network, for instance my own website where I would be able to view from anywhere. It would also require a login to view.

    Thanks !

  • #240 Miguel Grinberg said 2015-04-30T17:34:19Z

    @Andrew: this type of streaming uses a "pull" model, the client needs to pull from the server. You can write a little client that pulls from your Pi and then forwards the stream to clients behind authentication. This StackOverflow answer shows such a setup with nginx as reverse proxy: http://stackoverflow.com/a/24846230/904393.

  • #241 chakka said 2015-05-01T18:07:28Z

    While downloading the MJPG streamer the following error occured: HTTP request sent awaiting respense......404 Not found Error 404: not found Plz help me how should i proceed further

  • #242 Bargepole said 2015-05-25T10:06:09Z

    A really interesting article.

    I have a USB Web Camera with Pan/TILT can you point me so some info I could read hoe to stream from an USB Camera rather than the Rpi Camera (Anyone know of a good casing for the camra - I've seem some in amazon (from SB) but they reieved some bad critic.

    Many tahks

    BP

  • #243 Miguel Grinberg said 2015-05-26T05:46:40Z

    @Bargepole: You need an application that works similarly to raspistill, that can write the jpegs to disk so that mjpg-streamer can pick them up. If you have that, then you should be able to make it work.

  • #244 Bob said 2015-05-27T00:35:37Z

    The link at sourceforge is dead "wget http://sourceforge.net/code-snapshots/svn/m/mj/mjpg-streamer/code/mjpg-streamer-code-182.zip" seems to be unaccessible. ERROR 404 on source forge.

    Anyone got a fix for this....?

  • #245 Miguel Grinberg said 2015-05-27T22:59:27Z

    @Bob: not sure why sometimes that link breaks, it comes and goes. You can also go to http://sourceforge.net/p/mjpg-streamer/code/HEAD/tarball to download from your web browser.

  • #246 Bob said 2015-05-29T06:07:29Z

    That worked, thanks

  • #247 Edih said 2015-07-03T09:58:15Z

    hi, I keep getting this error when I try this line from your step $ raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &

    error: mmal: mmal_vc_component_create: failed to create component 'v camera' (1:ENOMEM) mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1) mmal: Failed to create camera component mmal: main: Failed to create camera component mmal: Camera is not detected. Please check carefully the camera module is instal rrectly

    I can take pictures on fswebcam so the camera might be working well. any suggestions?

  • #248 Miguel Grinberg said 2015-07-03T16:33:31Z

    @Edih: error ENOMEM means you are running low on memory. Try closing all other applications you are running.

  • #249 Girard said 2015-07-08T03:18:43Z

    hey there... i was following the instructions above but i got this message:

    $ make mjpg_streamer input_file.so output_http.so make: *** No rule to make target 'mjpg_streamer'. Stop.

    and so the next step, i get this:

    $ sudo cp mjpg_streamer /usr/local/bin cp: cannot stat `mjpg_streamer': No such file or directory

  • #250 Miguel Grinberg said 2015-07-08T04:47:08Z

    @Girard: the "make" line needs to do what it needs to do for the following step to work. Are you sure you are doing the "make" line in the correct directory?

Leave a Comment