Faster Video Streaming on Raspberry Media Server with MJPG STREAMER

MJPG Streamer is a simple and straight forward video streamer, but it is faster than most streamers, so this may be ideal for your remote control projects in which real-time video feed is crucial for navigation and orientation purposes.

Installing MPJG Streamer on Raspberry Pi

On Raspbian (wheezy), here is how you can compile it. First, load some packages you will need.

apt-get install subversion
apt-get install libv4l-dev
apt-get install libjpeg8-dev
apt-get install imagemagick

Subversion is software for downloading versioned software, it is used a lot by Open Source developers.
libv4l-dev and libjpeg8-dev are required dependencies.
ImageMagick will be needed for the installation, since it is used to process some image files during the compilation.
Now to do the compilation (as root, or sudo):

svn co https://svn.code.sf.net/p/mjpg-streamer/code/
cd mjpg-streamer/mjpg-streamer
make USE_LIBV4L2=true clean all
make DESTDIR=/usr install

From within ~/mjpg-streamer/mjpg-streamer folder, run command the following to run it:

mjpg_streamer -i "/usr/lib/input_uvc.so" -o "/usr/lib/output_http.so -w ./www"

Test it on local browser: http://localhost:8080.

That should do it.

Advanced Configurations

To run mjpg-streamer with advanced options, make sure no other processes are tying up the webcam, then it’s something like this:

mjpg_streamer -i "/usr/lib/input_uvc.so -d /dev/video0  -r 640x480 -f 1" -o "/usr/lib/output_http.so -p 8090 -w /var/www/mjpg_streamer"

Input modifiers:
-d specifies the device
-r is resolution
-f is frame rate, in number of frame per second
-y specifies YUYV format, rather than MJPEG format.

Output modifiers:
-p is port
-w is web serving directory.
See the documentation for more details.

To run it during boot:

sudo nano /usr/sbin/webcam.sh

mjpg_streamer -i "/usr/lib/input_uvc.so -d /dev/video0  -r 640x480 -f 1" -o "/usr/lib/output_http.so -p 8090 -w /var/www/mjpg_streamer"

Save the file and give it exec permission

sudo chmod 755 /usr/sbin/webcam.sh

Create a link to global applications folder so you can run it from any folder

sudo ln -s /usr/sbin/webcam.sh /etc/init.d/webcam.sh

Make sure it gets executed during boot

update-rc.d webcam.sh defaults 94 6

 

Mjpg-streamer includes a lightweight web server, so you will not need to run apache or lighttpd, but you can embed the streaming video into a web page, and for that you need a proper web server.

A typical line in the html file for displaying an image stream looks like this

<img src="http://10.0.0.38:8090/?action=stream" width="752">

The port here should match the port specified in the mjpg-streamer command.

A simple web page should look like, the following. Create the file and save it in a folder where the web server can recognise, by default:

sudo nano /var/www/video.html

<html>

   <head><title>MJPG Streamer</title></head>

   <body>
      <h1>MJPG video of ELMTREE Lounge Room</h1>
      <img src=”http://10.0.0.38:8090/?action=stream” width=”752″>
      <br><a href=”http://10.0.0.38:8090/?action=stream”>View</a>

  <body>
</html>

To access from a remote client, simply enter the web page in your browser:

http://10.0.0.38/video.html

And… BANG! There she moves!

Additional Notes

Some webcams will deliver MJPEG images. Mjpg-streamer is very efficient with these webcams, as it just reads the images and streams them to the web. CPU usage of ~ 1% with a 640×360 resolution MJPEG image, which is pretty efficient

If you camera does not deliver MJPEG, you can probably still read it with the YUYV format, but there is more CPU overhead, because the software must read the YUYV and convert it to MJPEG in order to stream it.

If you don’t know what formats your webcam delivers, try experimenting with fswebcam in “–verbose” mode. Fswebcam to very useful for characterizing cameras, and figuring out which resolutions they will serve. Fswebcam reads an image from a webcam and writes it to a file. You can then see under what conditions it failed, and whether the files are garbled or not. (Files may be garbled if the resolution is somehow screwed up.) Fswebcam is available on both Arch and Wheezy as a package.

The best alternative to mjpg-streamer seems to be motion coupled with a web server like apache or lighttpd. Motion is very sophisticated software intended for surveillance video. Try configuring by turning off options that are  not being used.

On wheezy, motion’s handling of MJPEG format from the webcam may be broken. That’s a minor annoyance. MJPEG is at least 20 times more efficient in terms of size and bandwidth than YUYV, so if you want to go to higher resolution, MJPEG may work where YUYV fails.

