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
  • #51 Miguel Grinberg said

    @Seedy: The pages served by the web server are in the www folder and can be changed, as long as you don't modify the streaming portions.

  • #52 Sascha said

    Good tutorial!
    Does anyone know a solution for the timeout problem?
    With "-t 9999999" raspistill runs 2-3 hours. After that the process ends. Is there a possibility to run the raspistill process endless?

    Thanks,
    Sascha

  • #53 Miguel Grinberg said

    @Sascha: You can enter a larger number, I didn't check but that probably goes to a 32-bit integer variable, so it should be safe to use something like 2 billion there, which will give you over 500 hours. Alternatively you can put the command inside a bash script that does an endless while loop, so that as soon as it ends it restarts.

  • #54 toby1kenobi said

    Great post, thanks for this (and the other one), very helpful! If you manage to get more advanced streaming methods working (as you've commented) I'd love to see how you do it.

  • #55 Miguel Grinberg said

    @toby1kenobi: This is the method with the shortest latency. The "more advanced" methods have better frame rates, but carry a delay of at least 10 seconds. I will be publishing a guide for HLS streaming soon.

  • #56 JP said

    Any way to accomplish this without continued writes to the SD?

  • #57 Miguel Grinberg said

    @JP: Sure, you can create a ramdisk and write the jpegs there. There are a lot of tutorials to setup a ramdisk, people also mount /var/log there to avoid the repeated syslog writes.

  • #58 aman agarwal said

    from where can i get libjpeg for raspbian????

  • #59 Miguel Grinberg said

    @aman: sudo apt-get install libjpeg8-dev

  • #60 Matthias said

    Hi Miguel,
    after I tried to use my PI-cam, it worked fine for me.
    Since I moved the the PI to another location, I had to unplug it from power.
    After the reboot, I'm not able to get it working again - I just started with all commands after unzipping.
    And further (same important):
    Which steps do I have to execute and where to write in these commands to start this everytime I boot the PI?
    Regards,
    Matthias

  • #61 Miguel Grinberg said

    @Matthias: you just need to repeat steps 7 and 8 above. See this article for ideas on how to configure a script to run on startup: http://raspberrypi.stackexchange.com/questions/8734/execute-script-on-start-up

  • #62 John said

    Hi,
    Everything is working fine for me until I get to step seven and run the line:
    $ raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &

    This:
    [1] 2265
    is added to the bottom and then after a long time of unresponsiveness it says

    $ Write failed: Broken pipe
    and my pi stops responding to anything but the camera light stays on.

    Any tips?
    Thank you very much!

  • #63 Ken Ng said

    I did one thing extra. Instead of writing the image to the local disk (SD card) i created a 1MB RAM disk and and used that instead for the image stream. Works really well and the good thing is this totally avoids the need to 'overwrite' on the flash cells that have a limited lifetime of writes.

    What I did was to simply create a script that basically creates the RAM disk, mount it, then start the raspistill capture and then start the mjpeg-streamer.

    Thought I'd share this tip and thanks for putting this up.

  • #64 Oat said

    Thank you. I followed your tutorial. I want to modify html file. How to I upload or modify the html file. I tried to go into mjpg-streamer/www and insert my own html file and it doesn't work. thanks

  • #65 Miguel Grinberg said

    @Oat: If you follow the above instructions then there is a copy of the "www" folder in /usr/local/www, that's the one you need to modify.

  • #66 andy crofts said

    Many thanks. Just worked straight out of the box. I simply copied/pasted each line from your page straight into the Pi. No intelligence/geekery whatsoever required!
    This is EXACTLY what I need. (Now, to try ZoneMinder with it...)
    Again, much your hard work much appreciated.
    -Andy

  • #67 Joe Colson said

    Thank you ! Thank You ! Thank You!
    I have searched the better part of the day looking for a simple way to add the camera module to my project when I found you website. It worked perfectly and the added web pages were a very happy plus. Thanks for taking the time to share !!

  • #68 Doug Wyman said

    I have mjpg-streamer up and it will stream a camera that failed on ff(whatever). It was very simple to set up and get running in the background. It runs for days without a glitch. Thanks for the guidence

  • #69 Ton van Wissen said

    Thank you Miguel for sharing these instructions.
    I'm using exactly your parameters, but my /tmp/stream directory is on ramdisk. All is working rather well, except for a few issues:
    1) after starting raspistill ... etc (using putty) I keep receiving messages from mmal: skipping frame .... to restart at frame ...
    2) The framerate stays around 1 fps with a lagtime of 0.5 sec

    Do you have further advice for me (I'm a Linux newbie) or must I settle for this?

    Thank you

  • #70 Fabian said

    Your instructions were AWESOME. Followed it and got my streamer running. Things to note. I kept getting errors spit to the console
    "mmal: Skipping frame # to restart at frame ##"
    This was preventing me from running it in the background as the command kept showing and logging off killed the streamer. Even sending it to dev null did not help. It looks like I was not getting enough power for the camera (I am also using a USB wifi) to do 10 frames a second. I switched it to 1 frame a second and it ran fine. I am now setting it up to use cron when my kids get home so I can see what they are doing. Very useful. Thanks for the thorough and great instructions.

  • #71 Joe Colson said

    Miguel - I have few questions.

    1) Is there a way to run it forever instead of a few hours?

    2) Will you method work with Zoneminder? ( www.zoneminder.com )

  • #72 Joe Colson said

    Gentlemen -
    Ok I answered my own question. ZoneMinder Does work with your method of sending Video. Setup is as follows
    * From the ZM console click “Add New Monitor”
    * Under the “General” tab name your monitor, set “Source Type” to “Remote”, then select the function you like.
    * Under the “Source” tab set:
    - “Remote Protocol” to “HTTP”
    - “Remote Method” to “Simple”
    - “Remote Host Name” to <RPI_IP_ADDRESS>
    - “Remote Host Port” to “8090″ (or whatever you used in mjpg_streamer command)
    - “Remote Host Path” to “?action=stream”
    - “Remote Image Colours” to “24 bit color”
    - “Capture Width” to “640″ (or whatever you set in the raspistill command)
    - “Capture Height” to “480″ (or whatever you set in the raspistill command)

    Which by the way is AWESOME !!

  • #73 Miguel Grinberg said

    @Joe: 1) you could wrap the call to raspistill in a script that calls the command again when it ends.
    2) Probably not. You need a specific support for the Pi camera, they don't seem to have that.

  • #74 Miguel Grinberg said

    @Joe: Ah, I think I see what you did. You are running zoneminder not on the Pi but on your desktop computer?

  • #75 Joseph Mathew said

    I have a 256MB ModelB. When I run raspistill imeediately, all the LEDs stop except for the Red LED and the network disappears. I have to start the ethernet with ifconfig <ip> up command to get network up. But I am able to save the captured image locally with -o option.
    Can you suggest how to debug this problem or this will not work on 256MB system?
    Appreciate your guidance.
    Thanks

Leave a Comment