Video Live Stream on YouTube from a Raspberry Pi
How to live stream video from a cheap IP camera to YouTube using a Raspberry Pi.
For a long time I wanted to set up a video stream of my hometown, but it was prohibitively expensive. Recently, I thought about that idea again, and realized that a lot has changed since the first time I had it.
I found online comments about Reolink cameras being a good choice for quite a low price, so I ordered a Reolink RLC-511W IP camera to test. This model is able to stream a 5MP video over WiFi, supports RTSP and costs only ~100 € (DE Amazon, US Amazon - affiliate links - As an Amazon Associate I earn from qualifying purchases.). It has a 4x optical zoom, IR for night mode, works on both 2.4 and 5 GHz, and can even be connected and powered over PoE instead of the provided power adapter.
Next, I wanted to see if the Raspberry Pi 4 (DE Amazon, US Amazon - affiliate links), which I had initially planned for another project, was strong enough to take the camera's RTSP stream and transfer it live (almost realtime) as a 1:1 copy to YouTube. It is normal for live streams on YouTube and most other platforms to have a lag of ~30 seconds, and with this setup I was able to achieve a live stream with ~15 seconds delay, while the load on the Raspberry Pi was only about 0.3.
First I installed ffmpeg, the standard tool in opensource video processing, and created a stream.sh file on the Pi, containing the following commands:
#! /bin/bash
STREAM_KEY="****-****-****-****-****" # YouTube Stream Key
CAMERA_USERNAME="test"
CAMERA_PASSWORD="test"
CAMERA_IP_ADDRESS="192.168.1.33"
RTSP_SOURCE="rtsp://$CAMERA_USERNAME:$CAMERA_PASSWORD@$CAMERA_IP_ADDRESS:554"
YOUTUBE_URL="rtmp://a.rtmp.youtube.com/live2" # Base YouTube RTMP URL
VBR="5200k" # Bitrate
USE_TCP=1 # Use TCP transport for RTSP, otherwise UDP
MUTE_AUDIO=0 # Discard audio from the camera
args=()
(( USE_TCP == 1 )) && args+=( '-rtsp_transport tcp' )
args+=( '-i' $RTSP_SOURCE )
(( MUTE_AUDIO == 1 )) && args+=( '-f lavfi -i anullsrc' )
args+=( '-b:v' $VBR )
args+=( '-tune zerolatency' )
args+=( '-preset veryfast' )
args+=( '-vcodec libx264' )
args+=( '-pix_fmt +' )
args+=( '-c:v copy' )
args+=( '-c:a aac' )
args+=( '-strict experimental' )
args+=( '-f flv' $YOUTUBE_URL/$STREAM_KEY )
echo "Starting ffmpeg ${args[@]}"
ffmpeg ${args[@]}
Make sure to update the variables to your specific camera and YouTube credentials and IP address of the camera.
Feel free to tune the ffmpeg command parameters, as it is one of the most flexible video processing tools, but these settings worked for me. The Raspberry Pi handles this easily.
This bash script takes the configuration, runs ffmpeg and tells it to connect to the camera, copy the stream without doing any video processing and output it to YouTube's RTMP endpoint.
Both the Raspberry Pi and the camera are connected to the network wirelessly, so the only cable the camera needs is for power.
You can watch the live stream here. Now I and everyone else can see what's happening in my hometown of Sombor, Serbia from anywhere in the world.