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
  • #101 Adrian said

    Hi, Miguel!
    First of all, big thanks for this great solution! Secondly, if i'm lucky enough and this article is still under your attention, i would very much appreciate some help regarding the frame rate. No matter the resolution and quality i use, i can't get more than roughly 2fps..and the processor is not to blame, because i tried much higher resolution with better quality and it didn't have even a small problem processing all that stuff. I really can't figure out what is causing this limitation..so i would be very thankful for any advice. Many thanks again!

  • #102 Daddadhad said

    I am very fustrated at this error that happened after the first time I ran the mjpg stream server (the first time I did it it worked fine) now every time I do thisraspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 & : this happens

    mmal: main: Error opening output file: /tmp/stream/pic.jpg~
    No output file will be generated

    please help I am very annoyed

  • #103 Miguel Grinberg said

    @Daddadhad: can you create files in directory /tmp/stream? you could have a permissions issue.

  • #104 Villumsen said

    For me it helped to use the parameter -n to specify the filename as a temp pic.jpg~ sometimes also was found in the folder.

    /mjpg-streamer-code-181/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"

  • #105 Calum Bushell said

    Is it not possible to have the stream taking the images directly from the camera by using a /dev for the input instead of saving the image, then sending them out over the web.

  • #106 Miguel Grinberg said

    @Calum: It's not possible with the currently available software. A plugin for mjpg_streamer that reads the camera directly would do this, but it does not exist as far as I know.

  • #107 Alston said

    Hey, it really helps a lot!! BIG THANK YOU =) Now I can watch real time video on my mobile browsers. However, it seems too slow, my guess is under 5fps, perhaps due to the slow writing/reading of the SD card. Is there any way to solve this low fps problem? Is it possible not to stream via a disk file?

  • #108 Miguel Grinberg said

    @Alston: you can write to a ramdisk for a little bit better performance. The best performance is obtained using the HLS protocol, but while this protocol gives you very smooth video it adds a latency of 10+ seconds to live video. I am working on a tutorial for HLS, coming soon.

  • #109 Alston said

    Hey I found that GStreamer can stream 1080p@25fps via Wifi from Pi to my macbook (mobile/Linux/Windows are also possible). But it's not as simple as a HTML5 application, it's a software framework and needs to be compile and install on both server and client device. Fortunately, it is open sourced and offers SDK for almost every platform. I'm working on it with iPhone right now. Hope this helps!

  • #110 Jos M. Alonso said

    Thank you very much. I have found this thread very useful.
    I would suggest an enhancement: to include the option "2>/dev/null" in
    the raspstill sentence.
    Regrads from Madrid (Spain)

  • #111 utasek said

    Hi
    Id like to know if it is possible to use mjpeg streamer for 2 ip cam.
    I have instaled it for one IP cam-everything OK but now I have plugged second IP cam into USB and Id like to know how to adjust.Can anybody help me please?
    Thanx
    REgards
    Utasek

  • #112 Bilal said

    Hi, Miguel
    please I did every thing &the web page appeared but there's no any picture or video
    (when I write :
    raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &
    it gives me:
    [1] 2402
    -bash: raspistill: command not found
    )plz help me

  • #113 Miguel Grinberg said

    @Bilal: are you using a recent rasbian? try an upgrade to the latest one.

  • #114 vibhav said

    Nice tutorial thanks
    but i want to stream it everytime when it is booted without making use of commands again and again.. how to do that??
    And how to remove lag , its performance is equal to web camera.

  • #115 Leprich said

    Always i try to install libv41-dev following message occure:
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    E: Unable to locate package libv41-dev

  • #116 Andre T said

    Hi Miguel,

    Great tutorial! I was looking to implement exactly the same setup but instead an USB, to use an IP Camera. Would this be possible?

    thanks,

  • #117 Miguel Grinberg said

    @Andre: I'm not using a USB camera, this is for the RPi camera module. The images from the camera are passed to MJPG_streamer through .jpg files written to the file system. Any camera that can dump jpgs to disk will work with this method.

  • #118 Raspi-Confused said

    I have installed MJPG-Streamer to stream video from my Logitech c525 USB Webcam that is connected to a Powered USB Hub, I attempt to access the streaming from port 8081 and all I get is garbage characters on the web page..

    Can any one provide assistance to me in order to get this up and running so I can move on in my project? It would be greatly appreciated.

  • #119 David said

    Thanks for the post with great instructions to get the camera and a stream setup on the Raspberry Pi.
    Worked great and uses so little resources.
    Absolutely wonderful!!!! Many thanks.

    I made 2 startup init.d script to start both raspistill and mjpgsteamer at startup.
    Hopes this helps someone.

    Init.d script for starting the RaspiStill
    http://codetidy.com/paste/raw/8368

    Init.d script for starting the MJPG_Streamer
    http://codetidy.com/paste/raw/8369

  • #120 Geof said

    Thank you Miguel for these excellent instructions. I have the system working with jpg storage on the SD card but when I use a tmpfs ram disk the images do not stream. I have posted the question here:

    http://www.raspberrypi.org/forum/viewtopic.php?f=43&t=71977

    and wonder if you could suggest what the problem might be.

  • #121 Geof said

    There is now an excellent answer to this problem on the above forum. mjpg-streamer misses the jpg file changes because they occur more quickly in the tmpfs file than in the SD file.

  • #122 Bilal said

    Hi, Miguel
    I upgraded & updated OS
    but I use microsoft 3000HD camera ,can it work with this codes?if not what should I do please?

  • #123 Bilal said

    Thank you my friend, The problem has resolved
    Thank you very much

  • #124 Ulrich Rohrwasser said

    Hello,
    just succesfully implemented MJPG streamer and it works fine.
    There is one little issue I need to overcome. I control/program the PI remotely via SSH from my iMac. As soon as I close the terminal window on my iMac, MJPG streamer stops to work, but the cam seems to be still on (LED is on). What do I need to do to make the streamer running continously? Thanks for any hints.

  • #125 Miguel Grinberg said

    @Ulrich: you need to detach the server from your console. Google "how to start mjpg-streamer on boot" or similar terms.

Leave a Comment