PiEye - webcam streaming in M-JPG format with Raspberry Pi

pieye live videopieye

With Raspberry Pi and a cheap web-camera connected via USB you can do video streaming in just a few simple steps. This solution is based on the mjpg-streamer server application. This solution is very simple as it uses existing streaming technologies.  Everything happnes over HTTP, a standard protocol found everywere and very accesible today.

Practicly, a mini-webserver that runs on Raspberry Pi, serves video as a M-JPG (Motion Jpeg, more info here) file. M-JPG is actually a set of JPG images as frames of a movie. M-JPG's advantage is that it requires no plugins or browser extension to play. A simple HTML IMG tag, with right dimmensions and source attribute set to point to the served M-JPG woudl be enough to show the video in a standard web page.

The whole solution is based on mjpg-streamer server application (you can find it here). The code was written by Tom Stöveken and has GPLv2 licence. I have compiled and prepared a version of this code for Raspberry PI. See below required steps to get a simple & efficient video streaming in a fiew minutes:

  1. Make sure you have an updated version of Raspberry PI's OS.
  2. Install libv4l-0 package, available in Raspbian: sudo aptitude install libv4l-0.
  3. Connect the web camera to USB. The web camera must be Linux compatible; to check this, make sure /dev/video0 file is available on Raspberry PI, else the camera does not have a Linux driver or required extra configuration to work (this issue is not discussed here).
  4. Download mjpg-streamer-rpi.tar.gz archive on Raspberry PI and extract it. Destination folder is not relevant. You don't need root access if you are using the default pi user. Go to mjpg-streamer folder, where you extracted the tar.gz file.
  5. Open mjpg-streamer.sh file; this is a simple bash script to control the mini-webserver. The header contains some writable parameters, as refresh rate or resolution. The default settings should work in most situations.
  6. Start the server with ./mjpg-streamer.sh start command in the current folder.
  7. Runt your prefered web browser and go to http://raspberrypi:8080/?action=stream (where raspberrypi is it's IP address). You should see the image from the webcam. Current version has some issues with Chrome, just use Firefox if the image is not refreshed.
  8. If the system doesn't work, see the mjpg-streamer.log file for debug info.

 

See below required commands for not-Linux geeks (install,download,rename,extract,configure,run):

sudo aptitude install libv4l-0
ls /dev/video0
wget http://www.bobtech.ro/get?download=36:mjpg-streamer-rpi
mv get\?download\=36\:mjpg-streamer-rpi mjpg-streamer-rpi.tar.gz
tar -zxvf mjpg-streamer-rpi.tar.gz
cd mjpg-streamer
sudo nano mjpg-streamer.sh
./mjpg-streamer.sh start

 

Change video parameters

To change video parameters you have to install v4l2-ctl:

sudo apt-get install v4l-utils
v4l2-ctl --list-ctrls
v4l2-ctl --set-ctrl brightness=200
v4l2-ctl --set-ctrl saturation=32

 

Live Demo

pieye live video

I have setup a webcam for live video stream from my aquarium at 5fps. Plants are moved by the water pump jet and a fish or shrimp can be seen from time to time.

Using the M-JPG format you can embedd live video in any web page with just a simple img tag:

<img src="http://raspberrypi:8080/?action=stream" />

Download

UPDATE: 12-11-2012
  • automatic restart after 3 seconds if "error grabing frames"
