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
  • #326 munish said

    thanks alot sir you really helped alot

  • #327 MARIE Arthur said

    it seam that the direct link to download the archive is http://sourceforge.net/code-snapshots/svn/m/mj/mjpg-streamer/code/mjpg-streamer-code-r182.zip (there is a "r" just before the number ;)

  • #328 Anthony Pacuraru said

    I am experimenting with mjpg-streamer. I am streaming video in HD and am noticing some latency as well as the image is not truly HD. I could use some guidance on how to obtain real-time hi-res video (h.264 format). Ideally, I will be able to source the Raspberry video feed into my Geovision Recording Server.

  • #329 Miguel Grinberg said

    @Anthony: if you are interesting in h.264 encoding then you are looking at the wrong project. Motion jpeg is a completely different encoding method, nothing to do with H.264. You may want to look at the HLS encoding.

  • #330 Sai Esvar said

    I recently tried installing ffmpg (unsucessfully), Now i am reinstalling mjpg-streamer, and it gives an error saying "Address already in use".
    Please help me how to rectify it. Im very new to pi

  • #331 Miguel Grinberg said

    @Sai: address already in use means that you have some other application using the same network port. Try rebooting the Pi, maybe that'll help. You may also need to uninstall other things that you have running in there that you don't need.

  • #332 suresh said

    hello all,
    i got below error while running the $ make mjpg_streamer input_file.so output_http.so

    pi@raspberrypi:~/mjpg-streamer-code-r182/mjpg-streamer $ make mjpg_streamer input_file.so output_http.so
    make: svnversion: Command not found
    gcc -D'SVN_REV=""' -O2 -DLINUX -D_GNU_SOURCE -Wall -c -o utils.o utils.c
    In file included from /usr/include/arm-linux-gnueabihf/sys/stat.h:446,
    from utils.c:33:
    /usr/include/arm-linux-gnueabihf/bits/statx.h:25:8: error: redefinition of ‘struct statx_timestamp’
    struct statx_timestamp
    ^~~~~~~~~~~~~~~
    In file included from utils.c:32:
    /usr/include/linux/stat.h:56:8: note: originally defined here
    struct statx_timestamp {
    ^~~~~~~~~~~~~~~
    In file included from /usr/include/arm-linux-gnueabihf/sys/stat.h:446,
    from utils.c:33:
    /usr/include/arm-linux-gnueabihf/bits/statx.h:36:8: error: redefinition of ‘struct statx’
    struct statx
    ^~~~~
    In file included from utils.c:32:
    /usr/include/linux/stat.h:99:8: note: originally defined here
    struct statx {
    ^~~~~
    make: *** [<builtin>: utils.o] Error 1

    Thanks

  • #333 Miguel Grinberg said

    @suresh: this is a 7 year old article, things may have changed since then, possibly in the C or C++ libraries in your compiler. You seem to have a duplicated symbol in the stat.h file in your compiler, this isn't a problem with the motion jpeg code.

  • #334 Alan said

    Hi

    This is a wonderful tutorial! The streaming works perfectly. I was wondering if it is possible to host the stream on your own web server like apache with a different interface.

    Thanks

  • #335 Miguel Grinberg said

    @Alan: you can create your own page and host it on your server, sure. Just make sure you use the correct URL to the video stream in the <img> tag.

  • #336 Aj said

    HI there I've got some very stupid questions but also know that i am super new to this. Q1 where do i type in this info you provided above? Q2 does this work with any type of web camera?
    So i have a Raspberry PI 4 which I run klipper on for my 2 3D printers and i would like to use the camera(live Stream) function so i can check up on my printers while I'm in a different room. Unfortunately i do not have money to buy a legit Raspberry PI camera.

  • #337 Miguel Grinberg said

    @Aj: First of all, this isn't a "plug and play" project that you install and run, so it is going to be tough if you do not have programming experience. Q1. in the terminal app for your OS. Q2. the project works for supported cameras only. You can write new drivers to add support for other camera models.

  • #338 Jaime said

    This tutorial helped me. I got the same error as @suresh
    I found the solution here: https://github.com/sunfounder/Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi/issues/44#issuecomment-632845253

    you need to comment lines in mjpg-streamer/utils.c line 32 & 33 :
    //#include <linux/stat.h>
    //#include <sys/stat.h>

    Then
    make clean all

Leave a Comment