(Visited 17,653 times, 1 visits today)

27 thoughts on “Faster Video Streaming on Raspberry Media Server with MJPG STREAMER”

  1. johnny says:

    i-m gettin an error on
    mjpg_streamer -i “/usr/lib/input_uvc.so -d /dev/video0 -r 640×480 -f 1” -o “/usr/lib/output_http.so -p 8090 -w /var/www/mjpg_streamer”

    Init v4L2 failed !! exit fatal
    i: init_VideoIn failed
    what shall i do?

    1. anonymous says:

      Johnny, If you have a V4L device you may need addition drivers. If not you may need to use the -y (YUYV) option. Do you see your camera with lsusb?

      1. johnny says:

        ok, i done that, but now after i open nano and isert the imput modifiers nothing happens… i changed the ip still nothing…

  2. Dipto Pratyaksa Dipto Pratyaksa says:

    You need to install ffmpeg to read mpeg files, or use the YUV as below:

    sudo mjpg_streamer -i “/usr/lib/input_uvc.so -d /dev/video0 -f 10 -y” -o “/usr/lib/output_http.so -p 8090 -w /var/www/mjpg_streamer”

  3. Dipto Pratyaksa Dipto Pratyaksa says:

    You need to install ffmpeg to read mpeg files, or use the -y parameter to set YUV as below:

    sudo mjpg_streamer -i “/usr/lib/input_uvc.so -d /dev/video0 -f 10 -y” -o “/usr/lib/output_http.so -p 8090 -w /var/www/mjpg_streamer”

  4. I Jones says:

    getting errors when i run the command, i: error grabbing frames every time i run it…any thoughts?

  5. brystr0 says:

    Good write-up, but the SVN repo location has changed to:

    svn checkout svn://svn.code.sf.net/p/mjpg-streamer/code/ mjpg-streamer-code

  6. andlinux says:

    Great, thanks to your text I’m able to see my images on my webiopi webpage 🙂

  7. rosenw says:

    I have ffmpeg installed and it doesn’t work unless I use the -y option. Is there any way to get it to deliver MJPEG images rather than using YUYV?

    1. ffmpeg supports both MJPEG (no additional parameter) and YUYV by adding -y parameter into the command line. Can you specify the error?

  8. utasek says:

    Hi
    I use mjpeg streamer with my usb cam for long time and its great, my question: is it possible to adjust it for 2 usb cams??
    Regards
    Utasek

  9. JT says:

    What framerate are you getting with such a low processor use? What’s the limitation to an even higher rate? If USB 2.0 allows 30MB/s, then you should be able to stream 720p at over 30 fps.

  10. Carl says:

    You can also se what resolution and fps your webcam support by writing: v4l2-ctl –list-formats-ext

  11. fish says:

    Isn’t it bad to stream a MJEG on to /var/www whichh is situated on the SD-Card.
    SD is a flash storage and this will get damaged fast with f.e. 25fps

    1. Mario says:

      That is fine if you have class 10 SDHC SD Card that is capable of high writing speed.

  12. Abhijeet Ramgir says:

    Went through the tutorial but getting a blank screen, installed the patch and used YUV

    any walkthrough which will work??

    Thanks!

  13. hk says:

    Hello Sir If i have already a Motion software on Raspberry pi,and i want to install mjpg streamer can i install mjpg also on raspberry pi or there will conflict occur between them…

    1. Dipto Pratyaksa Dipto Pratyaksa says:

      You can. Just dont run both at the same time, cause I think both shared the same resources if you use the same webcam for both.

  14. ambar says:

    I use raspbian jessi whether it can

    1. Dipto Pratyaksa Dipto Pratyaksa says:

      No problem. Just try it, there could be different packages, but they should be similar

    2. ambar says:

      after I install my mjpegstreamer sudo make install clean all USE_LIBV4L2 = true what went wrong https://uploads.disquscdn.com/images/eaa76014465b7af514f861e4a4a9da8b293dc037351c5603b48d491d1b313ad8.jpg

  15. Dipto Pratyaksa Dipto Pratyaksa says:

    This worked for older Raspbian running on Pi 1 and 2. Have you tried on Pi 3?

  16. Dipto Pratyaksa Dipto Pratyaksa says:

    I think you can.

  17. Dipto Pratyaksa Dipto Pratyaksa says:

    What cam did you use?

  18. Dipto Pratyaksa Dipto Pratyaksa says:

    good to hear that! Can you share your project link here?

  19. Dipto Pratyaksa Dipto Pratyaksa says:

    Which cameras are we comparing here?

Comments are closed.