I recently learned that YouTube now allows you to live stream 24/7 as a webcam. This is great news, having setup many webcams for businesses in the past i know the struggle of “live streaming” and the resources it can take, in the end mostly going with a still image refresh every 10-15 seconds.
Now that YouTube allows 24/7 streaming i wanted to try it out and i had a specific use for it also. I had a old first generation Raspberry Pi and pi camera gathering dust behind the TV so i dug it out and played around with ffmpeg but i think the webserver i had running was taking up too much resources to also be able to run a webcam, in the end i found a great docker solution here: https://blog.alexellis.io/live-stream-with-docker/
There were no 100% complete settings that were working for me and it took some time to find the sweat spot of quality and reliability, at first i was lowering and lowering the quality settings and it would still buffer on YouTube, but that low quality turned out to be the problem.
Anyway, if you just came here to figure out what settings i am using, this is what i did:
1. I followed Alex’s tutorial here to install the docker image: https://blog.alexellis.io/live-stream-with-docker/
2. Then used his gist to copy the congig file, i lso needed to change the file permission to get it to work right https://gist.github.com/alexellis/b86a91225eabd004573fe09da3fb34b2
3. My new settings which seem to keep quality and use about 1mb of bandwidth (of my 20mb upload – yes its your upload speed not your download speed this uses)
docker rm -f cam
echo Live-stream secret: $1
raspivid -o - -t 0 -w 1920 -h 1080 -vf -hf -fps 25 -b 0 -g 25 -qp 35 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i pipe:0 -c:v copy -c:a aac -ab 128k -g 25 -strict experimental -f flv -r 25 rtmp://a.rtmp.youtube.com/live2/$1
I would recommend using the pi cam v1 as you get full width native 1080p which you can’t get with the pi cam v2 direct without resizing which uses more resources. If you are using the pi cam v2.1+ then the above command will only use a zoomed in center portion of the camera, to use the native full width you can use the 16:9 (720p resolution)
-w 1640 -h 922 or -w 3280 -h 2464 which gives you a 4:3 ration and apparently only supports 15 fps, info here: http://picamera.readthedocs.io/en/latest/fov.html#camera-modes
if using the pi cam v2+ at 720p you can afford to increase the quality to around 32/30 and still keep below about 1.5mb of bandwidth.
The docker command to mentioned in the tutorial to auto restart on reboot only seemed to work if you did a soft reboot from terminal and not from a power reset reboot, to fix this i added a “startup.sh” file.
Work from power reset
To make everything work from a power reset i did the following:
1. Edit the pi rc.local and add our own startup script
sudo nano /etc/rc.local
I added this line before the exit command:
sudo bash /home/pi/startup.sh &
2. Create the startup.sh file:
cd ~ (to take us to the pi home directory)
sudo nano startup.sh (create the file)
# remove the cam container incast it is already there
printf "Removing cam container"
docker rm -f cam
# start the live stream
printf "Starting live stream, oh yeah!"
docker run --privileged --restart=always --name cam -d alexellis2/streaming:latest xxxx-xxxx-xxxx-xxxx &
(replace the xxxx… with your youtube live key)
3. After saving the file you need to make it executable:
sudo chmod +x startup.sh
And now the cam will start livestreaming straight from boot 🙂
Applying new settings
To apply new settings i do this:
sudo nano entry.sh
You may also need to make the file executable:
sudo chmod +x entry.sh
I then add my new settings and save changes.
2. Rebuild the container
sudo docker build -t alexellis2/streaming .
3. Run the docker cam container:
docker run --privileged --restart=always --name cam -ti alexellis2/streaming:latest xxxx-xxxx-xxxx-xxxx
(the xxxx-xxxx-xxxx-xxxx being the youtube streaming key.
This was running fine on my Pi v1.2 but i ended up buying a Pi zero w that came with a neat little case that also holds the camera.
So now i have a tiny live streaming webcam that will auto boot to live streaming to my YouTube channel 🙂