Raspberry Pi Streaming to YouTube
Raspberry Pi Streaming to YouTube
Problem: Working all day, I miss my ducks(and chickens too). I want to see what they are up to.
Solution: Take a raspberry pi and connect a camera to stream the video to YouTube.
Parts: Raspberry Pi Camera (connect to the camera port on the raspberry pi) WiFi adapter (if not already on board) YouTube account
One line of code to push to YouTube
Following the guide here Live Stream via Raspberry Pi
raspivid -o - -t 0 -vf -hf -fps 30 -b 6000000
| avconv -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero
-f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental
-f flv rtmp://a.rtmp.youtube.com/live2/[your-secret-key-here]
Let’s talk about some monitoring now. WIFI signal can go out and this script will totally stop sending to YouTube and will not resume. Placing the streaming code into a script, we can do some tracking on it.
first: —Actual Script to stream
ALL_THE_YT_STREAMING_CODE &
echo $! > /var/run/duckwatcher.pid
It’s really bad habit to place the pid file in the run directory of /var/. I would recommend someplace else, but for this coding example it is here for your quick review.
–Watchdog Use crontab -e and add this new watchdog script to run each minute(or whatever frequency you want). This does a really simple check from the previously stored PID file. Is that process running? Yes, Do nothing No, Start up the script. Wait 1 minute until next run(handled by crontab)
#!/bin/bash
service=ALL_THE_YT_STREAMING_JUNK
pid=`cat /var/run/duckwatcher.pid`
if [ ! -e /proc/$pid -a /proc/$pid/exe ];
then
/etc/init.d/$service start
else
echo "$service is running!!!"
fi