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 frame rate (FPS), high load average, frequent lockups, and SD cards dying. This slow frame rate 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 / motion detection needs to be removed from the Pi and moved to a more powerful machine.

The NVR software I went with

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. For the purpose of this article I will go through how I set up SecuritySpy, but you can use any video recording software that you want.

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

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:

sudo apt-get install vlc

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.

Make the script executable:

chmod +x stream-rtsp.sh

Test the script by running it manually:

./stream-rtsp.sh

Making the script run on startup

After you see it is working, set up the script to run automatically on startup. I have two examples of how to do this here. First is using a systemd startup script which is recommended. Second is inside of a screen session. You only need to choose one option.

Script startup Option 1 – systemd startup script

Create the file /etc/systemd/system/stream-rtsp.service

[Unit]
Description=auto start stream
After=multi-user.target

[Service]
Type=simple
ExecStart=/home/pi/stream-rtsp.sh
User=pi
WorkingDirectory=/home/pi
Restart=on-failure

[Install]
WantedBy=multi-user.target

Set the service to auto start

sudo systemctl enable stream-rtsp.service

Then you can start, stop, or check on the service with:

sudo systemctl start stream-rtsp.service
sudo systemctl stop stream-rtsp.service
sudo systemctl status stream-rtsp.service

Script startup Option 2 – Using screen

As a reminder, you do not need to do this part if you did the systemd startup script above.

Install screen

sudo apt-get install screen

To make screen start every boot, 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"

Note: When using this method the stream-rtsp script will NOT auto restart if for some reason it crashes. That it one benefit in using systemd. Easiest is to just reboot the box.

Option 1 for NVR – Using SecuritySpy

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

Add a new camera in the SecuritySpy setup.

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.

Option 2 for NVR – Monitor the camera with any NVR software of your choosing

The important part here is that we configured the Raspberry Pi to run a RTSP server which accepts connections over TCP or UDP on port 8554 and the request url is /stream

You can connect to this stream with a variety of applications.

One example is to stream this to another Raspberry Pi, you could do it with omxplayer:

Install omxplayer on some other Raspberry Pi:

sudo apt-get install omxplayer

Make a script called stream-video.sh

#!/bin/bash
killall omxplayer.bin
sleep 1
omxplayer --aspect-mode stretch -o hdmi rtsp://10.69.0.23:8554/stream

Edit the IP address to be the camera’s IP.

Make the script executable:

chmod +x stream-video.sh

Run the script

./stream-video.sh

and you should see the full screen video sent out the hdmi port

Works with Raspberry Pi Zero W

This technique of creating a RTSP server 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 the bundle pack that comes with the case and infrared camera is a great way to add extra wireless cameras to your home security system.

