Tuesday, June 23, 2015

Connect camera to Raspberry Pi and stream video

In my previous blog posts I have described how to configure Raspberry Pi for first time and how to prepare SD card with raspbian image.

Work with Raspberry without Key board, Mouse or Monitor
Write raspbian disk image to SD and Backup your SD Card on Linux

Connecting Raspberry Pi camera module with a Raspberry Pi is pretty simple. All you need is connect camera ribbon cable to CSI (Camera Serial Interface) port as follows. Please note that you have to aware about the correct side of the ribbon cable.

The flex cable inserts into the connector situated between the Ethernet and HDMI ports, with the silver connectors facing the HDMI port. The flex cable connector should be opened by pulling the tabs on the top of the connector upwards then towards the Ethernet port. The flex cable should be inserted firmly into the connector, with care taken not to bend the flex at too acute an angle. The top part of the connector should then be pushed towards the HDMI connector and down, while the flex cable is held in place.

Here is small video guide created by Raspberry Pi team about, how to connecting the camera correctly.


After you connecting the camera correctly, it is needed to enable kernel modules and drivers for camera functionality. So use "sudo raspi-config" to start configurations.


Then Enable camera and restart your Raspberry Pi. Once device is booted use following command to start the camera. If you have a display connected with raspberry Pi, it will display the video feed from camera. Also RED LED in the camera module will turn on by indicating camera activity.

raspivid -t 0

Then press CTRL+C to terminate the camera feed.

Now we need an internet connection to install the streaming server. I preferred vlc for video streaming since it supports h264 encoding and RTSP protocol. To install vlc, you need to update your software repositories in Raspberry Pi. Use this command to update software repositories.

sudo apt-get update

Then install vlc;

sudo apt-get install vlc

Now you are ready to test video streaming. So in first you need to run command with following format. It start raspivid and pipe output to console. Then cvlc tool can use to encode and stream video feed.

raspivid -o - -t 0 -w <width> -h <height> -fps <frame rate> -b <bit rate> |cvlc -v stream:///dev/stdin --sout '#rtp{sdp=rtsp://:<port>/}' :demux=h264

You need to replace <width> with frame width and <height> with frame height which is compatible with 4:3 or 16:9 accept ratios. Replace <frame_rate> with the value that you need to grab frames per second. Higher frame rates contains smooth video but need higher bit rate and high speed network connection. Video bit rate should be assigned as bit per second value. Higher bit rates deliver much clear video and deliver higher video quality. But it also required high speed stable network connection. Finally <port> is used to write stream data to network and you will be able to access video stream via the specified port of your network interface.

Finally you should have to find IP address of your Raspberry Pi device. If you don't know your IP address using ifconfig command.

This is a sample command for stream video with 640x480@15fps 378kbps. This stream can be access through the port 8554.

raspivid -o - -t 0 -w 640 -h 480 -fps 15 -b 393216 |cvlc -v stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264

So if you got IP address like 192.168.43.245, then you can access your video stream using your vlc media player in computer or with your mobile phone which are connected with the same network which your Raspberry connected.

Lets assume IP of your Raspberry Pi is 192.168.43.245, you can access video stream via rtsp://192.168.43.245:8554/

If you have MX Player in your android device you can access video stream via Network stream.



This is how I run it at my office. You can see my mobile is also showing the video feed.





No comments:

Post a Comment