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
  • #276 Sam said

    The instructions seem to work for me. However, the raspistill stops around 100003 frames each time - it doesn't take pics anymore. It is also no longer running (confirmed by ps -ef | grep raspistill). As a consequence, the mjpg_streamer shows a blank feed. Anyone else run into the same issue ? If so, could you tell me how you got around it ?

  • #277 Miguel Grinberg said

    @Sam: you can wrap raspistill in a bash script that re-runs it once it exists. Unfortunately raspistill does not have an option to run forever.

  • #278 Tomski said

    Thank you for this tutorial, it is really helpful.

    Couldn't find how to do steps 3-5i, but resolved here:

    http://jacobsalmela.com/raspberry-pi-webcam-using-mjpg-streamer-over-internet/

    picked up here again from part 5ii

  • #279 Jim said

    Hey!

    How do I embed the output on my port 8080 to a webpage?

  • #280 Miguel Grinberg said

    @Jim: look at the page source for the mjpeg-streamer page. You have to add that same code to your own page if you want to embed the stream there.

  • #281 Philip said

    Thanks for the tutorial.

    I'm trying to use this with a PI ZERO and a USB WEB CAM. when I enter the raspistill comamnd I get the message "failed to create component 'vc.ril.camera' i.e. failed to create camera component. the Camera works though because I can take pictures with it using fswebcam.

    can this solution not be used with a usb web cam?

    thanks
    Philip

  • #282 Miguel Grinberg said

    @Philip: The raspistill utility works with the Raspberry Pi camera module, to my knowledge it was never extended to work with USB webcams. If you find a replacement tool that can write pictures to disk with your camera, then you can keep everything else the same and you should be able to get the streaming to work.

  • #283 Hk said

    Currently I am using motion streamer for webcam server can i am using mjpg server simultaneously on same memory card in which currently motion streamer has been configured?????

  • #284 Miguel Grinberg said

    @Hk: Yes, but note that this project is for the Pi camera, not a generic webcam.

  • #285 Shane Leonard said

    When I enter the make mjpg_streamer input_file.so output_http.so command, the last line of code reads 'make:***No rule to make target 'output_http.so' . Stop.

    I have entered this code online to see what it means and how to fix it but not coming back with anything. The next commands aren't working as a result so it must be this error stopping me from progressing on. If you could shed some light on this error for me, and how to solve it, it would be appreciated.

    Looking forward to hearing from you,

    Shane

  • #286 Vivian Escalante said

    Hello,

    I was trying to make this work with the FLIR Lepton by starting off with only 1 image. I stored this image in the /tmp/stream folder, but when I run LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /tmp/stream -n IMG_0000.pgm" -o "output_http.so -w /usr/local/www" it just halts after the commands line below. Any idea why? Thanks!

    MJPG Streamer Version: svn rev:
    i: folder to watch...: /tmp/stream/
    i: forced delay......: 0
    i: delete file.......: no, do not delete
    i: filename must be..: IMG_0000.pgm
    o: www-folder-path...: /usr/local/www/
    o: HTTP TCP port.....: 8080
    o: username:password.: disabled
    o: commands..........: enabled

  • #287 Miguel Grinberg said

    @Shane: maybe you have an incompatible make version in your system, I'm not sure. Try running just "make" to see if that works. Else see if you can upgrade make to the latest version.

  • #288 Miguel Grinberg said

    @Vivian: not sure this is going to work with a single static image, the idea is that the image is constantly updated with new frames. Also just noticed you are using a pgm image, but the mjpeg format obviously requires jpegs.

  • #289 swapnil said

    is there requirement for mjpeg format video compatible usb webcam

  • #290 Luis said

    Thank you very much Miguel!
    Just a couple of question:
    1- Can I configure the system to show a continuous video?
    2- Can I set multiple cameras and access them from a simple browser? If yes, how?
    Thanks in advance.
    Regards,

    Luis

  • #291 Miguel Grinberg said

    @Luis: what do you mean by continuous video? The instructions above are for a stream, not for a static picture. Doesn't that qualify as "continuous"? To run multiple cameras you can just run multiple instances, one per camera.

  • #292 Miguel Grinberg said

    @swapnil: mjpeg video is just a sequence of jpeg images. Any camera that produces jpegs can be used, as long as you have a way to obtain those jpegs and feed them into mjpeg-streamer.

  • #293 Luis said

    @Miguel,
    Thanks for the answers.
    I can see a video, but it shows a photogram every second (approx.), and apparently, just one of every 5 images that the system capture. I would like to see something more dynamic.
    Are there some option to start the streaming automatically every time I take on the Rpi?
    Regards

  • #294 Miguel Grinberg said

    @Luis: my guess is that this is a bandwidth problem. Are you doing it over the internet? Try doing it within your LAN, that should work much better. To improve frame rate over the internet you need to make frames smaller, either by increasing jpeg compression, reducing image dimensions, or both.

    If you need something that approaches live video frame rates, then motion-jpeg is not the right approach. You can look at streaming formats like HLS, which can sustain a 24 or higher frame rate, but have a much higher latency.

  • #295 swapnil said

    raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &
    after entering this command it gives me that camera not detected ..i have installed USB camera Quantum 495lm What can be the problem...? please revert ASAP.

  • #296 Miguel Grinberg said

    @swapnil: raspistill only works with the RPi camera module. You will need to find another utility that works with your USB camera.

  • #297 Elraidor said

    I have the same problem as Shane - the make step fails with 'No rule to make target 'input file.so' Stop.

    Using command of just 'make' gives the error

    Makefile:80: recipe for target 'input_uvc.so' failed

  • #298 Miguel Grinberg said

    @Elraidor: I don't have a Pi at hand right now to test, but really, it all comes down to debugging the makefile. You need to build the input_file plugin and that does not work, for some reason. Look in the makefile and find the commands that need to run, then run them manually to see if you can get it to build that way.

  • #299 Steve Rider said

    Thanks for this! I have six Raspberry Pi webcams around my property. Here are a few extra bits I added.

    In /etc/fstab I added lines to put /tmp and /var/tmp in a ramdisk. This saves the SD card wear and tear.
    tmpfs /tmp tmpfs defaults 0 0
    tmpfs /var/tmp tmpfs defaults 0 0

    I also created a bash script to start things up, I arbitrarily named ~/stream.sh
    raspistill --nopreview -hf -vf -w 2592 -h 1458 -q 80 -o /tmp/stream/pic.jpg -tl 8 -t 0 -th 0:0:0 &
    LD_LIBRARY_PATH=/usr/local/lib /usr/local/bin/mjpg_streamer -b -i "input_file.so -f /tmp/stream -n pic.jpg" -o
    "output_http.so -w /usr/local/www"

    Then I created a crontab entry to start it all up at boot time.
    @reboot sh /home/pi/stream.sh

    Works good, lasts a long time.

  • #300 Kuan Wai Lai said

    Hi,

    Thank you for the post, what if I am using IP camera instead of using the rpi camera? Do I need to write coding to replace "raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &" that runs independently and keep updating the image? I am new to rpi, thank you in advance.

    Thanks!

Leave a Comment