41 thoughts on “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.

        1. Thank you for that feedback! I updated the document to mention installing screen, and also to fix the wording of adding the script to the bottom of /etc/rc.local.

        2. Use systemd to manage the service, not rc.local. this way you can ensure things start before or after other services (eg. network) and even make it auto restart if the service/script happens to die.. which is quite needed with security services.

  2. Dear Chris,
    thank you so much, it works on a PI 0 W.
    I’m playing the stream on a Windows PC using VLC… but there’s a huge lag (around 2 secs). Any idea to improve? Thank you!

    1. DaveT no I do not have audio set up yet. If we get cvlc to take audio input from the USB mic, it might be able to mix it in. I’ll give that a try. Try adding :input-slave=alsa://hw:0,0 to your cvlc

    1. DaveT I did play around with UV4L and like the website control panel it creates on the pi and the streaming to the browser is nice. I had trouble getting my setup working with UV4L but it does seem promising. It would probably have the benefit of running the Pi’s even cooler on the CPU. Basically I was trying to get a h264 RTSP stream out of it and off the top of my head I can’t remember what hurdle I kept hitting. If you figure anything out with regard to this, let me know!

  3. I am currently using Pikrellcam / nginx on the zero’s for the last year, it churns out mjpeg and can record the audio with the mjpeg… its super fast in browser but i cannot find the string for the stream to add to the windows server2012 running blueiris

    Hence landing here

    1. I did not go that route but that would be a fine solution. If you can move the motion detection off the pi you will get a much better FPS result on the video files. Also having the files being written on a faster drive would be a plus. The SD card on the Pi is not great with constant writes.

  4. Excellent work Chris,
    I am able to view the stream created using your method and script with my Samsung Tablet and only some stream viewing apps. For the benefit of anyone interested these include tinyCam Pro and Mango Player. This application of a Raspberry Pi and Camera should make for a cheap wildlife camera.

  5. This is awesome. Combined with VLC and a ZTE Mobley hotspot, I’m using this as a DIY carseat cam in my sedan. You just saved me at least $30,000 on a minivan, good sir!

  6. Why not simply run it from the crontab from user pi, with an entry like;

    @reboot /home/pi/stream_video.sh &

    Do a “crontab -e” and add the script to have it start at every reboot. You could also add a second script that checks every 1,2,3 minute(s) to see if the original script is still running and kick it back to life if it has failed for whatever reason.

    1. I have never seen that crontab trick. That looks very useful. Thanks for sharing it. I ended up going the systemd route which is handling the start/stop and restarting if it gets killed.

  7. Hi Chris, this worked brilliantly on my 2B, with both a 5mp and 8mp camera. Thanks! One small hiccup I’ve been trying to resolve… I get quite a lot of ‘noise’/’artefacts’ when there is significant motion. Upping the datarate seems to help, but the noise is often setting off my motion detection. Any thoughts on what this might be caused by? Thanks

    1. Maybe you can connect to the stream with VLC from a computer – connect to network – rtsp://10.10.10.10:8554/stream
      Then see if the same artifact/noise is happening with significant motion. That can rule in or out the video recording software as a culprit.
      If it is the Pi, then try tweaking more settings. Let me know if you figure out a better method.

      1. So, after a few hours, over a couple of days, of pissing around… I still can’t reproduce the problem I was originally having. In the process of trying to reproduce I stuff something and ended up re-imaging my RPi3! And I still can’t reproduce it… But, in case someone else reads this (I’d wanted to provide exact details) I was adjusting the time synchronisation settings within the VLC preferences and that appeared to resolve the problem. I’d been getting those ES_OUT_SET problems with the buffer appearing to run out – and the weird imaging would happen in the viewer after each resume.

  8. Also… Using this method, do you know if there is a way to force encoding in grayscale? That should help with overall bandwidth. Thanks again 🙂

  9. One slight thing I noticed was you also need to run:

    sudo systemctl enable stream-rtsp.service

    in order to have the service be enabled as an auto start item to run on reboot.

    Noticed this after having my picam offline for a couple days after I initially got things working and then booting it up to no rtsp stream being available.

  10. I don’t know what I’m doing wrong.

    You write “works for getting high resolution and high frame rate on Pi Zero”

    I’m testing on a Pi3 with your settings and even with 24fps and viewing it on Windows in VLC the video is pixelated and has i big lag. Also also tried a 50000 bitrate.

    1. Are you wired or wireless? Try it wired to rule out any video issues being caused by your wireless network first.
      If you are getting the same lag on the wired network, then we can try tweaking some more settings.

  11. Thanks a lot for this post. I wanted to use a Pi Zero W with the new NoIR cam module as a networked day/night baby monitor, connected to a pre-existing SecuritySpy setup.

    Followed your steps and ended up with a good result, streaming 1640×922 (to use the whole sensor) and reliably operating at 15fps (which translated to about 110KB/s when there’s no action). I have messed about with the bitrate quite a lot and I’m happy with 900000-1100000 for reliable streaming without overburdening the Pi.

    My script looks like this:

    #!/bin/bash
    raspivid -o – -t 0 -w 1640 -h 922 -fps 15 -b 900000 | cvlc -vvv stream:///dev/stdin –sout ‘#rtp{access=udp,sdp=rtsp://:####/stream}’ :demux=h264

    I ended up having to buy a small 12v IR-LED illuminator as the low light performance of the cam wasn’t good enough to work well with the LEDs that are already on our other RF-based baby monitor. I also had to separate the two devices because the dumb 2.4GHz on the monitor was jamming the Pi’s wifi.

    I looked into ride-along LED illuminators for Pis (people have used them for wildlife cams and stuff) but I think a remote illuminator works pretty well – I even experimented with having my lamp on top on the wardrobe at 45deg and using the ceiling of the room as a diffuse reflector. Awesome!

    Love these little cams, thanks for your efforts.

    On a side note, I did try to get the iStat daemon running on the Zero, so I could graph and log the CPU load and networking, but I haven’t been able to get it to work yet.

    1. Thanks for sharing your setup and that startup script. That resolution and bitrate looks good, I’m going to give it a try on some of my cameras and see how they do. If they run stable I will update the post with those settings as another option. I personally run three infrared illuminators from Amazon. Haven’t found some I’m totally in love with, and they are a bit expensive.

      Thats cool about the iStat daemon. Did not know about that tool, though I do use iStat menus. Do you use that with iStat View on OSX? It reminds me of a similar app on Linux, GKrellM http://gkrellm.srcbox.net/ . Running gkrellmd on the Raspberry Pi should be pretty easy and it can send all the stats over to GKrellM on a Linux machine. I used to run gkrellmd on Linksys routers and even my old TiVo. With GKrellM it does not push stats, but rather you connect to the gkrellmd process from your Linux desktop. So you get gkrellmd running on the Raspberry Pi. Then on your Linux desktop, install gkrellm and run `gkrellm -s ip.address.of.gkrellmd`, then you can see everything going on.

      1. Hi Chris. The small LED illuminators I have been using proved to be very unreliable – either the LDR goes, or some of the LEDs burn out or the power supply has almost-dry solders from the factory.

        Instead of buying yet another, I looked again at the config options tonight to get a decent pic from the 3x very weak LEDs on the off-the-shelf baby monitor we have in the room. The following gave a decent result:

        raspivid -drc high -ex night -o – -t 0 -w 1280 -h 1024 -fps 6 -b 900000 | cvlc -vvv stream:///dev/stdin –sout ‘#rtp{access=udp,sdp=rtsp://:8554/stream}’ :demux=h264

        Key items are the DRC being set to ‘high’ and the exposure mode locked to ‘night’. At 6pfs this gives a good pic. I will experiment with forcing a higher frame rate, as I don’t think these CMOS cams need to run at 1/20th sec / 5fps.

        Image example (the room is dark): https://d.pr/free/i/Xo9gMo

  12. I have good results with this:

    raspivid -o – -t 0 -w 960 -h 720 -fps 25 -g 1 -b 250000 -hf -vf | cvlc -vvv stream:///dev/stdin –sout ‘#rtp{access=udp,sdp=rtsp://:8554/stream}’ :demux=h264

    I don’t know why but I’ve noticed that resolution 960×720 works the best or raspberry pi (seems like taking whole sensor). I have no idea why nobody is talking about it.

  13. Save yourself the time and effort if you are trying to make this stream properly over Minibian.

    My personal advice, if following this guide on Minibian, is to go to your local arms dealer, ask for the biggest whammy kablammy gun they have. Load it with bullets marked “rm -rf” and do your part in stopping Skynet, by using this new device, to manually shut down any Minibian build system you are attempting to stream from via this guide.

    It’s the best option, I promise.

  14. big thanks for the write up, i had to google some more details like how to create a file via ssh as im a total beginner but with a few minutes of googleing i got everything that was missing for me.

    im now using my Synology NAS to connect to the stream and record if i want to, the only thing thats a bit annoying is the delay which is now about 5s because i increased the bitrate to 4000000 so get an acceptable image quality and i will probably try to push it even further just to see how it goes.

    i guess the delay matters less if you have a camera mounted to one place and not to play around with it.

Leave a Reply

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