Stream Video from the Raspberry Pi Camera to Web Browsers, Even on iOS and Android

Posted by
on under

I've been excited about the Raspberry Pi Camera Module since it was announced last year, so I went and ordered one from Element14 as soon as it came on sale.

I have a few ideas for cool things to build with this camera and I will be blogging about them as I get to develop them. Today, I will show you how to transform the Raspberry Pi into a webcam server. You will be able to watch the video stream from the camera on any device that has a web browser. And yes, this includes the iPad/iPhone and Android devices!

The official streaming method

The introductory article about the camera module in the Raspberry Pi blog shows a method to stream video from the Raspberry Pi to another computer. This method essentially works as follows:

  • On the Pi the raspivid utility is used to encode H.264 video from the camera
  • The video stream is piped to the nc utility, which pushes it out to the network address where the video player is.
  • On the player computer nc receives the stream and pipes it into mplayer to play.

This is an efficient method of streaming video from the Pi to another computer, but it has a few problems:

  • The Raspberry Pi needs to know the address of the computer that is playing the video
  • The playing computer needs to have an advanced player that can play a raw H.264 video stream. No mobile device that I know can do this, for example.
  • Since this system relies on a direct connection between the Pi and the player, it is impossible to have the player computer connect and/or disconnect from the stream, the connection needs to be on at all times.
  • What if there are two or three concurrent players? Things get awfully complicated for the Pi.

This ad hoc solution that the Raspberry Pi Camera team proposes isn't that useful to me, so I went to search for better options.

Streaming protocols

I think an important requirement for a streaming camera is that you can view it with ease. To me, this means that the stream should be playable from a web browser. Having to run a custom player is a complication, and puts it out of reach of most mobile devices.

There are a few modern streaming protocols for web browsers out there. For example, HLS is Apple's choice, so it has great support on iDevices but not much elsewhere. Another one, called Fragmented MP4 is supported by Adobe and Microsoft, but requires browser plugins from these companies on the player computer, so Windows and Mac computers can do it, but Linux and mobile cannot. HTML5 video is also based on the MP4 format but support is not that great.

Besides, for all the streaming protocols listed above there is a need to have a streaming server that prepares the video for streaming by segmenting it and packaging it, and while there are several open source utilities that can do this for a static video stream, I haven't found any that can do it on a live stream. Note: I have been corrected on this statement, more recent releases of ffmpeg than the binary available for Raspbian can generate an HLS live stream.

So what other options are there?

Motion JPEG to the rescue

I then investigated how IP webcams do it, and a lot of them use an older streaming protocol called Motion JPEG or MJPEG.

What is Motion JPEG? Pretty simple, it's just a stream of individual JPEG pictures, one after another. I was surprised to find that most modern browsers can play MJPEG streams natively.

The down side of MJPEG streams is that they are not as efficient as H.264, which greatly improves quality and reduces size by encoding only the differences from one frame to the next. With MJPEG each frame is encoded as an entire JPEG picture. For my needs this isn't a concern, though.

Continuing with my research I stumbled upon MJPG-streamer, a small open source MJPEG streaming server written in C that I was easily able to compile for the Raspberry Pi.

The following sections describe how I've used this tool to create a very flexible, play anywhere, streaming server for my Raspberry Pi camera.

Installing MJPEG-streamer

UPDATE: This section is outdated. Please use the instructions on my updated guide to build and install MJPG-Streamer.

Unfortunately there isn't a package for MJPEG-streamer that can be installed with apt-get, so it needs to be compiled from source.

MJPEG-streamer is hosted at, so head over to the project's download page to get the source tarball.

To compile this application I used the following commands:

$ sudo apt-get install libjpeg8-dev
$ sudo apt-get install imagemagick
$ tar xvzf mjpg-streamer-r63.tar.gz
$ cd mjpg-streamer-r63
$ make

This tool requires libjpeg and the convert tool from ImageMagick, so I had to install those as well.

The makefile does not include an installer, if you want to have this utility properly installed you will need to copy the mjpg_streamer and its plugins input_*.so and output_*.so to a directory that is in the path, like /usr/local/bin. It is also possible to run this tool directly from the build directory.

Setting up the JPEG source stream

