Live Streaming from a Raspberry Pi via Nginx

I had a Raspberry Pi 3B with the PiCam 2 that stopped working. To make the video better, I needed a shroud to prevent picking up light in the room when it was dark outside and ultimately, three PiCam 2’s died while working with that shroud. I ordered a PiCam 3 the other day to see if I could get it to work with my Pi 3B, and alas, it wouldn’t work. So, I used a 2GB Raspberry Pi 4.

To set up streaming in nginx:

In your main nginx.conf you need:

rtmp {
    server {
        listen some_ip_address:1935;
        application live {
            live on;
            interleave on;

            hls on;
            hls_path /tmp/hls;
            hls_fragment 5s;
        }
    }
}

Then in your site config:

    location /live/ {
        alias /tmp/hls/;
    }

Your Raspberry Pi should run a recent image. You’ll need to install rpicam-apps to replace rpicam-apps-lite which is installed by default:

sudo apt-get install rpicam-apps

The script you’ll create a script called livevid:

#!/bin/bash

date

while [ 1 == 1 ]; do
    rpicam-vid -v 0 -n -t 0 -g 10 --bitrate 2500000 --inline --width 960 --height 540 --framerate 30 --codec libav --libav-format flv --libav-audio 0 --audio-bitrate 8600 --av-sync 200000 -n -o 'rtmp://url_of_nginx/live/cam' --awb daylight

done;

And your crontab entry (crontab -e)

@reboot cd /home/user;/usr/bin/nohup /home/user/livevid &

With this, you can at least alter the parameters to make the image quality better, or use less bandwidth.

Leave a Reply

You must be logged in to post a comment.

Entries (RSS) and Comments (RSS).
Cluster host: li