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 11,249 times, 8 visits today)
  • johnny

    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?

    • anonymous

      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?

      • johnny

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

  • Dipto Pratyaksa

    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”

  • Dipto Pratyaksa

    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”

  • I Jones

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

  • brystr0

    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

  • andlinux

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

  • rosenw

    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?

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

  • utasek

    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

  • JT

    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.

  • Carl

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

  • fish

    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

    • Mario

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

  • Abhijeet Ramgir

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

    any walkthrough which will work??

    Thanks!

  • hk

    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…

    • Dipto Pratyaksa

      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.

  • Pingback: Using MJPG-Streamer on Raspberry Pi for live streaming using webcam | ALiF()

  • ambar

    I use raspbian jessi whether it can

  • Dipto Pratyaksa

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

  • Dipto Pratyaksa

    I think you can.

  • Dipto Pratyaksa

    What cam did you use?

  • Dipto Pratyaksa

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

  • Dipto Pratyaksa

    Which cameras are we comparing here?