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

  • #176 Raghav said 2014-09-14T07:31:52Z

    Thanks this can be very useful to me

  • #177 Boštjan Šuhel said 2014-09-21T13:33:26Z

    libv4l-dev it work without that lib. Lib is unknown.

  • #178 mark said 2014-10-02T10:21:23Z

    Hi Miguel,

    Thanks for the how to. I wrote a script to start the camera and MJPG at boot but it isn't working (the camera etc. starts but MJPG wont..)

    The 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" works on the command line but not in the script. Do you know how I can fix this?

    Thank you in advance! Mark

  • #179 Miguel Grinberg said 2014-10-06T00:33:53Z

    @mark: try setting the LD_LIBRARY_PATH variable in the line above the command.

  • #180 Ewa said 2014-10-24T06:37:44Z

    Hey Miguel,

    The streaming works perfectly for me. the one thing that is troubling me though is that i seem to be unable to embed the streamer into my website. I don't want the side bar menu or additional options that the MJPEG Streamer offers. I want to embed it into my website and be able to customize my background and such. I have no problems with a shoutout to MJPEG Streamer, i just want to be able to host this stream separately. Do you have any advice? I tried embedding it, like i would do with a youtube video. I tried just pulling the source code from the stream_simple.html and javascript_simple.html but i am having major issues with this. Am i missing something? any help would be great! thanks!

  • #181 Miguel Grinberg said 2014-10-24T14:34:03Z

    @Ewa: You can add the tag of the stream to your web page, that should be enough.

  • #182 Cancunia said 2014-10-24T18:24:55Z

    Many thanks for the writeup, it got me on the right track. May I suggest that for a future revision, you add the input_uvc.so plugin? Your examples are fairly clearly for the Pi camera module, but adding the UVC capability would be helpful for those people that don't have a Pi camera. Also might be worth including the make USE_LIBV4L2=true build option.

  • #183 Suavek said 2014-10-26T20:49:49Z

    Hi Miguel. Thank you for a good tutorial! Could you tell me what is the latency of the playback?

  • #184 Miguel Grinberg said 2014-10-27T00:49:18Z

    @Suavek: it depends on the resolution. For 640x480 it was around half a second.

  • #185 helmi said 2014-11-05T05:14:34Z

    is this only work in local network?

    can i stream the raspi using different network against raspi?

    really need the answer

  • #186 Miguel Grinberg said 2014-11-07T02:00:20Z

    @helmi: You can configure your firewall to direct traffic on certain port to your RPi, then you can connect to it from the outside.

  • #187 Matt said 2014-11-10T01:36:14Z

    Hi,

    Thanks for the article, really good explanation of how to get it working. I have one problem though. I have managed to get it working through the stream website i.e. http://:8080 but when I try to embed the image into a webpage it does not seem to load.

    Have you seen this before or know what it might be?

    Thanks

    Matt

  • #188 Miguel Grinberg said 2014-11-10T03:18:19Z

    @Matt: does the page is only missing the video, or is the whole page failing to load? I would check how you setup the web server for your web page, and if that is working then how you did the embedding, which you should copy from the mjpg_streamer web page HTML source.

  • #189 Steven Wheatley said 2014-11-10T22:01:55Z

    Hi Miguel, Thanks for this post - it was really useful. I got it working and saw the results being streamed. I'm new to UNIX and RaspPi so please bear with me. I was running the RaspPi headless and using Putty to open a terminal window. After running for about 3 or 4 minutes the network connection was dropped - probably due to the RaspPi WiFi dongle having a tiny aerial - so I had to pull the plug on the RaspPi. I connected it back to a monitor and rebooted. The thing is I can't find the /tmp/stream folder? Is this a UNIX thing where tmp folders are emptied? Also, as the camera is taking photos endlessly what's stopping the disk filling up? Many thanks for your time. This streaming module was really cool - I'm building a small roaming robot with my son and streaming pictures back to the control PC was on the list of things to do!

  • #190 Paul said 2014-11-18T10:14:10Z

    Miguel,

    Many thanks for a fantastic tutorial. It was incredibly simple to setup and a load of fun.

  • #191 Mikey B said 2014-11-21T15:12:22Z

    I just found this... first, i must thank you.

    Now if i may nitpick a little... step 2 is wrong (although functional). It really should be: sudo ln -s videodev2.h /usr/include/linux/videodev.h

    This uses a relative path, which for something in the same package (and in same directory even more so) is much better.

  • #192 Mikey B said 2014-11-21T15:45:57Z

    Holy Couch Potato Batman... The cpu on the Pi is running at 97%... IDLE. Wow!

  • #193 Kennet F said 2014-12-03T11:21:10Z

    Hi

    Great guide for a newbie!

    How do I add the startup command to rc.local to get this to start on every boot? I have just copied the lines to the file, but nothing happens.

    /K.

  • #194 Miguel Grinberg said 2014-12-03T22:30:16Z

    @Kennet: did you start both programs? For this to work you need raspicam and mjpg_streamer both running.

  • #195 Kennet F said 2014-12-05T09:06:34Z

    @Miguel When I type in the commands manually it works just fine, but when I reboot the raspberry I want it to start those commands, and I guess I had to put them in the rc.local file? I have just copied the commands into the file. Correct me if I am wrong. "$ mkdir /tmp/stream $ raspistill --nopreview -w 1024 -h 768 -q 5 -o /tmp/stream/pic.jpg -tl 100 -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"

    /Kennet

  • #196 Miguel Grinberg said 2014-12-06T00:45:35Z

    @Kennet: I don't have a Pi at hand right now to test, but the commands are probably be something like this:

    mkdir -p /tmp/stream raspistill --nopreview -w 1024 -h 768 -q 5 -o /tmp/stream/pic.jpg -tl 100 -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" &

  • #197 Dominik said 2014-12-26T12:36:44Z

    HI,

    how can i change the port?

    thanks

  • #198 Miguel Grinberg said 2014-12-27T07:29:23Z

    @Dominik: add a -p option right after output_http.so in the mjpg_streamer command line.

  • #199 Dishant Shah said 2014-12-27T12:59:09Z

    hi,

    Thanks for this great tutorial.

    However one error is coming while fetching mjpg-streamer files showing that files are missing from link http://sourceforge.net/code-snapshots/svn/m/mj/mjpg-streamer/code/mjpg-streamer-code-182.zip

    It will be grateful if I can get link from where I can fetch files.

  • #200 Miguel Grinberg said 2014-12-27T22:33:29Z

    @Dishant: the link you pasted works fine for me. Try again, maybe it was a temporary problem.

Leave a Comment