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
  • #301 Miguel Grinberg said

    @Kuan: Yes, you need to use an equivalent utility for your camera.

  • #302 Guillermo said

    Package libv4l-dev is not availabel for Mac. What other package I use ?

  • #303 Miguel Grinberg said

    @Guillermo: this tutorial is for the Raspberry Pi and its camera. If you plan on using a Mac then you need different code that can access the Mac camera.

  • #304 RA said

    Great blog! I know this was posted 3 years ago, but I just found it. Do you think I would be able to run this program (with some tweaking) on a Jetson microcontroller?

    Thanks!

  • #305 Miguel Grinberg said

    @RA: not sure, sounds like it is not at all like the RPi.

  • #306 luigichi said

    worked also for me on RPI3 with pi camera 1.3 - thanks!

  • #307 patrick said

    Thank you for the great tutorial. Is there any way I can change the port of the streamer?

  • #308 Miguel Grinberg said

    @patrict: see comment #198.

  • #309 Smera said

    Hi,

    I can view the stream on Raspberry Pi browser (http://localhost:8080) but cannot access the same from another machine in the same network using (http://<IP-address-of-RPi:8080).
    Any idea why?
    Thanks,
    Smera.

  • #310 Miguel Grinberg said

    @Smera: Probably something in your network setup is blocking external access, like maybe a firewall.

  • #311 delta1071 said

    Awesome tutorial, Miguel. I followed your instructions, changed the directory for the output image (pic.jpg) to my "Pictures" folder and voila!! it works flawlessly! However, I don't see any suggestions for improving the framerate. Is there a way to increase them so the stream is less jerky? Thanks again for your efforts in putting together this tutorial.

  • #312 Miguel Grinberg said

    @delta1071: this method is not going to give you great frame rates. It's been a while, but I think I was getting about 8-10 fps using a 640x480 frame size.

  • #313 delta1071 said

    OK. One more question: It seems the max number of lines the terminal can print is 100000, so when the "...skipping frame 'x' to restart at frame 'y'" message appears and y=100000, the stream stops. Is there a way to have the stream run continuously (i.e for hours or days), if desired? Thanks.

  • #314 Miguel Grinberg said

    @delta1071: you can create a wrapper script for raspistill that runs it again when it exits. Note that the problem is not the size of the terminal, raspistill has a timeout, this is the number you give in the -t argument.

  • #315 Kevin said

    "Skipping frame 2271 to restart at frame 390289"

    Why does this keep happening?

  • #316 Miguel Grinberg said

    @Kevin: The raspistill tool is saying that it had to skip a frame. There are many reasons for this, maybe because it can't keep up with the data rate. Try using smaller image sizes.

  • #317 kedar said

    Hi, Thanks for the tutorial, Great it works.

  • #318 Hao said

    Hi
    I've been running into a problem after stopping the streaming by exiting the terminal of camera starting as the program can't be stopped with Crtl + C,I get this error when i try to start the camera again:
    mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
    mmal: camera component couldn't be enabled
    mmal: main: Failed to create camera component
    mmal: Failed to run camera app. Please check for firmware updates
    The camera works after rebooting.
    What's the cause of this?
    Is there any proper way to exit?
    Thanks.

  • #319 reda almorsy said

    thanks for your effort
    would you tel me how can i get images from open cv instead of getting it directly from camera because i want to make face recognition then start streaming

  • #320 Miguel Grinberg said

    @Hao: I don't have a Pi at hand to test this right now, but have you checked if there are any processes still running after you press Ctrl+C? Did you stop raspistill, for example? You may want to manually kill any remaining process, that should free the resources and allow you to restart.

  • #321 Miguel Grinberg said

    @reda: I don't have specific code written for OpenCV, you will need to write a script that does the same job as raspistill, in that it writes jpg files to disk, from where mjpg-streamer can read them.

  • #322 Hardy said

    Thanks a lot .Its working perfectly for me

  • #323 John Paulo Lim said

    when i enter the last code it says "address already use"
    how can i change the port?

  • #324 Miguel Grinberg said

    @John: See comment #198.

  • #325 tom burtonwood said

    mjpg-streamer zip project is being maintained at: https://github.com/jacksonliam/mjpg-streamer

    the source forge page states the project has been abandoned (although the code is still there, rn)

    Also - Miguel - big thank you - your tutorial works great - my next task is to put my raspi on the internet so folks outside my local network can see the stream. i'm thinking about using yaler.net or remot3.it for this.

Leave a Comment