How I created a YouTube live stream of my guinea pigs

Hi there. My name’s James. I have two guinea pigs, Rocky and Marvin, who recently turned 3.

Marvin (Left) and Rocky (Right)

One day I had this really cool idea to stream them on YouTube so that I could watch them on my break at work, and also so I could capture them lounging about their new mansion in all of it’s glory.

Said Mansion. The webcam is currently placed on the plastic hut as it shows the cage off well and they can’t get up there since I still need to build them a bridge.

The Hardware

The Computer

The first step is a computer to process it. I didn’t want my PC to be on all the time and would like to use my MacBook without a webcam plugged into one of it’s 4 ports so decided to use a Raspberry Pi 3, which retail for around £35. That doesn’t include a power cable, display cable or a case, so I bought something similar to this which gave me everything I needed.

The Camera

Then, I needed to choose a camera. I was considering the Raspberry Pi Camera module which would work excellently and wouldn’t require much extra work, but I didn’t want to have a camera I could only use for one thing and limited me to where the Pi could be placed. Amazon Prime day rolls around and I got a great deal on the Logitech C920, a very good webcam which was currently very cheap. I ended up going for this webcam for my Pi. If you can get a good deal on this webcam, go for it. It’s great.

The additional hardware

You’re also going to need some additional hardware if you want a Streaming LED and a button. I originally had these attached to a breadboard, now they’re just soldered together. I got an LED, a button and a resistor. I know very little about electronics but the ones found in my untouched Arduino Uno kit work perfectly.

The basic hardware was here for the project, now I just needed the software.

Configuring The Hardware

It’s surprising I’m not a graphic designer, I know.

Configuring the hardware is relatively easy. The first thing is to connect the LED and Button to the Raspberry Pi. I did not have the correct cables so just twisted the ends of the wire i stripped with some tweezers so it hugs the pins. Note that the resistor needs to be connected to the long side of the LED. Please follow my incredibly scientific diagram seen above. Please don’t do this while the Pi is on as bridging some of the connections has caused my Pi to short circuit and turn itself off.

Mask on
F*ck it, Mask off.

As you can see in the two above photos, this is what my final set up looks like. Surprisingly, the LED just fits in the little hole in the raspberry logo.

Now you should connect the webcam, display, keyboard (and mouse if you feel that it is necessary) and plug in the power to boot up the Raspberry Pi. If you’re using wired internet (recommended but not required — I use wireless) connect your Ethernet cable as well

You’re going to need the latest version of Raspbian. You can download that here. Follow the website’s instructions to install it on your Raspberry Pi, some bundles come with the operating system pre loaded. (I’m using a 3B here, for no particular reason, it’s just the one that I own.)

You might see the LED faintly glow up as the OS boots, this is normal.

Now we’re going to enable the Logitech C920 webcam. To do this, navigate to the terminal (press the Windows Key on your connected keyboard, navigate to Accessories > Terminal (using the arrow keys and enter) and open up Terminal)

Now you need to run the following command:

sudo raspi-config

A big grey and blue menu will appear on screen, again using the arrow keys navigate to option 6 “Enable Camera” and press enter, then choose “Enable”.

That’s it, now your hardware is configured.

The Software

Thankfully, someone on the internet was trying to do a pretty similar job to what I was doing. They were using a Logitech C920 as well, which turned out to be an advantage as it has onboard H.264 encoding. If I’m honest, I’m not entirely sure why that’s so great but basically, the webcam does most of the processing and the Pi just sends off the data to YouTube.

When you’ve got the Operating System up and running on your little Raspberry Pi, open the terminal (press the Windows Key on your connected keyboard, navigate to Accessories > Terminal (using the arrow keys and enter) and open up Terminal) then run the following commands:

cd /home/pi/
sudo apt-get install libomxil-bellagio-dev -y
git clone
cd FFmpeg
sudo ./configure — arch=armel — target-os=linux — enable-gpl — enable-omx — enable-omx-rpi — enable-nonfree

This installs FFMPEG, and essential add on for what we are doing. This will allow us to stream to YouTube.