The streaming server needs a sequence of JPEG files to stream, and for this we are going to use the raspistill utility that is part of Raspbian. For those that are concerned about performance, keep in mind that the JPEG encoder used by raspistill runs in the GPU, the load required to generate JPEGs is pretty small.

To setup a constant stream of JPEG images the command is as follows:

$ mkdir /tmp/stream
$ raspistill -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &

Let's go over the arguments to raspistill one by one:

  • -w sets the image width. For an HD stream use 1920 here.
  • -h sets the image height. For an HD stream use 1080 here.
  • -q sets the JPEG quality level, from 0 to 100. I use a pretty low quality, better quality generates bigger pictures, which reduces the frame rate.
  • -o sets the output filename for the JPEG pictures. I'm sending them to a temp directory. The same file will be rewritten with updated pictures.
  • -tl sets the timelapse interval, in milliseconds. With a value of 100 you get 10 frames per second.
  • -t sets the time the program will run. I put a large number here, that amounts to about two hours of run time.
  • -th sets the thumbnail picture options. Since I want the pictures to be as small as possible I disabled the thumbnails by setting everything to zero.
  • & puts the application to run in the background.

Starting the streaming server

Okay, so now we have a background task that is writing JPEGs from the camera at a rate of ten per second. All that is left is to start the streaming server. Assuming you are running it from the build directory the command is as follows:

$ LD_LIBRARY_PATH=./ ./mjpg_streamer -i " -f /tmp/stream -n pic.jpg" -o " -w ./www"

Let's break this command down to understand it:

  • LD_LIBRARY_PATH sets the path for dynamic link libraries to the current directory. This is so that the application can find the plugins, which are in the same directory.
  • -i sets the input plugin. We are using a plugin called This plugin watches a directory and any time it detects a JPEG file was written to it it streams that file. The folder and file to watch are given as the -f and -n arguments.
  • -o sets the output plugin. We are using the HTTP streaming plugin, which starts a web server that we can connect to to watch the video. The root directory of the web server is given as the -w argument. We will use the default web pages that come with the application for now, these can be changed and customized as necessary.

Watching the stream

Now everything is ready. Go to any device that has a web browser and connect to the following website:


Where IP-address is the IP address or hostname of your Raspberry Pi.

The default website served by the streaming server provides access to several players to watch the stream. I've found that the "Stream" option worked on most devices I tried. For a few that "Stream" didn't show video I went to "Javascript" and I was able to play the video just fine.

I tested playback of the stream from an iPad, an Android smartphone and a variety of web browsers on Windows and OS X, and I was able to play the stream in all of them.

