Friday, May 24, 2019

How to Live Stream to YouTube With a Raspberry Pi

stream-youtube-raspi

By adding a camera module to your Raspberry Pi, you essentially get a portable, lightweight, and easy-to-hold-or-mount internet-connected camera.

So, it makes sense that you might want to stream footage with it. But how do you get started with this? Which Pi model should you use? Is one camera module solution better than another? And how the do you get the footage onto YouTube?

As with most things Raspberry Pi, it’s remarkably straightforward.

Why Live Stream With a Raspberry Pi?

With so many different devices capable of streaming to YouTube, you might well be wondering “why choose the Pi”?

Well, its size certainly comes into play, enabling you to position the Raspberry Pi in almost any position. Using the Pi as a dedicated YouTube live streaming camera frees up your other devices, too.

And then, there’s that age-old reason: because you can! Setting up the Pi as a live video streamer gives an appreciation of what is going on in the background on other devices performing the same task. It’s a little bit untidy, requiring a long command string, but the result is satisfying.

What You Will Need

To live stream whatever is in front of your Raspberry Pi to YouTube, you’ll need the following:

  • A Raspberry Pi 3 or later.
  • Raspberry Pi Camera Module (original or NoIR revision, either is fine). (While a USB webcam can be used, these instructions assume a Raspberry Pi Camera Module is in use.)
  • Portable battery supply (optional).

For the operating system, the standard Raspbian Stretch will be fine. But you might prefer Ubuntu or Arch Linux, or any of the other Raspberry Pi distros currently available.

Next, connect the camera and boot up. Our previous guide to setting up the Raspberry Pi Camera Module explains how to do this correctly.

You’ll also need a YouTube channel, for streaming your footage to. This isn’t as difficult to set up as you might think.

Set Up Your YouTube Channel

You probably already have a YouTube account. If you use Google Mail, there is an account ready for you to activate. You’ll need a special URL from here that directs the footage captured by the Raspberry Pi’s camera to YouTube.

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

To find this, head to YouTube, sign in, and look for the Upload button. This is what you would normally use in YouTube to add a video. On this occasion, however, we’re going to ignore this and click Get started button under Live Streaming.

Use YouTube live streaming

In the subsequent screen, fill in the details you want for the live feed. This will be information about the subject of the feed, and a title, which you should add under Basic Info. You’ll also get the chance to set the privacy level of the stream; is it Public, Unlisted, or Private?

Input basic details and description for your stream

In the next tab, Stream key setup, look for the Stream URL and Stream name/key (you’ll need to click Reveal to see this). Note that the Stream key must be kept private—anyone with this information can stream to your YouTube channel!

Grab the rmtp feed link for your Raspberry Pi stream

(Setting up your Pi streaming camera via SSH? Simply copy the stream name/key from the YouTube browser window into your remote Raspberry Pi command line.)

For a look at the other options here, see our guide to setting up a YouTube channel.

Prepare the Raspberry Pi for Live YouTube Streaming

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

Begin by upgrading. This ensures you’re running the most recent version of Raspbian, with all the necessary system and software updates, including raspivid.

sudo apt update
sudo apt upgrade

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

sudo raspi-config

Enable the Raspberry Pi camera module

Use the arrow keys to select Enable Camera, tap Enter, then select Yes. You’ll be prompted to reboot. When your Pi restarts, enter:

raspistill –o image.jpg

You’ll find the resulting snap in the Home directory. Once you know that your camera is working with your Raspberry Pi, you can proceed.

Set Up Streaming With avconv

The most recent versions of Raspbian have avconv preinstalled, so you shouldn’t need to install it. However, if you don’t want to upgrade your Raspberry Pi, you can simply install the libav-tools package:

sudo apt install libav-tools

With avconv installed, you’re ready to create the feed for YouTube. You’ll need the stream name/key that you noted down earlier for this.

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 to it. Now, if you want to go ahead and just run it, then copy the code, paste it into your terminal window, and hit enter. Remember to change [your-secret-key-here] for the Stream key you made a note of earlier.

If everything has worked as intended, you’ll end up with something like this:

Streaming video footage from the Raspberry Pi to YouTube

When this happens, switch back to the YouTube browser tab. A few moments later, the footage will start streaming:

Raspberry Pi video stream to YouTube

What the Stream Command Means

That long command above can be quite confusing to the untrained eye but features a collection of separate parameters. Let’s look at the most important.

-fps: This is the frames per second rate. For the best results it should be over 24, which is the speed movies traditionally ran at in order to create the illusion of movement. If performance is an issue, however, you may prefer to reduce this to improve steaming.

-w -h: These can be used to specify width and height. If you omit them, raspivid will use the full 1920×1080 high definition resolution (1080p).

-b: Output bitrate limit. YouTube’s recommendation is 400-600kbps. A lower figure will reduce upload bandwidth, in exchange for a lower quality video.

-acodec: This one is particularly important for streaming to YouTube. The service doesn’t allow video without an audio track (or audio without a video track) so we use this to create a fake audio track for the stream. As the Raspberry Pi doesn’t ship with a built-in mic, and the best audio results are gained from adding a sound card HAT, this is the easy solution.

-f: This is the output format; in this case it’s flv, the preferred format for YouTube live streams.

Detach Your SSH Session for the Stream to Continue

The raspivid command above initiates a stream, but if you’re connecting via SSH, when you disconnect the stream will close. Surely you can’t leave your PC running just for the Pi to keep streaming?

Fortunately, there is an answer: screen. This is a piece of software you can install that will keep the SSH session running once you disconnect.

Begin by ending the stream (Ctrl + X), then installing screen:

sudo apt install screen

Wait for it to install, then reboot the Pi.

sudo reboot

Reconnect over SSH, sign in, then enter the command to run screen:

screen

This basically creates a separate environment for you to run the raspivid command in, one that will persist when you disconnect. Simply run raspivid as above, then when you’re ready to disconnect hit Ctrl + A.

Close the SSH window, and the stream will continue.

Your Raspberry Pi Camera Is Streaming to YouTube

With the Pi streaming video from the camera, everything should be working fine. All it takes is for you to:

  • Connect the camera module to the Raspberry Pi
  • Position the Pi to capture the scene
  • Run a system update
  • Set up a YouTube channel and copy the stream URL
  • Initiate a stream with the raspivid command

Note that with persistent streaming, there is a chance that things can overheat, which will slow down the stream. If this happens, consider some Raspberry Pi cooling solutions.

Read the full article: How to Live Stream to YouTube With a Raspberry Pi



from MakeUseOf http://bit.ly/2VXkeAc

No comments:

Post a Comment