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 closer to the full resolution for each Raspberry Pi camera which is 1920×1080 at 30 frames per second – and reliably running for years without issue.

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
<code>

[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.


Posted

in

by

Tags:

Comments

70 responses to “Achieving high frame rate with a Raspberry Pi camera system”

  1. Wesley @ Raspberry Coulis Avatar

    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. Chris Carey Avatar

      Good luck! Let me know if this works out for you. I think the Synology should support it. I read that RTSP was added in Surveillance Station 7.1 beta.

      1. Wesley @ Raspberry Coulis Avatar

        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. Chris Carey Avatar

          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. George Vieira Avatar
          George Vieira

          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.

          1. Chris Carey Avatar

            Totally right George. I’ll get some proper systemd scripts on here and update the post.

        3. David Avatar
          David

          Hey Wesley,

          how did you managed to set it up in the surveillance station? He is not finding my stream.
          Thank you 🙂

          1. kb Avatar
            kb

            This is awesome. Works so well! Been looking for something this simple for ever. You can even image the card and drop them in other pis.

            In Survalliance Station, choose generic cam and choose rtsp and enter the url rtsp://xxx.xxx.xxx.xxx:8554/stream

            I personally set my framerate for 20fps, 1600×1200, and 7168k bitrate

      2. Cato Antonsen Avatar
        Cato Antonsen

        Hi, thanks for an excellent article! Finally I’m able to achieve good resolution and FPS!

        A couple of comments/questions:
        I’m not able to run this as a service (Option 1 – Systemd). I’m wondering if I got the correct parts info stream-rtsp.service. The formating ine the article makes me unsure ehat belongs where. And do the Unit-sectio only contain 2<code"? If I get autostarting to work I will be soo happy! 🙂

    2. Cato Antonsen Avatar
      Cato Antonsen

      Hi, thanks for this excelent article! I’mfinally able to stream in high resolution with FPS! 🙂 I struggle with running this as a systemd service. My files:

      $ cat rtps.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
      cat /etc/systemd/system/stream-rtsp.service:

      [Unit]
      Description=auto start stream
      After=multi-user.target
      [Service]
      Type=simple
      ExecStart=/home/pi/rtps.sh
      User=pi
      WorkingDirectory=/home/pi
      Restart=on-failure

      [Install]
      WantedBy=multi-user.target

      $ cat /home/pi/rtps.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
      pi@camera:~ $

      When running rpt.sh manually:

      main demux meta debug: no meta reader modules matched
      [b3500668] main input debug: `stream:///dev/stdin’ successfully opened
      [b3500668] main input debug: EOF reached
      [b360d520] main demux debug: removing module “h26x”
      [b360a340] main demux packetizer debug: removing module “h264”
      [b3603330] main stream debug: removing module “record”
      [b36030b0] main stream debug: removing module “prefetch”
      [b3602f50] main stream debug: removing module “filesystem”
      [01b287d0] main playlist debug: dead input
      [b3600858] main stream output debug: destroying useless sout
      [b3600aa8] main stream out debug: destroying chain… (name=rtp)
      [b3600aa8] main stream out debug: removing module “stream_out_rtp”
      [b3600aa8] main stream out debug: destroying chain done
      [01b287d0] main playlist debug: changing item without a request (current 0/1)

      Do anyone see what I have done wrong or have a suggestion? I’m a Linux newbie..

      1. Cato Antonsen Avatar
        Cato Antonsen

        Ehen I’m running the script manually, I’ able to watch the stream for about 60 seconds. This seems to be the same as mentioned here: workaround:https://code.videolan.org/videolan/vlc/-/issues/25662

        Are you able to watch for (considerably) longer then 60 seconds? If so, what did you do?

  2. Arturo Avatar
    Arturo

    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. Chris Carey Avatar

      Maybe tweak the raspivid bitrate settings to something lower like 50000? I also have noticed lag at high res with high framerate.

      1. searching_for_pi_stream_solution Avatar
        searching_for_pi_stream_solution

        That would be 50 kbps. That’s 7x less than 3G standards (http://help.encoding.com/knowledge-base/article/understanding-bitrates-in-video-files/)….dial up maybe?

        1. Chris Carey Avatar

          Good point. That’s way low. I’m not sure how to get rid of that 2s lag

  3. DaveT Avatar
    DaveT

    Chris, This is exactly what i am looking for

    Does your method have audio/video stream using a zero-w and usb mic?

    1. Chris Carey Avatar

      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

  4. DaveT Avatar
    DaveT

    And Chris, can i ask if you have tested the latest UV4L and how it compares to raspivid

    http://www.linux-projects.org/

    1. Chris Carey Avatar

      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!

  5. DaveT Avatar
    DaveT

    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

  6. Sandeep Mathur Avatar
    Sandeep Mathur

    Did you consider a linux server running motion ? Would that work ?

    1. Chris Carey Avatar

      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.

  7. John Grylls Avatar
    John Grylls

    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.

  8. JD Avatar
    JD

    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!

  9. Harry van der Wolf Avatar
    Harry van der Wolf

    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. Chris Carey Avatar

      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.

  10. Adam Avatar
    Adam

    Hi there,
    The “stream-rtsp.sh” script need to be made executable by “sudo chmod +x stream-rtsp.sh”, correct?

    Thanks,

    1. Chris Carey Avatar

      Yes, I updated the post with this.

  11. Craig Avatar
    Craig

    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. Chris Carey Avatar

      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. Craig Avatar
        Craig

        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.

  12. Craig Avatar
    Craig

    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 🙂

    1. Chris Carey Avatar

      No, I do not know how to do grayscale

  13. AndreG Avatar
    AndreG

    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.

    1. Chris Carey Avatar

      Thank you. I updated the post with this.

  14. MrGlasspoole Avatar
    MrGlasspoole

    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. Chris Carey Avatar

      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.

  15. BabyMonGuy Avatar
    BabyMonGuy

    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. Chris Carey Avatar

      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. BabyMonGuy Avatar
        BabyMonGuy

        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

  16. […] raspberry Pi just wasn’t cutting it, so I decided to setup an RTSP stream as per this post here by Chris Carey, that was straightforward enough, but then I needed a way to periodically grab […]

  17. alsec Avatar

    Fantastic! Thanks so much for the share Chris

  18. mmhorda Avatar
    mmhorda

    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.

  19. Dan Avatar
    Dan

    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.

  20. Tom Avatar
    Tom

    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.

  21. […] ACHIEVING HIGH FRAME RATE WITH A RASPBERRY PI CAMERA SYSTEM […]

  22. ANDREW Avatar
    ANDREW

    Really nice write up, thanks so much. I manged to get my PI streaming with a low bandwidth. BUT , I Know, it seems to hit a buffering error and freeze for 1/2 second or so every few seconds. Otherwise the video is great. Is there anyway to stop it from doing this? ES_OUT_SET (GROUP) PCR is called too late jitter of 23992ms ignored
    Timestamp conversion failed for 4840001: no reference clock
    Could not convert timestamp 0,0 for h.264
    Ever deal with this?

    1. Chris Carey Avatar

      Tom from Germany wanted to make this reply here but was having issues. Here it is…

      • Fix #1 – for raspivid error “ES_OUT_SET_(GROUP_)PCR is called too late” & stream keeps freezing:

      Use h264-fps-option at the end of cvlc-command:
      –h264-fps=YOUR-FPS-RATE

      • Fix #2 (optional) – for audio playback error “PulseAudio server connection failure”:

      Specify a device to playback on at the beginning of cvlc-command:
      -A alsa,none –alsa-audio-device default

      • general example (WITHOUT fixes):
      raspivid -ih -pf -pts -o – -t 0 -w 1280 -h 720 -fps 20 -b 3500000 -g 40 | cvlc -vvv stream:///dev/stdin –sout ‘#rtp{access=udp,sdp=rtsp://:8554/stream}’ :demux=h264

      • example WITH fix #1:
      raspivid -ih -pf -pts -o – -t 0 -w 1280 -h 720 -fps 20 -b 3500000 -g 40 | cvlc -vvv stream:///dev/stdin –sout ‘#rtp{access=udp,sdp=rtsp://:8554/stream}’ :demux=h264 –h264-fps=20

      • example WITH fix #1 + #2:
      raspivid -ih -pf -pts -o – -t 0 -w 1280 -h 720 -fps 20 -b 3500000 -g 40 | cvlc -A alsa,none –alsa-audio-device default -vvv stream:///dev/stdin –sout ‘#rtp{access=udp,sdp=rtsp://:8554/stream}’ :demux=h264 –h264-fps=20

      • access the RTSP-stream with VLC-player or any other NVR software:
      rtsp://IP-TO-YOUR-RASPBERRY:8554/stream

      *tested on Raspberry Pi Zero W running Raspbian Stretch with Raspberry Pi Cam NOIR v2.1

  23. Michael Schirrmeister Avatar
    Michael Schirrmeister

    hey,
    worked nice for me. Realy good documantion thats all I needed. I just have one question :
    Is there a way to stop the video playback on the pi itself? so that this run just in the background

  24. Collin Avatar
    Collin

    Thank you thank you thank you. Ive been pulling my hair out with Motioneye trying to figure out why I couldn’t get more than a 1-2 FPS at any decent resolution streaming to my ZoneMinder server. I couldn’t stop thinking about how my $40 1080p cam from amazon could do a 1/2 decent job at it, but the Raspberry Pi couldn’t. Now with your setup I’ve got great quality, high FPS, and reliable connection. Thank you so much for this!

  25. Mikkel Avatar
    Mikkel

    Awesome post thanks!

    I modified it a bit, as the 250kbps bitrate was unuseable at 1080p for me.

    Also, you don’t need the shell script sitting around, you can stuff it into the systemd service with `bash -c “command here”`

    This is the systemd service I ended up with:

    “`systemd
    [Unit]
    Description=A service for starting an RTSP stream on port 8554
    After=network-online.target

    [Service]
    User=pi
    ExecStart=/bin/bash -c “raspivid -o – -t 0 -w 1920 -h 1080 -fps 30 -b 5000000 | cvlc stream:///dev/stdin –sout ‘#rtp{sdp=rtsp://:8554/stream}’ :demux=h264”
    Restart=on-failure
    RestartSec=2

    [Install]
    WantedBy=multi-user.target
    “`
    The stream was already decent at 1Mbps, but I upped it to 5, as most motion artifacts were gone by then. I’ve also remoted udp, but I wouldn’t really consider that a general improvement. I just happen to be deploying this behind a tcp-only tunnel, so yeah.

    Works great, thanks for the good tip! 🙂

  26. Collin Avatar
    Collin

    After reading through the documentation for the Raspi V2 cam this is what I ended up with:

    #!/bin/bash
    raspivid -o – -t 0 -md 5 -qp 30 -b 0 -fps 30 | cvlc -A alsa,none–alsa-audio-device default -vvv stream:///dev/stdin –sout ‘#rtp{access=udp,sdp=rtsp://:8554/stream}’ :demux=h264 –h264-fps=30

    the -md command uses built in modes for the camera that allows the full sensor to be used with no binning for a sharper cleaner image. Then I used -qp 30 in combination with -b 0 to set the output to a variable bit rate. -qp can be adjusted from 10-40, 10 being the best quality, 40 being the lowest quality. This works well on the Pi Zero W and so far it’s been streaming for the last 4 days with no issues or WiFi drop outs. I also under-clocked the CPU to 700MHz on the Pi Zero W as mine would hang and drop WiFi about once a day at the stock 1.0GHz.

    1. Tom Avatar
      Tom

      that solution looks very promising but when i try your settings for the stream it does some weird stuff and ends with.

      main art finder debug: no art finder modules attached.

  27. Tom Avatar
    Tom

    Brilliant article. I was struggling with motion and Pi Zero W due to the described resource issues for quite a while. Yout tutorial is crisp and to the point! Thanks for that!

  28. Fredrik Idengren Avatar
    Fredrik Idengren

    This is so great! I just wanted a RTSP-stream from a RPi Zero w.

    However as a new user of RPi I was a bit confused in the “Script startup Option 1 – systemd startup script” about what I would add in stream-rtsp.service or what to write as a command. Did not get it working – so went with the “screen-option”.

    However, even though the hdmi now shows a perfect image after rebooting – I cannot add the RTSP-stream to my NVR-solution (Camect).

    I´ve tried adding (fake IP): 192.165.0.1:8554/stream – and add it as a RTSP-stream – but it cannot connect.

    Oh, and please add “sudo” to the “Make the script executable:”-line. Took me ages to figure out. 😉

    Cheers

  29. Philipp Avatar
    Philipp

    Great work!
    Somehow I can‘t reach the stream over a Browser. I‘ve tried with the spysoftware App for iOS. It won‘t work.

    Do you have an idea why?

  30. Philipp S Avatar
    Philipp S

    Great work!

    Do you have an idea, why I’m not reaching the server over browser or the SecuritySpy software?
    Do I need to take the local IP adress of the Pi in my network?

  31. Philipp Avatar
    Philipp

    Great Work!

    I can’t get the rtsp server working with the securityspy software.
    I strictly followed your instruction.

  32. Drew Helgerson Avatar
    Drew Helgerson

    Hi. I stumbled upon this page while looking for ways to stream video from a RPi 0 powerd robot I built. I initially tried using motion but the lacking power of the RPi0 made it unusable. the problem is that I am using a USB webcam rather than a raspberry pi cam. can you provide a script for using a USB webcam instead? possibly ?fswebcam? but I couldn’t get it to work

    1. Chris Carey Avatar

      Hi Drew, something like this might work
      cvlc -vvv v4l2:///dev/video0:chroma=h264 :input-slave=alsa://hw:1,0 –sout ‘#rtp{access=udp,sdp=rtsp://:8554/stream}’ :demux=h264

      1. Drew Helgerson Avatar
        Drew Helgerson

        thanks, this seems to work from the server end with no errors (I deleted the also argument because I don’t need audio) but I can’t get it to pull up in vlc on another device.

        console spams `[6cf01688] mmal_vout generic debug: vd_pool: fmt:640×480,sar:0/0; source:640×480` when running

        1. Dor Avatar
          Dor

          Help pls… Do u find a solution for that

  33. Miguel Avatar
    Miguel

    Hello Chris!
    The script works fine, but the stream using RTSP stops after 1 minute (in VLC). Do you have an idea of what I’m doing wrong?
    Thanks!

    1. vladimir vinarsky Avatar
      vladimir vinarsky

      Hi Miguel,
      I am in the same situation, using the .sh file provided here works well for a minute or so, but then my VLC stops.
      In my case the stream is obviously going on, because I can connect again, so it is on a VLC side (using the Vetinari 3.0.14).
      I registered into a VideoLAN forum (https://forum.videolan.org/) but still did not figure how to add a topic (maybe it comes after gaining some merits) so I am pretty helpless.
      At least being not the only one with the issue raises my hopes of finding a solution;)
      Have a nice day
      Vladimir

    2. Chris Carey Avatar

      Try experimenting with different values for -w -h -fps and -b . Read through the comments here to see some other configurations people have had success with. On some systems I see this where it will only stay connected for a minute. Usually I found this to be the buffer. You should run the command on the Pi interactively, so you can watch the output in the terminal to see if it’s the buffer running out or see if you can catch any errors show up on the console.

      1. Vladimir Vinarsky Avatar
        Vladimir Vinarsky

        Dear Chris,
        thank you for reply, I tried varying following parameters without any visible differences on when VLC drops the stream (around 60sec):
        * the bitrate (-b) parameter from variable (-b 0) to (-b 25000000)
        * the resolution (-w 320 -h 240 to -w 1080 -h 920)
        * the network buffer in VLC (1000ms to 10000ms)
        * the fps (-fps 10 to -fps 30)

        I suspect that the fault is on the VLC side:
        * the stream is running all the time from the Zero
        * I do not see any error messages when VLC drops the stream in the command line accessed through ssh
        * I can reconnect again (the timestamp on the vlc video screen does not start from zero).
        * the only messages I see are after stopping the stream using the CTRL+C starting with “main libvlc debug: removing all interfaces.”

        I am very new to this stuff, so I wonder, if it is just something wrong with VLC, and it is easier to switch to other software? I am testing it from my old win7 computer but plan to have it streamed to my raspberry pi4 for some image analysis and then record/stream to the internet. Would you recommend some other free or paid software I can run on win7 and then RPi?

        Thanks Again Vladimir

  34. Drew Helgerson Avatar
    Drew Helgerson

    Hi, to anyone reading this still struggling with performance, especially on the raspi zero, look into mjpg-streamer. It performs much better and uses almost no resources.

    1. vladimir Avatar
      vladimir

      Thanks Drew,
      will check it out:)
      Have a nice day Vladimir

  35. Joschi Avatar
    Joschi

    Hey,

    I’ve been playing around with the new Pi Zero 2 and figured a working solution.

    raspivid -t 0 -n -b 1000000 -g 30 -ih -pf baseline -w 800 -h 600 -fps 30 -o – | cvlc -vvv stream:///dev/stdin –sout ‘#rtp{sdp=rtsp://:8554/}’ :demux=h264

    This is not a final one since I’m still playing with the raspivid options and it highly depends on the used camera module itself. I’d recommend using a –mode (-m) and setting the –h264-fps to the same value as -fps. This wasn’t neccessary on my end.

    There are some reconnects and I’m still digging into the cvlc settings.

    The stream works fine for me using VLC, Synology Surveillance Station and OBS. Latency is about 3 seconds which is ok for me.

    If the stream still freezes after a longer period of time, try adding force-turbo=1 to \boot\config.txt

    If you’re done with your settings, remove -vvv. I’ve added a heatsink to my Zero 2 and the temperatures stay below 45°C.

    Good luck!

Leave a Reply

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