From Bees to YouTube: How I Live Streamed My Local Cam Feed with This Simple Trick!
I want to share my local cam stream with which I monitor my bees to youtube. So For this, I got a problem. The camera does not support streaming native to youtube.
My local video stream
My local cameras are available via real-time stream protocol rstp. My Reolink camera now will be used for this, especially my camera for the bees. The cameras are already attached to an NVR device. So this will help me to provide an RTSP Endpoint.
The Endpoint looks like this:
rtmp://192.168.xxx.xxx/bcs/channel3_sub.bcs?channel=0&stream=0&user=xxx&&password=XXXX
I checked this, by simply adding this URL into a video player. And it will show me the video output.
So the next step is to set up Youtube.
Setup Youtube
To get a video broadcast, I will use Youtube for thi because it's a popular platform and fortunately I have an account on this. To get a Live video stream there you must create a live stream. you will need the key (marked in yellow).
Connect together
To get a working stream pushed to youtube, I must use the RTSP Stream from my camera and publish it to Youtube. That's simple... but before that, we must transcode the stream to a youtube friendly format. Fortunately for this, I can use ffmpeg. This is an all to all converter.
After a little experimenting, I end up using the following command to generate a live stream
This will take the input stream, convert it to the desired target format (audio and video), and push it immediately to the target stream (on Youtube). That's cool. But now I want to use it headless. So that I can run it on my home server and it will push this to my stream.
Going headless
To run the script headless I decide to run it in a docker container. Luckily there is a docker for ffmpeg (jrottenberg/ffmpeg), so I use it for my base implementation.
But I need a little bit of flexibility to get a running image that will get as a parameter my original stream and the youtube output. For this, I wrote a small shell script
#!/bin/sh
if [ "$#" -lt 2 ]; then
>&2 echo "Arguments: IP_CAMERA_ADDRESS LIVE_ID [TIMELAPSE_ID]"
exit 1
fi
if [ ! -d /data ]; then
>&2 echo "Expected Docker mounted volume at /data for recordings"
exit 1
fi
cd /data
IP_CAMERA_ADDRESS=$1
LIVE_ID=$2
>&2 echo "IP_CAMERA_ADDRESS=$IP_CAMERA_ADDRESS"
>&2 echo "LIVE_ID=$LIVE_ID"
exec ffmpeg \
-f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-thread_queue_size 128 -i $IP_CAMERA_ADDRESS \
-shortest \
-vf "fps=30" \
-map 0:a:0 -c:a aac -b:a 16k \
-map 1:v:0 -c:v libx264 -preset veryfast -crf 30 -g 90 \
-f flv rtmp://a.rtmp.youtube.com/live2/$LIVE_ID \
-f segment -reset_timestamps 1 -segment_time 600 -segment_format mp4 -segment_atclocktime 1 -strftime 1 \
"%Y-%m-%d_%H-%M-%S.mp4"
When you run it in a Linux shell it will do a parameter validation. So it requires two parameters
- The rtsp Url from your camera
- The key to youtube live streaming
Now it's time to build a small image that will use this script as an entry point. I end up using this Dockerfile
FROM jrottenberg/ffmpeg
MAINTAINER Sascha Bajonczak <xbeejayx@hotmail.com>
ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT /entrypoint.sh $@
This will use the base ffmpeg image and add our script to the container. When the container starts it will run the entry point and pass all start arguments as parameters. Now you can build the image. After that, you can use it for your needs.
For those that want a ready-to-use image, I put it on the docker hub for you so you can start it with the following command
docker run --restart=always -d beejay/streamforwarding:latest -- "rtmp://192.168.xxx.xxx/bcs/channel3_sub.bcs?channel=0&stream=0&user=admin&&password=xxxx" r3kh-XXXX-XXXX-XXXX-XXXX
This will now start a detached docker image, that will take my webcam stream hosted in my network at "rtmp://192.168.xxx.xxx/bcs/channel3_sub.bcs?channel=0&stream=0&user=admin&&password=xxxx" and forward it to the youtube live stream identified with the key r3kh-XXXX-XXXX-XXXX-XXXX.
Here are my result
Current limitations
Yes this is awesome, but there is some limitation. Youtube will stop the stream after a while. So when you have no visitors, you will be get stopped and it must be restarted by hand. But stay tuned, I have an idea to keep it online.
Conclusion
You learn how easy it is to integrate an existing camera stream into a live stream. So now you are able to broadcast some videos to the (youtube)world. Instead of using an expensive setup with OBS-Studio or other fancy things, you can get it right for free.
You find all of the code on my github page here: SBajonczak/rtsp-to.youtube: A small docker definition, that will forward the rtsp stream from a local camery in your network and put it out on a youtube livestream (github.com)
I hoped you enjoyed this article and I'm happy if you leave me a comment and subscribe!