UPDATE: 30-10-2012
  • added YUV option for webcams without MJPG protocol (default disabled; if you activate it, Raspberry Pi is used to process the MJPG files instead of webcam's CPU)
  • 6 pcs Rasbora (Trigonostigma heteromorpha)
UPDATE: 08-10-2012
  • bugfix for frequent crash (now should be online permanently)
UPDATE: 06-10-2012
  • autorestart when process stops
  • server clients are logged to mjpg-streamer.log
  • support multiple webcams (need to copy script to multiple locations)
  • start/stop messages, exit codes to be used by other scripts
  • 2 pcs Corydoras Nanus
  • 4 pcs Cherry Shrimp (Neocaridina heteropoda)

Where to buy

Below you can find affiliate linked images to buy directly from Amazon. Thank you for supporting this site!

Comments

  • sumache

    Super cool, o sa-mi pun si eu acasa doua!
    Sunt curios daca se pot arhiva imaginile pe NAS-ul synology.

    Posted at 2012-10-25 09:34:21

    Reply

  • sumache

    L-am facut sa mearga, doar ca webcam-ul Logitech C615 se focalizeaza bine doar pana la 1,5m. O sa-i pun o lentila de ochelari cu -1 dioptrii sa se vada clar si mai departe in casa.

    Posted at 2012-11-03 20:29:49

    Reply

  • alex

    superr... merge perfect si se comporta foarte bine....ff bun tutorialul si de folos..celor care nu le merge recomand sa incerce si cu alte camere....;)

    Posted at 2012-12-12 20:08:51

    Reply

  • Dan

    Foarte interesant proiect!! Felicitari!

    O sa imi iau un Raspberry PI curand si o sa incerc cu singuranta acest proiect.

    Imi puteti recomanda un webcam pentru acest proiect? Am un Chicony DC-5110 B dar nu stiu daca e bun. Sau recomandati-mi un webcam cheap&dirty cam s-ar zice :)

    Posted at 2012-12-16 14:43:39

    Reply

    • Radu

      Pentru video de pe aceasta pagina folosesc o camera web Logitech C210, care suporta protocolul MJPG.
      Am mai testat camera web Gembird CAM28U, dar functioneaza numai cu optiunea YUV activata (nu suporta MJPG) si camera web Genius iLook 310, dar nu functioneaza deloc pe RasPi (pe PC este OK).

      Posted at 2012-12-23 19:07:54

      Reply

  • thanos f

    Hi,
    can this software be used with a usb microscope? I am planning to buy one to connect to my Pi to see images with high resolution e.g. when soldering. should i buy a specific microscope with a specific chipset or specification?
    Thank you

    Posted at 2012-12-24 16:01:45

    Reply

    • Radu

      Yes it can, but it's main purpose is to stream video over internet, so in your case it wouldn't make sense. Just use a standard video player for Linux and you will have HDMI output: http://lifehacker.com/5866656/the-best-video-player-for-linux

      Posted at 2012-12-27 09:00:05

      Reply

  • redmund

    failed to start.

    this the log:

    ./mjpg-streamer.sh: line 40: ./mjpg_streamer: cannot execute binary file



    anybody can help me??what is line 40==>>OUTPUT_OPTIONS="-p ${PORT} -w www"
    ?? what will be solutions??
    please help

    Posted at 2013-01-06 19:18:54

    Reply

    • Radu

      you have to make the script executable, like this http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html

      Posted at 2013-01-08 00:20:29

      Reply

  • Dumitru

    Nu inteleg de ce cei de la Raspbian nu au pus in main repo acest pachet de mjpg_streamer. Pana si pe OpenWRT este si mergea pe router fara probleme, evident, la un framerate mai mic, avand in vedere puterea de procesare.

    O chestie legata de mjpg-streamer. Acesta stie user si parola, altfel spus, credentials, in cazul cand vrei sa scoti camera "outside of the LAN", prin port forwarding, de ex.

    "- c username:parola" adaugati la parametrii de output rezolva problema. Se paote schimba si portul pe care sa ruleze webserver-ul de streaming cu "-p port" la fel, in sectiunea de parametri de output.

    Cheers,

    Posted at 2013-01-17 22:52:35

    Reply

  • cross

    i followed the post as it is but what im getting is a green screen when i try to stream with any browser and even in vlc...so you have any idea why this is happening.....?????

    Posted at 2013-01-26 15:01:35

    Reply

    • Radu

      Enable/disable YUV parameter, lower resolution and/or framerate.
      It's also very possible that your camera isn't compatible.

      Posted at 2013-01-26 16:33:40

      Reply

  • cross

    yeah i checkede and and tried with YUV=true bue same still the same problem....????

    Posted at 2013-02-01 16:17:48

    Reply

    • Radu

      then the camera is not compatible :(

      Posted at 2013-02-01 16:44:10

      Reply

  • Sebi

    Se poate face strimul si cu sunet?

    Posted at 2013-02-03 18:29:30

    Reply

  • Eros Nicolau

    Salut,
    Lucrez la un proiect care printre altele are nevoie sa vada remote (printr-un browser) ce vede DSLR-ul - dar in acelasi timp sa si transmita aparatului foto oarece comenzi/setari. Crezi ca ma poti contacta, pe mail (ai adresa, acum) si sa ne auzim la un telefon zilele astea, ca sa ma sfatuiesc un pic cu tine?

    Posted at 2013-02-14 01:19:03

    Reply

    • Radu

      ai mail

      Posted at 2013-02-14 17:35:05

      Reply

  • calvarr

    Aplicatia super ok, multumesc.
    Folosesc raspberry-pi B, cu Microsoft LifeCam
    Idei:
    - dupa ce porneste serverul sa fie afisata in CLI adresa de stream cu care te poti conecta;
    - posibilitatea de stream pe adresa externa:
    (am vazut mai sus un comentariu ca mjpeg-streamer stie sa o faca inclusiv in mod securizat)
    In prezent fac stream extern, nesecurizat in felul urmator:
    - deschid portul TCP in router sau/si firewall;
    - verific daca acesta este deschis:
    $ telnet adresa_externa 8080
    - redirectionez prin tunelizare ssh portul:
    $ ssh nume@adresaexterna -R 8080:127.0.0.1:8080
    - conectare:
    webbrowser http://adresaexterna:8080/?action=stream

    Lucrez la un proiect care foloseste ffserver, sper sa reusesc si eu ceva.

    Posted at 2013-02-20 12:49:45

    Reply

  • vlad

    Care ar fi rezolutia maxima suportata de un raspberry pi, folosind un singur webcam cu rezolutie mare sau mai multe cu rezolutii mai mici?

    Posted at 2013-03-26 06:47:55

    Reply

  • John

    libv41-0 seems not available on the standard immage from the Raspberry PI?

    I recieve the message nopackage will be installed!

    John

    Posted at 2013-04-09 16:07:21

    Reply

  • ciprian

    Am urmat pasii indicati si functioneaza foarte bine.
    Am schimbat din 4 fps in 15 fps si a functionat ca atare.
    Am incercat sa schimb rezolutia in 800x600 dar a ramas tot la 640x480. Cred ca asta este rezolutia maxima a webcam-ului cu care am lucrat. Nu-i bai. Bun si 640x480.
    As vrea totusi sa pot accesa stream-ul si din alte locatii, in afara casei. Raspberry este conectat wireless la router (ASUS WL-530G); documentatia routerului este aici:
    http://ro.asus.com/Networks/Wireless_Routers/WL530g/#download
    Marea mea rugaminte este sa imi spuneti cum ar trebui sa configurez routerului astfel incat sa pot accesa stream-ul din orice locatie (cu acces la internet bine-nteles :) )
    Multumesc anticipat!

    Ciprian

    Posted at 2013-04-29 23:33:45

    Reply

    • ciprian

      gata, s-a rezolvat :D

      Posted at 2013-04-30 23:23:37

      Reply

  • ciprian2

    salut ciprian
    ca sa poti sa accesezi webcam-ul din afara instaleza pe raspberry
    http://www.stuffaboutcode.com/2012/06/raspberry-pi-access-from-internet-using.html

    si iti faci cont pe: http://www.noip.com/ si iti creezi un subdomeniu....eu am facut si am reusit din prima ... o seara buna

    Posted at 2013-04-29 23:45:34

    Reply

    • Radu

      Scuze Ciprian2 ca am activat comentariul tau prea tarziu, a fost pus automat in spam din motive necunoscute :)
      Ciprian (1), in articolul cu Pimotique e descris un alt mod cum poti sa faci asta. Nu e chiar pt routerul tau, dar vad ca te-ai descurcat pana la urma.

      Posted at 2013-05-01 10:37:50

      Reply

  • George

    Salut,

    Foarte bun tutorial dar as aprecia daca cineva mi-ar da un sfat legat de problema pe care o intampin.
    Webcam-ul este un Delux : http://www.ro.all.biz/webcam-delux-dlv-b01-g1234 si vad ca se aprinde led-ul odata ce rulez scriptul dar nu reusesc sa obtin o imagine cand accesez url-ul.
    M-am gandit ca ar putea fi din cauza alimentarii, dar vreau sa stiu si de la voi daca e necesar un hub usb alimentat pentru asa ceva ?

    Va multumesc.

    Posted at 2013-06-04 18:17:16

    Reply

    • George

      Am uitat sa atasez log-ul generat - nu-mi dau seama care ar fi problema :
      http://pastebin.com/uPPfYwzy

      Si desigur camera e detectata corect si nu pare a fi problema de driver : /dev/video0 .

      Posted at 2013-06-04 18:27:07

      Reply

      • George

        Am rezolvat, era ceva de la mine... Multumesc foarte mult pentru ghid, e explicat foarte bine.

        La mai multe pe viitor :D

        Posted at 2013-06-04 19:05:52

        Reply

  • johnny

    la mine nu merge:( nu poate sa dezarhiveze, iar daca dau wget http://www.bobtech.ro/get?download=36:mjpg-streamer-rpi da eroare

    HTTP request sent, awaiting response... 501 Not Implemented
    2013-06-04 17:31:26 ERROR 501: Not Implemented.

    Posted at 2013-06-04 20:37:58

    Reply

    • George

      Asa am patit si eu asa ca l-am downloadat de aici si uploadat pe dropbox la mine si am folosit linkul ala. Nu pot sa iti dau share la link ca nu am voie dar poti sa incerci si tu asa, pana repara autorul...

      Posted at 2013-06-05 15:11:30

      Reply

    • Radu

      Am facut un link direct pentru download aici:
      http://www.bobtech.ro/download/mjpg-streamer-rpi

      Te rog verifica daca functioneaza.
      La mine merge si link-ul existent, nu stiu de ce nu merge la voi, dar n-am timp sa fac debug acum.

      Posted at 2013-06-05 17:38:51

      Reply

    • Radu

      Ai copiat toate comenzile din scritpul de mai sus ?
      Vezi ca dupa download, redenumesc fisierul care incepe cu ?...
      si apoi i-l dezarhivez.

      Posted at 2013-06-05 17:43:05

      Reply

      • johnny

        imi apare error 501 "not implemented" oricare din link-uri l-as folosi

        Posted at 2013-06-06 17:39:43

        Reply

  • Jochen

    Thank you!
    This is exactly what I was looking for!

    michaels Comment is helpful aswell :)

    Posted at 2014-11-17 18:38:53

    Reply

  • KELLER

    Hello.
    I follow your "How to embed". Nice job.
    I have a Raspberry pi2 and raspicam camera OV5647
    Everything works except :
    1) The camera module does not have a native v4l driver (at the time of writing)
    I need to install v4l driver because motioneye does not recognize my raspicam when i use "add camera …".
    2) Autostarting At Boot
    You'll probably want motionEye to start automatically at boot.
    Add the following line to /etc/rc.local (right before the exit 0):
    sudo -u pi sh -c '/home/pi/motioneye/motioneye.py > /home/pi/motioneye/run/motioneye.log 2>&1' &
    I did not add this ligne. This does not work for me.
    Instead, i use cron :
    sudo crontab –e
    Add the following line at the end
    #demarrage de motioneye a chaque reboot
    @reboot /home/pi/motioneye/motioneye.py &
    This will start the server with the user pi and run the process in the background.
    3) I can email me at my gmal account with :
    echo "tata" | mail -s subject mylogin@gmail.com
    but motioneye does not send me any motion notification.
    I think there is a file permission problem.
    Does anyone has an idea ?

    Posted at 2015-05-06 20:23:25

    Reply