By adding a camera module to your Raspberry Pi, you essentially have a portable, lightweight and easy-to-use or web-mountable camera.
So it makes sense that you can stream videos with it. But how did you get started with it? What model of Pi should you be using? Is one camera module solution better than another? And how do you get footage on YouTube?
As with most things on the Raspberry Pi, it’s surprisingly simple.
Why live stream with Raspberry Pi?
With the availability of easy-to-use streaming services like Mixer and Twitch, and with so many different devices capable of YouTube streaming, you might be wondering «why choose Pi»?
Well, its size definitely comes into play, allowing you to position your Raspberry Pi in just about any position. Using the Pi as a dedicated YouTube live stream also frees up your other devices.
And now, there is this eternal reason: because you can! Setting up your Pi as a live video streamer gives you an idea of what’s going on in the background on other devices doing the same task. It’s a little messy, requires a long command line, but the result is satisfactory.
What will you need
To stream anything in front of your Raspberry Pi to YouTube, you’ll need the following:
- Raspberry Pi 3 or later.
- Raspberry Pi camera module (original or NoIR revision, or OK). (While a USB webcam can be used, these instructions assume you are using a Raspberry Pi camera module.)
- Portable battery (option).
For the operating system, the standard Raspbian Stretch will be fine. But you may prefer Ubuntu or Arch Linux or any other Raspberry Pi distributions. currently available.
Next, plug in your camera and boot up. Our previous guide to setting up the Raspberry Pi Camera Module explains how to do it right.
You will also need a YouTube channel to stream your footage on. It’s not as hard to set up as you might think.
Set up your YouTube channel
You may already have a YouTube account. If you are using Google Mail, there is an account ready to be activated. From here, you’ll need a special URL that directs the video captured by the Raspberry Pi camera to YouTube.
It is called RMTP address and is basically a specific media URL.
To find this, go to YouTube, sign in and look for the » Download» . This is what you usually use on YouTube to add a video. In this case, however, we are going to ignore it and click on the «Start» button in the «Live Streaming» section.
On the next screen, enter the required details for the live broadcast. This will be information about the topic of the channel and a title that you should add in the » Basic information» . You will also get the option to set the privacy level of the stream; it public , unregistered or private ?
On the next tab Stream key setting find URL thread and thread name/key (to see this you need to click » Show» ). Note that the Stream key must be private — anyone with this information can stream your YouTube channel!
(Setting up a Pi streaming camera over SSH? Just copy the stream name/key from a YouTube browser window to the remote Raspberry Pi’s command line.)
To see other options, see our guide to setting up a YouTube channel. broadcast broadcast
Prepare Your Raspberry Pi for YouTube Live Streaming
Now it’s time to set up your Raspberry Pi for streaming.
Start with an update. This ensures that you are running the most recent version of Raspbian with all required system and software updates, including raspivid.
sudo apt update sudo apt upgrade
This will take several minutes. When finished, open a terminal window and type:
Use the arrow keys to select » Turn on camera» press » Input», then select » Yes». You will be prompted to restart your computer. When your Pi restarts, enter:
raspistill –o image.jpg
You will find the resulting snap in your home directory. Once you know that your camera works with the Raspberry Pi, you can proceed.
Setting up streaming with avconv
Recent versions of Raspbian have avconv pre-installed, so you don’t need to install it. However, if you don’t want to update your Raspberry Pi, you can simply install the libav-tools package:
sudo apt install libav-tools
With installed avconv you are ready to create a YouTube channel. To do this, you will need the stream name/key that you wrote down earlier.
The command, however, is long:
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]
As you can see, it has a lot of elements. Now, if you want to go ahead and just run it, copy the code, paste it into your terminal window, and press Enter. Don’t forget to change [your-secret-key-here] for the Stream key you specified earlier.
If everything works as intended, you will get something like this:
When this happens, return to the YouTube browser tab. After a few seconds, the footage will begin:
What does the flow command mean
This long command above can be confusing to the untrained eye, but contains a set of separate parameters. Let’s look at the most important.
-fps : This is the frame rate per second. For best results, this should be over 24, which is the speed at which films have traditionally been shot to give the illusion of movement. However, if performance is an issue, you can turn it down to improve steaming.
-w -h : These can be used to specify the width and height. If you omit them, raspivid will use the full 1920×1080 (1080p) high definition resolution.
-b : Output bitrate limit. YouTube recommendation is 400-600 kbps. A lower number will reduce download bandwidth in exchange for low quality video.
-acodec : This one is especially important for YouTube streaming. The service does not allow video without an audio track (or audio without a video track), so we use that to create a fake audio track for the stream. Since the Raspberry Pi does not come with a built-in microphone, and the best audio results are obtained by adding a HAT sound card, this is an easy solution.
-f : this is the output format; in this case it is flv, the preferred format for YouTube live streams.
Disconnect SSH session to continue flow
The raspivid command above will initiate a stream, but if you connect via SSH, disconnecting will close the stream. Surely you can’t leave your computer on just for the Pi to keep streaming?