Next up, we’re going to write (well, you’re going to copy and paste most likely, in which case I suggest installing Chromium for Raspberry Pi then installing the Pushbullet extension so that you can send text and images between your devices. This saved me plugging in my keyboard and mouse to and fro quite a lot. I suggest you use it simply for the simplicity.

Next up we’re going to be coding with Python. Luckily I had some good experience with it as I learnt it in school but unluckily it was slightly different as we’re running an operating system based off of Linux, and the most important part of our code uses GPIO pins. These are pins located on the Raspberry Pi for tinkerers to mess with and can even power small touch displays.

This is the Python code.

I have annotated it (the writing after the hash/pound sign (#)). There is also some original annotation from the code I was “inspired by” (read: Stolen but with extra shiny bits) Click here to see the original code. To open up Python, Press the Windows Key on your connected keyboard, navigate to Programming then select Python 3 (IDLE) it must be Python 3 as it has a slightly different way of executing code and is incompatible with Python 2.

import RPi.GPIO as GPIO #This let’s us use the GPIO pins on the Raspberry Pi
import time #This lets us create pauses in our code
import os #This lets us run the long command
LedPin = 11 # pin11 — — led
BtnPin = 12 # pin12 — — button
Led_status = 1
def setup():
 GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
 GPIO.setup(LedPin, GPIO.OUT) # Set LedPin’s mode is output
 GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin’s mode is input, and pull up to high level(3.3V)
 GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led
def swLed(ev=None):
 global Led_status
 Led_status = not Led_status
 GPIO.output(LedPin, Led_status) # switch led status(on →off; off →on)
 if Led_status == 1:
 print(“Cancelling Livestream… (Warning, I haven’t coded this in yet…)”) #Note that this will never be seen because it runs in the background. It also doesn’t stop it. I’m still figuring out how to stop the stream apart from yanking out a couple of cables
 print(“Stream is starting…”) #Again, this won’t be seen but if you need to diagnose the code it’s good to see what’s working
 print(“Livestream LED is turning on…”)
 os.system(“ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f v4l2 -codec:v h264 -framerate 30 -video_size 1920x1080 -i /dev/video0 -codec:v copy -f flv rtmp://”) #Never share your stream key, it will let anyone stream on your account.
def loop():
 GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=swLed, bouncetime=200) # wait for falling and set bouncetime to prevent the callback function from being called multiple times when the button is pressed (I have no idea what the hell this means)
 while True:
 time.sleep(1) # Don’t do anything
def destroy():
 GPIO.output(LedPin, GPIO.HIGH) # led off
 GPIO.cleanup() # Release resource
if __name__ == ‘__main__’: # Program start from here
 except KeyboardInterrupt: # When ‘Ctrl+C’ is pressed, the child program destroy() will be executed. (again, this only works when you open the script yourself and don’t have it run automatically at startup

Save this code as whatever you’d like and save it again, wherever you’d like too. To make this simple, we’re going to name it (.py is Python, the programming language we’re using,’s file extension.)

You’re probably wondering if I even needed half of that code, and to be completely honest I don’t think I need it, I was just so happy to get it working that I didn’t dare modify anything just in case I was stuck diagnosing it and wanting to give up for another 4 days.

As you can see, I was very happy to have the stream working (The bottom right thumbnail)

But what does this code MEAN?

Basically, when the code is executed (which will be at the startup of the Raspberry Pi), it will be waiting for the button to be pressed. In turn, this will turn on the LED which acts as a signal that the stream is on. The LED being on then triggers some more code which will send a command to the Pi’s Terminal, which will then turn on the webcam and send it’s video data off to YouTube.

ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f v4l2 -codec:v h264 -framerate 30 -video_size 1920x1080 -i /dev/video0 -codec:v copy -f flv rtmp://

This is the code that will be sent to the terminal that will run the stream. I advise that you run this code on it’s own to make sure it will work, which, let’s be honest, it probably won’t :(. The stream also sends a dud (blank) audio stream to YouTube because otherwise YouTube would reject the stream. Nothing will be heard by your viewers which is exactly what I wanted on this stream. This is because the Guinea Pigs’ Room (yes, a whole one to themselves!) is right next to a bathroom and is in a busy house. The Pi sends out 30 frames a second to YouTube at a resolution of 1080p. If you’re seeing dropped frames (stickiness in your video), consider lowering these numbers. This will use up quite a lot of bandwidth.

Remember that Python script? We’re now going to make it open on the startup of our Raspberry Pi. This is how I can just leave my Raspberry Pi plugged in on a shelf and press a button to make it come to life and start streaming.

Type in the following line into the terminal (Windows Key, Accessories, Terminal):

sudo nano /etc/rc.local

now you need to scroll all the way to the bottom with the arrow keys. You should see the line “exit 0”. Right above this line, type the following:

sudo python /home/pi/

This is assuming that in the previous steps you named the file the same as mine and placed it into the same directory.

Now press Control + X to exit the editor nano, press Y then hit enter.

Now the Python file will open on startup.


Now comes the great satisfaction or the annoying sigh of failure. Let’s test the stream! Reboot your Pi by pressing the Windows Key, navigating to Shutdown, then choosing Restart. You might have to wiggle the arrow keys and press tab a couple of times to select restart. (If you can’t do it, press escape, then open terminal and type in “sudo reboot”

Sit back and wait until you boot back into your desktop.

As your Pi boots you may see the LED glow gently which should fade by the time you’ve reached the desktop. Now you can press the button you connected over the GPIO pins and see if YouTube gets the stream. You’re going to have to constantly refresh the home page of the YouTube channel you’re streaming on, and you can also subscribe on another YouTube channel (you can create multiple under the same Google / YouTube account) and enable notifications so that your phone or browser notifies you. If the light shines and the camera starts streaming, you’ve done it. Congratulations.

Quirks, Bugs and other weird things

The first thing you’re likely to notice is that sometimes the light comes on when you press the button but it doesn’t stream. Press the button again and the light will go out. Pressing it again seems to do the trick for me, if not, wait a couple of seconds just in case the Pi hadn’t finished booting up yet. Quite a lot of the time for me the stream won’t start and I’ll have to press the button a bunch of times (just spam it, it doesn’t matter) and then it will start. You know the stream has started because the light will not go out when you press the button again. If anyone knows how to hook up a button to turn the whole thing off without yanking out of the power, please let me know.

That’s it!

If you’d like to contact me, my Twitter handle is the same as my Medium name (llamafiuff) which is the best and easiest way to contact me. You can also try PM’ing me on my Reddit which is /u/CoolJWR100