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
  • #1 Edwin said

    Hello,

    I just wanted to say 'big thank you for your efforts'. However, I still have some question. There is symbol & in code, which puts the application to run in the background. I pasted command in terminal, but preview windows still showing up. I can't do anything in a system, because it's so big. It hides my dekstop. Maybe there is another way to hide this preview function? Thanks!

  • #2 Miguel Grinberg said

    @Edwin: I don't see a preview because I don't use the windowed mode, I do everything in the command line. Would that be an option for you?

    Looking at the raspistill documentation it appears you can use "-n" to disable the preview. Here is the documentation: http://www.raspberrypi.org/wp-content/uploads/2013/07/RaspiCam-Documentation.pdf
    If you confirm that this addresses the problem I'll add the -n to the example above. Thanks!

  • #3 Edwin said

    I made short video about this problem. http://sdrv.ms/18LuFE2 Maybe it has to be that way? I am still a beginner in Linux, so there is a high probability that's my mistake. Sorry.

  • #4 Miguel Grinberg said

    @Edwin: interesting. Try running this from the command prompt, not from a Terminal inside "startx". That is how I do it, I didn't even realize raspistill tries to open a preview window. If I figure out a way to turn it off I'll post it here.

  • #5 Cromfel said

    Hello Miguel

    Thanks for this tutorial. Im stuck on the phase for getting the mjpg-streamer. I found some r63 tar.gz file only from sourceforge, since your link was dead. Unfortunately Im not experienced enough in linux to circumvent this. Do you think you could upload this .zip file to google drive or somewhere for download or the 181 ?

  • #6 Cromfel said

    Forget the previous message. It was as usual just a stupid typo :)

    Now I have everything apparently up and running.
    I run:
    LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /tmp/stream" -o "output_http.so -w /usr/local/www"

    And now apparently the streaming is live. When I go to my www folder and open the mjpg-streamer simple html I see only thumbnail of the picture what should be there (SOme placeholder image) so apparently it is not working. My picture file is updating correctly with the raspistill.

    Any idea how to proceed debugging this?

  • #7 Miguel Grinberg said

    @Cromfel: are you opening the pages in the "www" directory directly? That's not how it works, you should connect to the web server instead. Type "http://localhost:8080" in your address bar, or if you are doing it from another machine "http://<IP-address>:8080".

  • #8 Edwin said

    Hello, Miguel!

    In previous comment you mentioned about 'command prompt'. I tried to use your code withouth Gui, but same thing happened. I saw myself in monitor. I will have plenty of time this weekend, so I will check camera and PI connection, maybe there is wrong connection. Can you provide your email? We can chat about this stuff. I am glad that there is people who like technologies, Linux, etc.

  • #9 Cromfel said

    Ok naturally. Yeah I managed to grab another PC to test it. Works. Unfortunately the fps using the raspistill is quite low. And for some reasons every now and then it just stops the streaming saying "could not open file for reading: No such file or directory" what seems to be pretty random. Some times it never starts. Some times it works for while and suddenly gives this message.

  • #10 Homer said

    I have tried this twice now on a fresh build of wheezy, and get the same problem. MJPG-streamer is running but I get a file not found. I can't work out why. Any idea's anyone? Thanks Homer.

    pi@raspberrypi ~/mjpg-streamer-code-181/mjpg-streamer $ sudo cp mjpg_streamer /usr/local/bin
    pi@raspberrypi ~/mjpg-streamer-code-181/mjpg-streamer $ sudo cp output_http.so input_file.so /usr/local/lib/
    pi@raspberrypi ~/mjpg-streamer-code-181/mjpg-streamer $ sudo cp -R www /usr/local/www
    pi@raspberrypi ~/mjpg-streamer-code-181/mjpg-streamer $ mkdir /tmp/stream
    pi@raspberrypi ~/mjpg-streamer-code-181/mjpg-streamer $ raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &
    [1] 4460
    pi@raspberrypi ~/mjpg-streamer-code-181/mjpg-streamer $ LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /tmp/stream" -o "output_http.so -w /usr/local/www"
    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..: -no filter for certain filename set-
    o: www-folder-path...: /usr/local/www/
    o: HTTP TCP port.....: 8080
    o: username:password.: disabled
    o: commands..........: enabled
    could not open file for reading: No such file or directory

  • #11 Miguel Grinberg said

    @Edwin: When you say you tried the command prompt, was it a console outside of "startx" or a terminal application window inside "startx"? The first is the one I was suggesting, as that runs without the graphics and windowing subsystems enabled.

  • #12 Miguel Grinberg said

    @Homer: another commenter says he worked around the "could not open file for reading" adding a "-n pic.jpg" argument right after "-f /tmp/stream". Could you try that and let me know if it works for you as well?

  • #13 Edwin said

    So I managed to get through it. I used '--nopreview' in raspistill code. Thanks, Homer! Now camera works in the background mode. Anyway, one thing is not clear. I check http://<IP-address>:8080 where <IP-address> is my ip address and I don't see any picture. There is Welcome page, but somehow my system can't show any pictures. Very strange stuff I should say.

    P.S. Homer. Try to change directories in which you place your picture. I will get some break, because those issues of Linux drives me crazy.

  • #14 Miguel Grinberg said

    @Edwin: One of the sections of the MJPG-Streamer website shows static pictures. Do you see a picture there? If you do, then the problem with the streaming is browser related, try the Javascript section which shows the stream using a different method.

  • #15 Kevin said

    Same error here as Homer has got...
    By the way, your other (older) tutorial has worked fine for weeks with another pi. So thank you very much anyway :)

  • #16 Edwin said

    I produced another video. http://sdrv.ms/16LDQZb Same thing happens without startx mode. I don't want to spam your blog.

  • #17 Miguel Grinberg said

    @Edwin: what's that error you get on your screen, something like "no output file will be produced"? I think there is a problem with your raspistill comand. Check in /tmp/stream to make sure file pic.jpg is constantly generated.

  • #18 Edwin said

    Hello, guys!

    After long week of failures, my Raspberry Pi Camera works like a charm! What I did differently? Miguel was right! You need attention to detail. I double-checked raspistill script and found a tiny mistake. I used slash where you don't need that. So my new script line was 'sudo raspistill -n -w 640 -h 480 -q 5 -o pictures/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &' My mistake was a slash before pictures. I don't know why, but I use sudo just in case. After that, I hit another command sudo LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f pictures -n pic.jpg" -o "output_http.so -w /usr/local/www" I added a tiny -n pic.jpg argument after pictures folder. Now everything works. My advise to others - double-check your scripts and try changing directories. I think there is no difference where you will place your pictures.

    Here is my video proof about whole concept. http://sdrv.ms/1f9MBOY Sorry about lots of rubbish comments in this blog. I guess that's a newbie mistake. You ask a lot of questions.

  • #19 StaticDet5 said

    Fantastic work!

    I just set this up to work on two of my Pi's. The first one gave me fits. I may have hit upon one solution. Between steps six and seven I tried:
    "sudo apt-get update"
    followed by
    "sudo apt-get upgrade"

    When I did this in sequence with the second Pi, things went very smoothly.
    Finally, I wrote a quick command, CamStart:

    mkdir /tmp/stream
    raspistill --nopreview -w 960 -h 540 -q 80 -o /tmp/stream/pic.jpg -tl 10 -t 9999999 -th 0:0:0 &
    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"

    It's just steps 7 and 8 in a command file, making it easy to start the camera up.

    Thanks again!

  • #20 Juan said

    Hi

    Thanks for your tutorial.

    I have a problem with this command

    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"

    it appear this error

    MJPG Stream Version: svn rev:

    ERROR: could not find input plugin
    Perhaps you want to adjust the search path with:
    # export LD_LIBRARY_PATH=/path/to/plugin/folder
    dlopen: input_file.so: cannot open shared object file: No such file or directory

  • #21 zark said

    Homer,

    I found this page last night. I got the same error. Check /usr/local/www directory to make sure web server markup did not end up in /usr/local/www/www.

    Miguel, thanks for the writeup.

  • #22 PattyChuck said

    Any advice on getting this to run on boot? I'm a bit of a noob with Linux. I added the "mkdir" and "raspistill" commands to a start-up script, and it works just fine (the jpg is most certainly being written to the temp folder). However, adding the command in step #8 does not seem to start the server. Any pointers on getting this to run when the RPi is booted up?

  • #23 Miguel Grinberg said

    @PattyChuck: an easy way to get a program to run after boot is to add the command to the /etc/rc.local file. For an example see http://www.raspberry-projects.com/pi/pi-operating-systems/raspbian/auto-running-programs.

  • #24 Juan said

    Hi

    I check the path that you said and I found the /usr/local/www has the same archives than /usr/local/www/www

    Any help would be much appreciated

    Thanks

  • #25 melb_guy said

    worked for me. Great instructions How do I run this script at the boot up to start camera automatically ?. and storing it on the cloud or NAS storage ?

Leave a Comment