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
  • #26 Andrew said

    Hi All,
    I have everything up and running. Great tutorial. The only problem i have is the FPS will be between 1.5-2 no matter what i change for the raspistill settings. any suggestions?

  • #27 Simon Pooley said

    Excellent tutorial Miguel.
    Thank you very much for your work,
    regards, Simon.

  • #28 Stephen said

    Fantastic Job. After mucking about for more time that I would like to admit with original Raspberry Pi Camera instructions using Mplayer and netcat (mplayer could never never find/connect to the stream from RPI and would exit) I came across your page. Initially a bit hesitant because there are so many ways for compiling to go horribly wrong, I went step by step through your instructions and in 10 minutes was streaming to browsers on Mac, Windows 7 and Linux. Thanks for doing all the heavy lifting!

  • #29 Martyn Jones said

    Thanks Miguel, these instructions are great and it worked as soon as I'd got rid of all my typos!. Can you tell me the highest quality video settings that you've managed to achieve with this software? i.e best -tl and -q settings. I'd like to stream nestbox video without any timelapse (jerks) but leaving out the -tl switch results in no video at all on my setup.
    Thanks again for your work on this.
    Cheers, Martyn

  • #30 PIT2 said

    Hi,
    great job :-)
    It works like a charm ! but i try to get only the pic.jpg
    what is the direct url to get the pic.jpg
    I would integrate the raspcam into my domotic box.

  • #31 Miguel Grinberg said

    @PIT2: I don't have it setup right now to check, but if you view the source code of the page you can find out the URL easily.

  • #32 Denis said

    Hello,
    I got the stream working over ethernet but trying to start it using wifi the interface the connection breaks down immediately and does not reconnect. That appears to be a big problem for me, so please post some advice... Greets, Denis.

  • #33 PITP2 said

    ok i will try this.
    I've got a problem , the cam freeze sometimes.
    I can not see the video whitout having to reboot the rapsberry regularly ... it's not great if i want to use it for an ip camera

  • #34 Miguel Grinberg said

    @PITP2: likely a power source problem.

  • #35 PITP2 said

    ok i changed the power source. We will see if it was the problem.
    How to change the http server port ? I would like to use 80 instead of 8080

  • #36 Miguel Grinberg said

    @PITP2: To run on port 80 add "-p 80" (without the quotes) right after output_http.so. Note that port 80 can only be accessed by the root user.

  • #37 PITP2 said

    thank fort the port.
    I changed the power source last evening but this morning i can access to the web server but not the video . The cam is down (the red led is off)
    It's seem that is not a source problem. The source power output is 5V 1A

  • #38 MeanMarty said

    Hi,

    Is there any way to get this running in background, as a service, and to start automatic after reboot?

  • #39 Miguel Grinberg said

    @MeanMarty: if you google for "raspberry pi start program on boot" you will find plenty of articles that show how to do what you want.

  • #40 mkkyah said

    Dear Miguel,
    Thanks for the detailed tutorial, it really worked for me easily.
    Now I need to start this installation on every startup. I have tried some sh and rc.local tricks, but couldn't make it work.

  • #41 LeB said

    Hi Miguel,

    I appreciate your detailed post. It solves my problem on broadcasting live stream with RPi camera. But the link you provided is no more available. I forgot to backup data after installed.

    Do you (or anyone) have any spare links to download file "mjpg-streamer-code-181.zip"? Please help me! Many many thanks.

  • #42 Miguel Grinberg said

    @LeB: I have updated the link.

  • #43 Ponch said

    Raspberry Pi / Linux squid alert!

    Hello, I think I'm following your instructions but for the life of me I can't figure out what I'm doing wrong. When I type:
    raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &
    I get:
    mmal: main: Error opening output file: /tmp/stream/pic.jpg~
    No output file will be generated

    What I'm trying to do is to have the camera run so that I can embed the video feed on to my website. I figure if I can have this working with the streamer website, I could extract some code from it to add the feed to my site. Any thoughts? Thanks in advance.

  • #44 Miguel Grinberg said

    @Ponch: did you create folder /tmp/stream?

  • #45 Ponch said

    <Quote:@Ponch: did you create folder /tmp/stream?>

    Miguel, thanks for helping me ... Yes and I've even gone back to try and create it again and it tells me it already exists.

  • #46 Miguel Grinberg said

    @Ponch: well, for some reason raspistill cannot create the file in /tmp/stream, so you should investigate why the user account that you are running on does not have write access in that folder. You can also try setting a different output folder, maybe try "-o /home/pi/pic.jpg" and see if that goes through. Remember to also change the mjpg_streamer command to match.

  • #47 Ponch said

    Miguel, thanks again for helping me. I'm not sure what I did wrong but I decided to start from scratch and reload an image. Once reverted back to my image, I followed your steps from the start again and not it works. Again, not sure what I did wrong, but your instructions are correct and VERY helpful. thanks again!!

  • #48 Dan Cristian said

    add -n and it shoould work

    mjpg_streamer -i '/usr/local/lib/input_file.so -f /tmp/mjpg -r -n test.jpg' -o '/usr/local/lib/output_http.so -p 8080 -w /usr/local/$

  • #49 slava said

    @Miguel, Thanks for great instruction. It worked well !

    @homer, in last command line, you missed specifying actual name of the file "-n pic.jpg"

  • #50 Seedy said

    Hey and thanks for doing this guide! It really helped me get my setup working. But I do have one more question, once we have the stream up and running, how do we customize the mjpg streamer demo page?
    I'd like to add some encryption at the very least. Thanks again!

Leave a Comment