element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Stream live videos from your Pi
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Raspberry Pi requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: rados-dp
  • Date Created: 26 Aug 2016 12:09 PM Date Created
  • Views 1014 views
  • Likes 2 likes
  • Comments 3 comments
Related
Recommended
  • raspberry pi live streaming
  • media_stream
  • raspberry pi 3 projects
  • internet of things
  • live_video
  • mjpg-streamer
  • Raspberry Pi Camera
  • raspberry pi video camera
  • raspberry-pi3
  • raspberry-pi2
  • raspberry pi
  • mjpg
  • iot
  • live_streaming

Stream live videos from your Pi

rados-dp
rados-dp
26 Aug 2016

OVERVIEW

In this article we'll show how to stream live videos from your Pi to a web browser. The technology that we've used to achieve that is an MJPG-streamer application and a Raspberry Pi camera. Below is a preview of the end result.

image

PRE-REQUISITES

You will need:

  • Raspberry Pi connected to the internet.
  • Raspberry Pi camera
  • Dataplicity installed. If you need some help to get up to speed, see Getting started with Dataplicity.

SET UP RASPBERRY PI CAMERA

The first thing to do is to set up the camera and make sure it's working. Connect the camera to your Pi as show below with the blue marking on the camera tape connector facing the ethernet port.

image

After the camera is physically connected you have to enable it. This is done using the native Raspberry Pi configuration tool - raspi-config. Run the tool using the command below.

sudo raspi-config


A menu with different configuration options will appear. Navigate to
Enable camera and press enter.


image

Select Yes option and press enter.


image

From the main menu exit raspi-config by pressing the right arrow twice to navigate to the "<Finish>" option and press enter to exit. You will be prompted to reboot your Raspberry Pi for changes to take effect - select Yes.

TEST THE CAMERA

To test that the camera is functioning correctly we'll take one image and save it to /home/pi using the command below.

raspistill -o /home/pi/image.jpg


When the command is executed you will see a camera preview appear on your display. The preview will stay on for few seconds and then an image will be taken.

RASPBERRY PI CAMERA AS USB VIDEO DEVICE

The Raspberry Pi camera can appear in /dev as a standard USB video device (required by MJPG) if we load the "Video for Linux 2" (V4L2) module for the corresponding hardware (BCM2835). We do this as follows:

sudo modprobe bcm2835-v4l2

 

Upon successful execution of this command you should see Video0 device file inside /dev directory. To verify, run the command below.

ls /dev | grep vid

 

image

UPDATE YOUR SYSTEM SOFTWARE

Prior to proceeding with the steps below please first make sure that the latest updates are installed on your Pi. To update your Pi run the two commands below.
sudo apt-get install update
sudo apt-get install upgrade

INSTALL PRE-REQUISITES

This project requires the presence of libjpeg-dev, imagemagick, libv4l-dev. Install them as follows:

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

BUILDING MJPG-STREAMER

The MJPG-streamer application doesn't come in a package form that we can install using sudo apt-get install command. Instead, what we have to do is to download and compile it from source code.

Source code usually comes in the form of a "tarball" i.e. multiple files packed together into an archive using the tar utility and then compressed using gzip utility to reduce the size of the archive.

Download MJPG-streamer tarball using the command below.

wget http://terzo.acmesystems.it/download/webcam/mjpg-streamer.tar.gz

 

Unpack the tarball as follows:

tar -xvzf mjpg-streamer.tar.gz

 

Make a symlink between two libraries that will be used to compile MJPG-streamer:

sudo ln -s /usr/include/libv4l1-videodev.h /usr/include/linux/videodev.h

 

Navigate into MJPG-streamer directory and edit Makefile using your favourite editor.

cd mjpg-streamer/

nano Makefile

 

In this file, comment out the input_gspcav1 plugin by inserting " # " at the beginning of the line, like so:

# PLUGINS += input_gspcav1.so

 

Finally, run make to compile the program.

make 

 

Compilation should only take couple of seconds and should produce no errors. When successful we'll have an MJPG-streamer application executable built.

RUN MJPG-STREAMER SERVER

The command below will run the server.

sudo ./mjpg_streamer -i "./input_uvc.so -f 10 -r 640x320 -n -y" -o "./output_http.so -w ./www -p 80"


If you see an output similar to the one below it means that the server is running correctly

image

ENABLE WORMHOLE

If you haven't already done so, log in to your Dataplicity account and go to the device (e.g. the Pi) you want to link to. At the top of the page will be a link to 'Activate Wormhole'.

image

Press it and enable Wormhole. The link will contain your device ID.

ACCESS YOUR CAMERA VIDEO STREAM VIA DATAPLICITY WORMHOLE

To see our video stream embedded using HTML we'll go to the address below (remember to replace <YOUR_ID> with your device Wormhole URL from your Dataplicity account).

https://<YOUR_ID>.dataplicity.io/stream_simple.html

 

Below is an example of the video captured from the above URL.

image

TROUBLESHOOTING THE VIDEO STREAM

You might find that sometimes your stream could crash, jitter or suffer high lag. This can happen if your Pi and/or your internet connection cannot sustain the video stream at the framerate and resolution you have selected

 

Breakdown of MJPG-streamer run command

The MJPG-streamer itself contains couple of options. In the above command that we used to run the server we've set two options:

  • "-i" is an option that takes path to an input plugin and the parameters to it.
  • "-o" is an option that takes path to an output plugin and the parameters to it.

The parameters that we've used for the input plugin are:

  • "-f" framerate - this sets how many frames are captured per second.
  • "-r" resolution - this sets the size for the image we're capturing
  • "-n" This option suppresses the errors that show up when the command is ran. These errors show because our camera can't be physically controlled. The errors have no impact on the streaming functionality but it's nice to run the server without seeing them.
  • "-y" Specifies YUV format for the output image.

The parameters that output option takes that we're using are:

  • "-w" path to folder where the website content is served from.
  • "-p" port on which the website and stream is served on.


Accordingly, as we ran it above, mjpg_streamer was operating at a resolution of 640x320 and a framerate of 10 images per second. Here is the command to run it at a reduced 5 frames per second:

sudo ./mjpg_streamer -i "./input_uvc.so -f 5 -r 640x320 -n -y" -o "./output_http.so -w ./www -p 80"
  • Sign in to reply

Top Comments

  • DAB
    DAB over 6 years ago +1
    Excellent detailed post. I was not aware that you could setup the RPi to stream video like this. Great idea. DAB
  • amri_amin
    amri_amin over 6 years ago in reply to mud07

    try updating /etc/apt/sources.list with alternate download source.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mud07
    mud07 over 6 years ago

    I'm trying it but this line of code:

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

    gives a series of errors then downloads some of the files and at the end fails to fetch some of the files saying that they are not found.

    Could you please update me with it

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 6 years ago

    Excellent detailed post.

     

    I was not aware that you could setup the RPi to stream video like this.

     

    Great idea.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube