Raspbery Pi IP Camera - Part III
Now that I have a video feed, lets make is so that thousands of people can watch live. For arguments sake, my most important viewers are from Australia. In this case, it makes sense to use the Cloudflare CDN since its basically plugged in at the ISP level.
For North America and Europe bunny is meant to be way cheaper and more video-centric as well.
Cloudflare Streams
Since I have a Cloudflare account already, I converted to the paid streaming add-on to test out the system. There are two types of interesting streaming you can do:
- Upload a video file(s) and Cloudflare Streams can distribute it for you, and transcode for you
- Stream live video
Lets look at streaming live video. We already have most of what we need up and running with MediaMTX providing the Raspberry Pi Camera feed, so all we have to do is publish the feed to Cloudflare Streams - RTFM.
Follow the guide Stream live video
-> Start a live stream
to create yourself a test stream. Do not check low-latency or you will need to play with FFMPEG command.
FFMPEG
With a paid plan, in theory all we need to do is grab data from MediaMTX and send it to cloudflare’s upload service directly. As this is a push operation, there is no need to do anything with Cloudflare tunnels or mess around with firewalls. Other approaches involve saving and forwarding files for HLS.
There is a bit of trial and error to getting FFMPEG to place nicely with Cloudflare. If you mess up the command, your stream will typically work for a few seconds and then display a spinner forever. Cloudflare really didn’t like -c copy
and also needs keyframes at 4 seconds (less if you want to enable low-latency).
Keyframes need to be expressed in frames which you can work out like this:
Step 1. Find frames per second:
ffprobe rtsp://localhost:8554/cam
Step 2 - Do Math (keyframes every 4 seconds):
30 × 4 = 120
Run it
Now we are ready to build our command line, here’s mine:
ffmpeg -re -i rtmp://localhost:1935/cam -c:v libx264 -c:a aac -preset ultrafast -b:v 1000k -f flv \
-g 120 -keyint_min 120 \
"rtmps://live.cloudflare.com:443/live/<RTMPS Key>"
rtmp://localhost:1935/cam
is the URL of your video feed in MediaMTX-preset ultrafast
was needed to make sure speed=1.0 as the pi encoding was lagging-g 120 -keyint_min 120
is the keyframe infos<RTMPS Key>
can be found on the live stream connection info tab- If FFMPEG dies, your stream is over. Since this is a one-off test, I’m just running it in tmux
Clicking around the Streams UI in the Recordings tab for your stream, find the one marked “live” and click “Video link” this is the streaming video URL which you can embed in your web page or share with the world.
In theory, you can blast this URL with as many viewers as you want and should be fine.
Test
I was able to stream just fine. There is about a minute or so delay in streaming which is fine for most applications.
This ends up being a short blog post because most of the hard work is done in the CDN and all you really have to do is figure out FFMPEG.