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

    @Bex: that "mmal" error never happened to me, so I do not know what causes it, but I suspect is load related. Try using a larger number in the -tl argument to reduce the fps (for example, try 250 for 4fps).

    Note that my new streaming app does not use raspistill, but it depends on the same system libraries raspistill uses, so I expect you will still get the error if all other conditions are the same.

  • #152 Fenlig said

    Hey Guys,

    Im getting this on the last step:

    MJPG Streamer Version: svn rev:
    i: folder to watch...: /tmp/stream/
    i: forced delay......: 0
    i: delete file.......: no, do not delete
    i: filename must be..: pic.jpg
    o: www-folder-path...: /usr/local/www/
    o: HTTP TCP port.....: 8080
    o: username:password.: disabled
    o: commands..........: enabled
    bind: Address already in use
    o: server_thread(): bind(8080) failed

    When I run: lsof -i TCP | grep 8080
    I get nothing returned.

    Any idea?

  • #153 Tony D'Angelo said

    Hello, and thank you in advance for your help.
    I am working on the cam rover project as outlined in magpie magazine. I am trying to install
    mjpg-streamer
    However, when I run the “make” command I get a fatal error: “videodev.h no such file no such directory. “ I have tried all the fixes that I have found as well as asked for help on the raspberry Pi home page and the source forge, Tom says he recalls the error. But not the fix, I have gone so fare as to down load a new O.S. run the updates and the upgrades and try the install and it still crashed with the same error at the same spot. I am thinking either I am doing something dumb or they have up graded there Software? I have also tried to install tried to install the mjpg-streamer_94-i386.deb but I can’t seem to get that to extract correctly. (grrrrrr) the link in the article points to the r63 file that dies on the “make’ command.
    Thank you all very much.
    Tony

  • #154 Tony D'Angelo said

    I can not thank you enough for this page! I have spent the last two days drinking black coffee, trying to get this working!
    U R DA MAN!

  • #155 Miguel Grinberg said

    @Tony: you missed step 2 above.

  • #156 Dodutils said

    Compile worked fine.

    But I do have a question, I actually have the RaspiCam and also an old Philips ToUCam Pro II connected on USB port.

    How do I use it with ToUCam instaed of RaspiCam ?

  • #157 Sandipan Mandal said

    How to access this streamed video from opencv using remote pc?
    thanx...

  • #158 KrusjMe said

    Thank you very much. Works like a charm!

  • #159 Erik said

    For my domica i need to link to a file, could you tell me to which i can link ?
    http://www.domoticz.com/wiki/Camera_Setup
    Id like to link to a video stream and image.

  • #160 jacob fowler said

    pi@raspberrypi ~ $ sudo ln -s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h
    ln: failed to create symbolic link `/usr/include/linux/videodev.h': File exists
    pi@raspberrypi ~ $

    i keep getting this error when i try to do step 2 of this tutorial any thoughts?????

  • #161 Miguel Grinberg said

    @jacob: that command will only work the first time, if you run it again it will give you an error. Just move on to step 3, you will be fine.

  • #162 Tony said

    Hello,
    I am looking for some guidance with Mjpg streamer on the raspberry Pi computer.
    I have been working on a rover cam project for some time now and I have it almost worked out. I am down to my last bug. I am running mjpg-streamer experimental and have my web page up and running with the stream. I have 7 .sh files that I use to control relays via the gpio pins. My problem is every time I use a button on my web page the stream blanks out. It will come right back on a refresh or reload. Is there some Html or bash coed I can add to fix this, I have asked this question before but the answers I get either are so vague that they are useless or so pointed they go right over my head.
    I am very new to this and have only been at it a few weeks.
    Best regards.
    Tony

  • #163 Miguel Grinberg said

    @Tony: check your browser's console to see what error do you have. If you see a XSS (cross-site scripting) error, then you may be able to solve this by adding a X-XSS-Protection header.

  • #164 Azaleo said

    hello,

    I have my rPi but I want to stream the jpeg files from a network its another windows PC ; the jpegs are from automatic screenshot of that windows PC.

    How can I use my rPi for this case?

    I hope you can help with this.

    Regards
    Leo

  • #165 Tom said

    The last comment was made 10 months ago but I'll add this in case anyone else finds this page (like I did). I am running Pidora 20 and raspistill version 1.3.8. My cameraand raspistill worked but mjpeg_streamer did not. I discovered that when raspistill writes a new version of pic.jpg it actually writes it first to a temporary file named pic.jpg~ which mjpg-streamer sees as the new file (instead of pic.jpg which we told it to watch for). Because I already had the source code for mjpg-streamer, I took a look at input_file.c down in the plugins/input_file subdirectory. I got things working by chopping off the ~ from pic.jpg~ by adding new line 251 as shown below. I also added a one millisecond delay (ine 260) to give raspistill time to replace pic.jpg. Finally I replaced the break (line 267) with a continue statement to keep the tread alive just in case.

    After mkaing the changes I removed input_file.so from the mjpg-streamer and ran "make input_file.so"

    There is no command line option to make mjpg_streamer run in verbose mode but you can build it with debugging messages by uncommenting the "CFLAGS += -DDEBUG" in the Makefile.


    250 / prepare filename /
    251 ev->name[strlen(ev->name) - 1] = '\0';
    252 snprintf(buffer, sizeof(buffer), "%s%s", folder, ev->name);
    253
    254 / check if the filename matches specified parameter (if given) /
    255 if((filename != NULL) && (strcmp(filename, ev->name) != 0)) {
    256 DBG("ignoring this change (specified filename %s' does not match%s')\n", filename, ev->name);
    257 continue;
    258 }
    259 DBG("new file detected: %s\n", buffer);
    260 usleep(1000);
    261
    262 / open file for reading /
    263 rc = file = open(buffer, O_RDONLY);
    264 if(rc == -1) {
    265 perror("could not open file for reading");
    266 //break;
    267 continue;
    268 }

  • #166 Vladimir said

    Than you, I had lots of fun trying this, it works perfectly!

  • #167 sana siraj ahmed shah said

    i just want to know how can my webcam give laive stream on browser?? for now MPEG Streamer just showing congratulation page on browser, why?

  • #168 Ian said

    How can you stop the "skip frame XXX to restart..." from looping while running?
    I can't really do anything else with the pi in this state.

    thanks

  • #169 Miguel Grinberg said

    @Ian: are you doing other work on your Pi? The Pi can't keep up with your frames. You may want to try lowering the framerate.

  • #170 Quinn Keaveney said

    So now how do I watch it from a computer from an external network?

  • #171 Miguel Grinberg said

    @Quinn: if you are behind a firewall or router, then you will need to implement port forwarding mechanism to allow external computers to reach the mjpg-streamer server. This will depend on your specific situation, consult your firewall and/or router documentation to learn how to enable access to internal services from the outside.

  • #172 Tom said

    Sorry if I missed it, but can this use the motion sensor be utilized to record video?

  • #173 Marcus said

    Hey Miguel
    i just wanted to start by saying ur instructions are awesome especially for someone who is really new to programming.

    I was just wondering is there any way to reduce the lag. I plan on making a wifi controlled robot and the lag is just a bit too much for me to use properly?

  • #174 Miguel Grinberg said

    @Marcus: use a ramdisk to save the pictures, reduce the image width/height, increase compression. All these will make processing lighter and increase frame rate.

  • #175 Miguel Grinberg said

    @Tom: the Pi camera does not have a motion sensor, this is something that is typically implemented in software. So no, with this setup you cannot do that.

Leave a Comment