Achieving high frame rate with a Raspberry Pi camera system

When you read about using Raspberry Pi cameras as part of your home security system, most of the information you will find will point you in the direction of running motion which is the de-facto standard for doing video motion detection on Linux. There are also variants of motion such as MotionEye or motionEyeOS which provide a nicer UI on top of motion.

Motion requires some horsepower to handle the video processing to detect motion, and also to run the web server and other features. On the modest hardware on the Raspberry Pi, I was only able to reliably achieve 4-10 frames-per-second at 800×600, depending on the Raspberry Pi model in use.

Using this technique below, my camera system is now able to get the full resolution for each Raspberry Pi camera which is 1920×1080 at 30 frames per second.

The problem with using motion on the Raspberry Pi

After much experimentation and time trying to make it work, I found the motion solution is not that great in practice. I have spent countless hours writing scripts, web pages, and various hacks to build a robust 4 camera home security system using this setup.

The cameras suffered from low FPS, high load average, frequent lockups, and SD cards dying. This slow FPS is due to the high CPU needed by motion performing image processing on the low powered Pi. The high load average and SD card death is being caused by the high number of writes (images and video files) on the SD card.

Managing all the video files was a problem as well. Files from the multiple cameras need to be remotely viewable in one central location. The SD card fills up fast and old files need to be deleted to make room for new files.

There has to be a better way!

The Solution

The solution I found was to take as much of the processing off of the Raspberry Pis as possible. I wanted to get the devices to send video, and not write anything on the local SD card. The image processing needed to be centralized on a more powerful machine.

For your camera server, you could choose any NVR hardware or software which supports the RTSP standard for video streams. In my case I had an old Mac Mini in the closet, and went on the hunt for software that would work on MacOS. After some research, I came across a software called SecuritySpy from Ben Software. This software provides so many capabilities that I was attempting to create myself and now I am a huge fan of it.

SecuritySpy is a paid software and is totally worth the price for the features and support that you get.

Download, Install and set up SecuritySpy

SecuritySpy download page

Some of the features SecuritySpy provides:

– Live Video with FPS overlay
– Constant recording, or motion detection recording
– A robust interface for viewing recorded videos
– Web based interface for viewing on computer, phone, TV
– iOS client for live viewing on iPhone, iPad, AppleTV
– Automatic deletion of old video files (by age, or when the hard drive fills)
..and much more

Configure the Raspberry Pi camera to be a RTSP streaming server

Set up the Pi camera according to the camera setup guide. You need to run raspi-config and enable the camera module.

This technique uses the software raspivid and the VLC command line interface cvlc. raspivid should already be installed with your Raspbian install, but you will probably need to install VLC. We will also install screen which is needed for the auto-start:

sudo apt-get install vlc screen

Create a script to turn your Raspberry Pi into a RTSP streaming server. In the pi home directory /home/pi, create the file stream-rtsp.sh:

#!/bin/bash
raspivid -o - -t 0 -w 1920 -h 1080 -fps 30 -b 250000 | cvlc -vvv stream:///dev/stdin --sout '#rtp{access=udp,sdp=rtsp://:8554/stream}' :demux=h264

Tweak the settings to your liking. The -w width -h height -fps fps -b bitrate are good values to play with.

I sometimes run on a lower configuration to reduce network bandwidth:

#!/bin/bash
raspivid -o - -t 0 -w 1000 -h 576 -fps 20 -b 250000 | cvlc -vvv stream:///dev/stdin --sout '#rtp{access=udp,sdp=rtsp://:8554/stream}' :demux=h264

With this simple script, your Pi will be running as a RTSP server listening on both TCP and UDP. Remove access=udp to disable the UDP stream if you want. My suggestion is to leave both enabled, and see which one works best for you.

Test the script by running it manually.

./stream-rtsp.sh

After you see it is working, set up the script to run automatically on startup (inside a screen session). Add this to the middle of your /etc/rc.local file (above exit 0):

# start streaming video
su - pi -c "/usr/bin/screen -dmS stream /home/pi/stream-rtsp.sh"

Configure SecuritySpy to connect to the camera

In SecuritySpy camera setup, add a new camera.

Enter the Raspberry Pi camera IP address. Select “Profile” “Manual configuration”, “Format” “RTSP TCP (video and audio)”, and down in “Request” enter “/stream”.

Apply Preferences, then the video should show up.

This technique also works for getting high resolution and high frame rate out of the new Raspberry Pi Zero W camera packs (infrared or standard)! At $45 they are a great way to add extra wireless cameras to your home security system.

4 Replies to “Achieving high frame rate with a Raspberry Pi camera system”

  1. This is great! I currently have MotionEye OS running on a Pi B in my garage, but I am baffled to why the FPS is terrible, even with Ethernet connectivity (as opposed to WiFi).

    I’ve actually seen that it might be possible to add a RTSP stream to Synology’s Surveillance Station as well, so I’m going to give that a go and see how that copes. I also own a PPC Mac Mini, and saw that SecuritySpy version 3.2.1 supports this, so I’ll give this a go too if the NAS doesn’t work as well.

    Looking forward to trying this out later!

      1. Hey Chris,

        I managed to get this working in Synology Surveillance Station 8.0.3-5159, but I needed to make a few tweaks.

        I started with Raspbian Lite (2017-04-10) and needed to install screen so that I could get the script to run on boot. Plus I needed to ensure I added the script in rc.local before the “exit 0” line (rather than at the end).

        Thanks for the guide! Pleased with the result.

Leave a Reply

Your email address will not be published. Required fields are marked *