I hope you find this method useful. Let me know in the comments below if you have a different method of streaming.


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!

  • #51 Luis said

    ERROR: could not find input plugin
    Perhaps you want to adjust the search path with:
    # export LD_LIBRARY_PATH=/path/to/plugin/folder
    dlopen: cannot open shared object file: No such file or directory

    I check the download packet and there is no makefile to compile

  • #52 William said

    This works great, but I did find one problem. If you only specify the folder for mjpg_streamer, it can see the temporary file created by raspistill, then try to load it, which fails if the file has been deleted or renamed. I got frequent "file not found" errors. The solution is to also specify the file name filter using "-n":
    ./mjpg_streamer -i " -f tmp -n pic.jpg" -o " -w ./www"

  • #53 srinivas said

    hai sir,
    how to upload images to ftp server

  • #54 Stefan said

    If you get "could not open file for reading: No such file or directory", use the following:
    LD_LIBRARY_PATH=./:./plugins/:./plugins/input_file/:./plugins/output_http/ ./mjpg_streamer -i " -f /tmp/stream/" -o " -w ./www"

  • #55 Aruzhu said

    pi@thatraspberry /mnt/usb/realstream/mjpg-streamer/mjpg-streamer $ LD_LIBRARY_PATH=./ ./mjpg_streamer -i " -f /tmp/stream" -o " -w ./www"
    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..: -no filter for certain filename set-
    o: www-folder-path...: ./www/
    o: HTTP TCP port.....: 8080
    o: username:password.: disabled
    o: commands..........: enabled
    could not open file for reading: No such file or directory

    if i do dir
    CHANGELOG Makefile scripts utils.o mjpg_streamer www mjpg_streamer.c TODO mjpg_streamer.h plugins utils.c
    LICENSE mjpg_streamer.o README utils.h

  • #56 Niall said

    I too am not getting anything close to 10fps, more like 1, and the raspistill process runs about 10 times longer than specified.

    Also, I think the web connection is not very robust. I need to load or reload the page and start the streamer in some specific order (which Im not sure of), otherwise the server does not return anything to the client. Haven't quite figured that bit out yet...

  • #57 Miguel Grinberg said

    To all those that are having issues building MJPG-Streamer: I have finally took the time to revise the build and installation instructions. I have written a new article with that information here:

  • #58 Alexandra said


    I followed the howto... I do have an error at the last step...

    pi@raspberrypi /opt/mjpg-streamer $ LD_LIBRARY_PATH=./ ./mjpg_streamer -i " -f /tmp/stream" -o " -p 9000 -w ./www" &
    [1] 5654
    pi@raspberrypi /opt/mjpg-streamer $ MJPG Streamer Version: svn rev: 3:172
    i: folder to watch...: /tmp/stream/
    i: forced delay......: 0
    i: delete file.......: no, do not delete
    i: filename must be..: -no filter for certain filename set-
    o: www-folder-path...: ./www/
    o: HTTP TCP port.....: 9000
    o: username:password.: disabled
    o: commands..........: enabled
    could not open file for reading: No such file or directory

    and the http page does not shows the picture... like red

    pic.jpg is really an image...I check it.
    but the page show me the mpg-streamer demo page, but the image is blank.
    what could be the problem?

  • #59 Miguel Grinberg said

    @Alexandra: see the updated article referenced right above your comment. For some people it is necessary to add the -n option as indicated in the new article.

  • #60 Orlando Security Officer said

    Stream" option worked on most devices I tried. For a few that "Stream" didn't show video I went to "Javascript" and I was able to play the video just fine.

  • #61 Peter Harrison said

    Does the other computer have to be on the same network? This didn't work for me but I'm guessing it's because I was attempting to do it online from another network.

  • #62 Miguel Grinberg said

    @Peter: The two computers must be on the same network, or else you have to ensure that the routing is setup for the two computers to be able to talk. If you have the RPi at home behind a router then you may need to configure your router to allow port 80 access to the RPi from the outside.

  • #63 Thomas said

    Would the raspberry pi be able to display the video feed at the same time?

  • #64 Miguel Grinberg said

    @Thomas: if you want to display live video within the same RPi then it is more efficient to avoid the streaming and just let raspistill show a preview.

  • #65 Bedfford said

    thanks for you info. very usefull!

    My raspicam work smooth and fine using mjpg-streamer.

    But, I wonder if you know a methos to move the www folder without loose the javascript function.

    I tried but the stream under javascript page is missing.


  • #66 Miguel Grinberg said

    @Bedfford: It should work after moving the www folder, I think. Did you look in your browser's Javascript console to see what the error is?

  • #67 Eddy said

    I'm using the same process : it works well but I have a big problem : it is always writing on the SD card the file (jpg) captured by the camera before used by the stream server. And after some weeks, the SD card has some bad sectors and crashes. So is there a way to have the same result without writing continuously the file on the SD card ???


  • #68 Miguel Grinberg said

    @Eddy: Sure, you can create a ramdisk and write the jpgs there instead. Google "raspberry pi ramdisk", there are many tutorials.

  • #69 seif said

    can I use a web cam instead of the camera module using mjpeg ? thank you in advance

  • #70 Miguel Grinberg said

    @seif: not directly, you will need to find a tool that is equivalent to raspistill.

  • #71 Trey Smith said

    Is there a way to have ONLY the video showing on port 8081? I'm trying to make an android app that shows a webview of my streaming video and I don't need the panel on the side. I only want the 'javascript' format of my streaming video.

  • #72 Miguel Grinberg said

    @Trey: Sure, you can just use the URL that points to the jpg stream.

  • #73 Amina said

    I am having issues with the stream running in the background. It takes up 75% of the screen when I run the raspistill command before starting the streaming server. How do I prevent this?

  • #74 Miguel Grinberg said

    @Amina: Try adding --nopreview to the raspistill command.

  • #75 Amine said


    i try to make the same application on ZedBoard, with Xillinux ( based on ubunty 12.04)
    but always , error of installation , : depends : .....

    any suggestion

    thank you

Leave a Comment