This blog describes how to stream video + audio to Youtube Live events using Raspberry Pi. There are lot of sites online that describe how to Live stream a webcam video to Youtube with Raspberry Pi but no where I found working solution for live streaming both Audio & Video to Youtube. Hence this blog..
Bit of background:
I work for a non-profit organization that needed a free 24/7 webcam streaming. They were using VLC in tandem with Quicktime broadcasting for last 10 yrs that worked great until the recent advent of new HTML5 and Mobile platforms. It became obsolete especially due to its need for Quicktime browser plugin and lack of support for Mobile platforms.
After some research, Youtube Live events came out to be the only free streaming solution. Initially I tried using Adobe Flash Live encoder to live stream to Youtube but its a User-interface driven tool and not a fire-and-forget type solution. This where Raspberry Pi fit the job.
Raspberry Pi 2: a tiny, cheap yet savory unix box
- Raspberry Pi 2 or 3
- SD card to load Raspbian OS
- Camera component
- WiFi adapter (only if its Raspberry Pi ver 2. WiFi & Bluetooth are built-in to Raspberry Pi from ver 3+)
- USB Audio adapter
- USB Micro-phone
- Power adapter
- Raspberry Pi case (optional)
- Keyboard, Mouse, HDMI cable, Monitor. This is optional and only needed until you setup SSH. Alternatively, you can use this cool PiBakery project to burn SSH and WiFi info directly into your SD card thus avoiding needs for initial peripherals to boot up RPi.
Here is the command that works to stream both Audio and Video:
sudo /usr/bin/raspivid -o - -t 0 -w 1280 -h 720 -n -fps 25 -b 600000 -rot 180 | /usr/local/bin/ffmpeg -f h264 -r 25 -i - -itsoffset 5.9 -f alsa -ac 1 -i default -vcodec copy -acodec libfdk_aac -ac 1 -ar 44100 -b:a 32k -map 0:0 -map 1:0 -f flv rtmp://a.rtmp.youtube.com/live2/
- You need to install the ffmpeg onto your RPi in order use above script. I’ve detailed its setup in my blog here.
- For some reason, Audio starts falling behind the Video after about 20 mins. To give better user experience, I setup a Cron job to re-start above ffmpeg script code every 20 mins. Since Youtube does DVR of your live streaming, users won’t notice any difference.
Here is its screenshot: