Friday 19 August 2016

Raspberry pi Live video stream

After accessing raspberry pi remotely , ever wondered what all things can be accessed remotely?
The possibilities are endless and in this post you will get to access the live video stream remotely on your laptop.
Yes you can consider this as a project of "internet of things" where you are communicating to the webcam via internet.


Raspberry pi  can be used to stream live video from a USB webcam or a raspberry pi camera module. This concept can be useful for projects involving video streaming like a surveillance camera , drone , image processing etc.

There are 2 ways to stream video online
  • 1.       Using MJPG Video streamer  : MJPG-streamer takes JPGs from Linux-UVC compatible webcams, filesystem or other input plugins and streams them as M-JPEG via HTTP to webbrowsers, VLC and other software. It is the successor of uvc-streamer, a Linux-UVC streaming application with Pan/Tilt
  • 2.       Using motion – Which is a library file with inbuilt functions to stream , record , sense motion , etc which can be modified according to our convenience.( We will be using this method)



You will need:

  • 1.       Raspberry pi ( I have used raspberry pi 3)
  • 2.       SD Card with pre- installed raspbian
  • 3.       Usb webcam
  • 4.       Power adapter(2.5 Amps)
  • 5.       Wifi router/dongle
  • 6.       Laptop
  • 7 Wifi adapter( If you are using other versions of raspberry pi)

Pre requisites:


1.       Raspberry pi should be connected to internet
2.       Remote access via putty should be ready

Steps:


  • .       Update and upgrade your raspberry pi


                 sudo apt-get update
                 sudo apt-get upgrade
  • .       Install motion

                  sudo apt-get install motion
  • 3.       Open the file to reconfigure it


                  sudo nano /etc/motion/motion.conf
  • .       Press cntrl+w to search for keywords and press enter as it would be cumbersome to find the word in 700-800 lines of code:


·         daemon on #default off (This allows the motion to run in the background)
·         framerate 30 #default 2 (increased framerate)
·         width 640 #default 320 (changed width to match that of the webcam)
·         height 480 #default 240 (same as above but for height)
·         threshold 2000 #default 1500 (threshold value for motion detection)
·         pre_capture 2 #default 0 (captures 2 frames before motion was detected and adds that to the videos to make them smoother)
·         post_capture 5 #default 0 (same as above but captures frames after)
·         output_normal off #default on (this disables storing images, since we only require video)
·         ffmpeg_video_codec msmpeg4 #default swf (msmpeg4 is accepted by windows media player, hence easier to play on Windows)
·         target_dir /mnt/motionvideos #default /tmp/motion (changed the directory where videos will be stored)
·         webcam_maxrate 5 #default 1 (increase the max framerate on lie stream)
·         webcam_localhost off #default on (allows you to set up a live stream of the webcam)

  • If you want to store pics turn output_normal on , you can tun off ffmpeg video option if you don’t want to record video.
  • Instead of webcam _localhost and webcam_maxrate  your file may have stream_localhost and stream_maxrate respectively.
  • Press cntrl+x to exit , press y and then enter to overwrite the file
  • We will need to also create the directory we specified in targer_dir above and change the owner of that directory to motion. Use the following command to do this:

               sudo mkdir /mnt/motionvideos
               sudo chown motion /mnt/motionvideos
  • We’ll now enable daemon by using the following command:


               sudo nano /etc/default/motion
  • .           Once the file is opened, change start_motion_daemon=no to yes. Then hit Ctrl+X to close         and Y  to save (same as above). Enabling daemon also means that motion will start on boot by default.
  • .       Enter the commands to start webcam and stop it respectively

                sudo service motion start
                sudo service motion stop

  • if the output on your terminal is as:

               ubuntu@RPI:~$ sudo service motion start
              * Starting motion detection daemon motion       [ OK ]
              Then it is working properly . Otherwise type
              sudo motion.
  • .       Open a browser (chrome/firefox) . and in search bar type

          http://192.168.1.105:8081

         192.168.1.105 is the IP adress of my raspberry pi , you can get that by ifconfig in LXTerminal
          If its not working , stop the webcam , change the port
          webcam_port 8081 to 8086 in sudo nano /etc/motion/motion.conf
           again instead of webcam_ port you may have stream_port


This image was taken by webcam as it detected motion.

Adding security to web streaming:

You may need to add security to prevent other people from watching you if they are connected to same network.
Adding id and password may give you the first line of security to your web streaming.
To add security to your page , open the config file using sudo nano /etc/motion/motion.conf

Press cntrl+w to search the keyword authentication.

# Set the authentication method (default: 0)
# 0 = disabled
# 1 = Basic authentication
# 2 = MD5 digest (the safer authentication)
stream_auth_method 0

# Authentication for the stream. Syntax username:password
# Default: not defined (Disabled)
 stream_authentication id:password

This will be the default setting for authentication ,
Change the stream_auth_method  to 2
And add your id and password to the line-  stream_authentication id:password
Remove the semicolon in front of stream_authentication:id password

You may need to reboot the pi , login again , and run motion using sudo service motion start or sudo motion , open browser in one of your device and enter the ip address followed by port no. , you will be prompted with message for id and password.

Accessing the photos and videos:

The camera will click photos and shoot videos if it detects any movements. You can change the intensity by changing the threshold value in sudo nano /etc/motion/motion.conf
Edit  output_normal  on  in motion.conf  to click pictures
Edit  ffmpeg video on in motion.conf  to shoot videos

You may need enough space in SD card as the video occupies enough space and plenty of pictures will be clicked.
If you don’t have enough space you can test it for a small period of time and disconnect your webcam from pi and edit the motion.conf file by setting output_pictures and ffmpeg video to off.
Again if you don’t want several pictures to be clicked , you have several options in your motion.conf file:
# Output 'normal' pictures when motion is detected (default: on)
# Valid values: on, off, first, best, center
# When set to 'first', only the first picture of an event is saved.
# Picture with most motion of an event is saved when set to 'best'.
# Picture with motion nearest center of picture is saved when set to 'center'.
# Can be used as preview shot for the corresponding movie.
output_pictures off

To access your pictures type command startlxde-pi in your putty
The xming should be running in tray , only then you will be able to get the GUI of raspberry pi on your laptop screen.
Go to file explorer, type
/mnt/motionvideos
You will be directed to folder containing your image and videos


What next?

Try port forwarding concept to access your web streaming from any part of the world( i.e. outside your local network).
Currently I am working on it will be uploading its tutorial soon in this blog!