Low Latency (~0.4 s) Video Streaming From Raspberry Pi Using Mjpeg-Streamer and OpenCV

By Petr Kout, August 24, 2014

Low Latency (~0.4 s) Video Streaming From Raspberry Pi Using Mjpeg-Streamer and OpenCVThere are multiple ways to stream video from Raspberry Pi (RPi) to another computer via the wired or wifi ethernet. After trying multiple methods, stumbled upon one that leads to minimal latency and works really well over a wifi connection. The solution is the combination of mjpeg-streamer on the RPi and OpenCV client program on the other computer. In this tutorial, I am working with linux Ubuntu as the client computer. Let’s get to it! First, we will install the RPi software, then receiving computer software.

RPi Software Installation

We will not use the standard mjpeg-streamer package because that one doesn’t have a built-in support for the RPi camera. Instead, we’ll use this guy’s great fork of mjpeg-streamer: It allows streaming video frames directly from your RPi camera, which is very efficient as well as convenient. So, let’s install it:

Log into your RPi and go to /usr/src/ and create a directory mjpeg-streamer there:

cd /usr/src
sudo mkdir mjpg-streamer
sudo chown `whoami`:users mjpg-streamer
cd mjpg-streamer

Now clone the mjpeg-streamer from the github repository there:

git clone .

In order to compile the code, we’ll need to install some library dependencies: apt-get install libv4l-dev libjpeg8-dev imagemagick build-essential cmake subversion Next, we’ll need to compile the mjpeg-streamer. Enter:

cd mjpg-streamer-experimental

Now we should be set to start streaming the video. There are many options you can set. For details, visit the GitHub page linked above and look at the readme page. Here, we will do a simple example of streaming of 640×480 resolution video at 20 frames per second. If you lower the resolution, the latency will get smaller.

./mjpg_streamer -o " -w ./www" -i " -x 640 -y 480 -fps 20 -ex night"

The export LD_LIBRARY_PATH variable sets the current directory as a path where programs should look for libraries. Our program uses and libararies found in the current directory, which is why we added that directory to LD_LIBRARY_PATH.

That’s it! Mjpg-streamer will now stream the video to the port 8080 on this RPi. We will access the streamer from the other computer over the network and display it.

Client Computer Software Installation

Note down the RPi’s IP address as we will need it to access the stream on its port 8080. Before we do so, we’ll need to install Python and OpenCV, if they don’t exist on your system yet.

  • Install Python and OpenCV:

    sudo apt-get install python python-opencv python-numpy
  • Now, you have two options how to view the stream from the RPi. Either you open a browser and enter your RPi’s IP address followed by 8080 (e.g. This will load a web page generated by the RPi. You can then click on the Stream tab and see the video stream right there. Alternatively, and those who want to further process the video will prefer this, you can create a file and paste the script below into it to get a video stream from the RPi and display it using OpenCV.


    Paste this into the file and save it.

    import cv2
    import urllib
    import numpy as np
    while True:
        a = bytes.find('\xff\xd8')
        b = bytes.find('\xff\xd9')
        if a !=- 1 and b != -1:
            jpg = bytes[a:b+2]
            bytes= bytes[b+2:]
            i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
            if cv2.waitKey(1) == 27:


    Don’t forget to change the IP address in the script with your RPI’s IP address.

  • Finally with the mjpg-streamer running on the RPi, execute the above Python script on the computer where you want to see the video:


    You should now see a window open and start streaming the video with a latency 0.5 seconds or less.

BIG THANK YOU to jacksonliam user on GitHub for doing this work! You’ve done an amazing job!