How to RTSP stream on a Pi Zero

  bash, hardware, raspberry-pi

Install VLC

sudo apt-get install vlc

Enable the camera

sudo raspi-config

and select finish, reboot.

Start streaming

raspivid -o - -t 0 -sh 55 -br 55 -vs -b 1500000 -w 640 -h 480 | cvlc --ffmpeg-hw -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554}' :demux=h264
Watch the video stream from a Windows PC using VLC player
Control + N and type the URL (it’s RTSP, not HTTP): rtsp://ip-of-your-pi:8554/
Make sure to add a slash / at the end of the URL:
Play.
Put it in a script
Stop the camera if it’s running.
touch /home/pi/live.sh
chmod u+x /home/pi/live.sh
nano /home/pi/live.sh

Add contents:

#!/bin/bash
 
# If you need to rotate the camera, you can use: 
# raspivid --nopreview --rotation 180 -o - ... etc 
 
raspivid --nopreview -o - -t 0 -b 3000000 -w 800 -h 600 | cvlc --ffmpeg-hw -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554}' :demux=h264

Save, test it:

/home/pi/live.sh

If it works, stop it again, Control + C

Create a startup script

sudo nano /etc/init.d/startcam

with contents:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          startcam
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts camera in stream mode
# Description:
### END INIT INFO
# for ntfs-3g to get correct file name encoding
if [ -r /etc/default/locale ]; then
        . /etc/default/locale
        export LANG
fi
do_start() {
        nohup su pi /home/pi/live.sh &
}
case "$1" in
  start|"")
        do_start
        ;;
  restart|reload|force-reload)
        echo "Sorry, not implemented." >&2
        exit 3
        ;;
  stop)
        # No-op
        ;;
  *)
        echo "Usage: startcam.sh [start]" >&2
        exit 3
        ;;
esac
:

Save it.

Make it executable

sudo chmod a+x /etc/init.d/startcam

Add it to always start up

sudo update-rc.d startcam defaults

Reboot and test that it starts up

sudo shutdown -r now

Reboot and test that it starts up

sudo shutdown -r now

And go to VLC and see that you can see the rtsp://ip:8554/ stream