How to stream live to YouTube with Raspberry Pi

By adding a camera module to the Raspberry Pi, you basically get a mobile Internet camera, compact and easy to hold or attach somewhere.

By adding a camera module to the Raspberry Pi, you basically get a mobile Internet camera, compact and easy to hold or attach somewhere.

If you want to stream footage with this module, do you know where to start? Which Pi model should you use? Where is the camera module solution better in this case? And how do you live streaming YouTube footage? As with most things on Raspberry Pi, these things are very simple.

Guide to streaming videos to YouTube with Raspberry Pi

  1. Why choose live streaming with Raspberry Pi?
  2. What do you need to prepare?
  3. Set up YouTube channel
  4. Prepare Raspberry Pi to stream directly to YouTube
  5. Establish transmission with avconv
  6. What does the stream command mean?
  7. Split session SSH to play online

Why choose live streaming with Raspberry Pi?

With so many different devices capable of streaming live on YouTube, are you wondering why you chose Pi?

First, the size of the Raspberry Pi certainly works, allowing you to position Raspberry Pi in almost every position. Secondly, using Pi as a dedicated YouTube live camera also frees other devices.

Operations can be a bit lengthy, requiring a long series of commands, but the results will definitely satisfy you.

What do you need to prepare?

To stream anything with the Raspberry Pi directly to YouTube, you'll need the following:

  1. A Raspberry Pi 3 or above.
  2. Raspberry Pi camera module (original version or NoIR are available). (Although you can use a USB webcam, these instructions assume you are using the Raspberry Pi camera module).
  3. Backup battery power (optional).

For the operating system, standard Raspbian Stretch will be fine. But you can also choose Ubuntu or Arch Linux or any other Raspberry Pi distribution available.

Next, connect the camera and start up. You will also need a YouTube channel to stream your footage live. This process is not as difficult to set up as you think.

Set up YouTube channel

You may already have a YouTube account. If you use Google Mail, there is an account ready to activate. You will need a special URL from here to transfer the footage recorded by the Raspberry Pi camera to YouTube.

This is called the RMTP address and is basically a specific media URL.

To find this address, visit YouTube, log in and find the Upload button . Here are the steps you usually use in YouTube to add videos. However, in this case, we will ignore this and click the Get started button in Live Streaming.

In the next screen, fill in the details you want for the live feed. This will be information related to the topic of the feed and the title you should add in Basic Info. You will also have the opportunity to set privacy levels for streams ( Public, Unlisted or Private ).

How to stream live to YouTube with Raspberry Pi Picture 1How to stream live to YouTube with Raspberry Pi Picture 1

In the next tab, Stream key setup , find Stream URL and Stream name / key (you need to click Reveal to see this information). Note that the Stream key must be secure because anyone with this information can stream it directly to your YouTube channel!

How to stream live to YouTube with Raspberry Pi Picture 2How to stream live to YouTube with Raspberry Pi Picture 2

(If you want to set up Pi streaming via SSH, just copy the Stream name / key from the YouTube browser window to the command line of Raspberry Pi remotely).

To see other options here, see the tutorial: How to play live video on YouTube? from TipsMake.com.

Prepare Raspberry Pi to stream directly to YouTube

Now, it's time to set up Raspberry Pi for streaming.

Start by upgrading. This step ensures you can run the latest Raspbian version, with all necessary system and software updates, including raspivid.

 sudo apt update sudo apt upgrade 

This step will take a few minutes to complete. Once completed, open a terminal window and enter:

 sudo raspi-config 

How to stream live to YouTube with Raspberry Pi Picture 3How to stream live to YouTube with Raspberry Pi Picture 3

Use the arrow keys to select Enable Camera, press Enter , then select Yes. You will be prompted to restart. When Pi restarts, enter:

 raspistill –o image.jpg 

You will find results in the Home folder. When you know for sure that the camera is working with the Raspberry Pi, you can continue.

Establish transmission with avconv

The most recent versions of Raspbian have been pre-installed with avconv, so you don't need to install it. However, if you don't want to upgrade your Raspberry Pi, just install the libav-tools package :

 sudo apt install libav-tools 

Once avconv is installed, you are ready to create a feed for YouTube. You may need Stream name / key that you recorded earlier for this.

However, the command is quite 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, the command has many factors. Now, if you want to continue and run it, copy the code, paste it into the terminal window and press Enter . Remember to change the [your-secret-key-here] to Stream key that you noted earlier.

If everything works as intended, you'll end up with the following result:

How to stream live to YouTube with Raspberry Pi Picture 4How to stream live to YouTube with Raspberry Pi Picture 4

When this happens, return to the YouTube browser tab. A moment later, the footage will start streaming:

How to stream live to YouTube with Raspberry Pi Picture 5How to stream live to YouTube with Raspberry Pi Picture 5

What does the stream command mean?

The long command above can be quite confusing for beginners, but contains a separate set of parameters. Look at the most important thing.

  1. -fps: This is the frame rate per second (frames per second). For best results, this speed must be above 24. However, if performance is an issue, you may want to reduce this ratio to improve the stream.
  2. -w -h: They can be used to specify width and height. If you ignore them, raspivid will use 1920 × 1080 (1080p) high resolution resolution.
  3. -b: Output bitrate limit (bitrate is the amount of data transmitted over a certain time period). YouTube's proposal is 400-600kbps. A lower number will reduce the upload bandwidth, make the video lower quality.
  4. -acodec: This parameter is especially important for streaming to YouTube. This service does not allow video without audio (or audio but no images), so the example uses this to create fake audio for the stream. Because Raspberry Pi has no built-in mic and the best sound results are obtained, it requires additional HAT sound cards (this is an easy solution).
  5. -f: This is the output format. In this case, it's flv, the preferred format for live streams on YouTube.

Split session SSH to play online

The raspivid command above will initialize a stream, but if you are connecting via SSH, when you disconnect, the thread will close. Surely you can't let the computer run just so that Pi continues to transmit, right?

Fortunately, there is a solution to this problem. It is a screen. This is a software you can install, which helps keep the SSH session running when you disconnect.

Start by ending the stream ( Ctrl + X ), then install the screen:

 sudo apt install screen 

Wait for the installation screen to finish, then restart Pi.

 sudo reboot 

Reconnect via SSH, log in, then enter the command to run the screen:

 screen 

This basically creates a separate environment for you to run the raspivid command, an environment that will exist when you disconnect. Simply run raspivid as above, then when you are ready to disconnect, press Ctrl + A

Close the SSH window and the stream will continue.

With live video from the camera on Pi, everything will work well. All you need is:

  1. Connect camera module with Raspberry Pi
  2. Position Pi to record the scene
  3. Run system update
  4. Set up YouTube channel and copy URL stream
  5. Start a stream with the raspivid command

Note that with continuous streaming, it is possible that all devices may be too hot, this will slow down the stream. If this happens, consider some Raspberry Pi cooling solutions.

Hope you are succesful.

3.9 ★ | 14 Vote