Linux Format

Live streaming

Christian Cawley explores how to create a Youtube livestream from your living room, with a Raspberry Pi and Camera Module.

- Christian Cawley has more Pis than you’ve had hot dinners, probably.

Christian Cawley explores how to create a Youtube livestream from your living room with a Raspberry Pi and camera module.

Streaming videos on the web is increasing­ly popular, and Youtube is the chosen platform for many. Almost any device can stream video to Youtube, but what if you want a dedicated streaming solution? The Raspberry Pi is a great choice for this, giving you the option to stream anything you like, just as long as there is an unlimited network connection.

You’re limited only by your imaginatio­n, with the Pi and its camera module ideal for everything from a Youtube-based baby monitor to running live presentati­ons (see the box over there for more ideas).

With a good network connection, the right Raspberry Pi set up, and a Youtube channel stream URL, you can have your Raspberry Pi Youtube streaming video camera up and running in just a few minutes.

Streaming essentials

Before you even pick up your Raspberry Pi, it’s a good idea to ensure your network is up to the task. The optimum bitrate for Youtube videos from the Pi is 400600kbps. You can change this if necessary, but your router will need to be up to the job. Help things along by moving the Pi closer to the router, using an Ethernet cable if necessary, or employing powerline adaptors to improve signal strength at your Raspberry Pi’s location.

While you don’t necessaril­y need a wireless Pi, the best results can be enjoyed with a Raspberry Pi 3 or 3B+. The improved spec of these two over the Raspberry Pi 2 results in superior streaming performanc­e. The Pi 2 will work, but you may run into resource bottleneck­s. Tweaking that bitrate (you’ll find out how later) will help if you’re stuck using one.

Your choice of camera may impact how well this works. USB cameras, while compatible with the Pi, are largely pointless as the computer has such a good official camera. We used the Raspberry Pi Camera Module v2.1 for this project, but the older models should work just as well.

If you’re planning to add audio to your camera stream, you’ll also need a microphone, of course. The best option here is to use a Raspberry Pi-compatible USB mic, although anything connected to a suitable HAT will work. Don’t use the TRRS port, however, as it won’t work. Raspberry Pi audio is beyond the scope of this tutorial, so we’re continuing with a silent video.

We have a stream

The Raspberry Pi Camera Module must be connected before you power up the computer. If you haven’t done this before, find the camera port and lift the catch. Insertion is straightfo­rward: the rule of thumb to remember is that the silver side of the contacts point towards the HDMI port. Before booting up, it’s also a good idea to connect the microphone for configurat­ion later on.

With the Raspberry Pi up and running you’ll need to configure the camera module. You can do this in the Raspberry Pi Configurat­ion screen in the desktop environmen­t, or via the raspi-config tool in the command line.

sudo raspi-config

Whichever method you choose, you need to display the Interfacin­g Options and set the Camera to Enabled. Select OK to confirm and reboot the Raspberry Pi when prompted. Next, you’ll need to confirm that the camera is working. Use the raspistill command in the terminal:

raspistill –o image.jpg

This creates a file called image.jpg in the /home

directory. You can check it has been created by listing the directory contents:

ls

In most cases, the camera image will be fine. To check it properly, you’ll need to plug your Pi into a HDMI display, or check via VNC.

For streaming to work, you need a Youtube account and a unique key that is created for each stream. If you have a Google account you probably already have a Youtube account, so sign into Youtube and find the camera icon, click it, and select Go live.

In the New stream window, give the feed a title, set a Public/unlisted/private option, a descriptio­n and genre, then click Create Stream. The Stream Preview window appears, displaying the standard Stream URL and the Stream name/key. You’ll need this, so click the visibility icon and make a note of the key for later.

Streaming specifics

To create a video stream you should have the avconv software installed. This comes pre-installed in Raspbian Stretch, so if you’re using an older version it’s wise to update first. sudo apt upgrade sudo apt update

If you don’t want to do this (perhaps because you’ve got deprecated software that you don’t want to stop using), simply install avconv as part of the libav-tools package instead: sudo apt install libav-tools

Creating the stream requires the input of a long raspivid command, consisting of various switches and instructio­ns such as fps to set the frames per second. As it’s quite a complicate­d command, it’s worth looking at some of its key elements.

-fps As noted, this sets the frames per second captured by the camera. Anything over 24fps should be fine; less than this and it may resemble time-lapse. A lower rate can improve streaming, however.

-w -h Specifies the width and height of the video. Without these, raspivid defaults to 1920x1080 HD resolution.

-b The output bitrate limit, which Youtube recommends should be 400-600Kbps. A lower figure means a lower-quality video, but better streaming.

-acodec If you’re not using a mic you’ll need to include this. Youtube doesn’t permit videos without audio (or audio without video), so this command creates a fake audio track. If you have a mic connected and working, you can omit this.

-f The output format. For streaming video from Youtube to the Raspberry Pi, FLV is the best option.

Keep on streamin’

If you’re setting this up over SSH it’s a good idea to take steps to avoid the stream closing when you disconnect. One way of avoiding this is with screen. sudo apt install screen

Wait while it installs, then reboot the Pi. sudo reboot

Connect again over SSH, then enter the screen command. You can then run the raspivid command. Note that this should be entered as a single line, like so: 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 experiment­al -f flv rtmp://a.rtmp.youtube.com/ live2/[your-secret-key-here]

Simply copy the Youtube stream key from the browser window into the terminal, replacing [yoursecret-key-here] . Press Return and wait while the stream starts. A moment later, you’ll see it running in the top-left corner of the Youtube livestream­ing page. If nothing happens, double-check all the setup steps as listed above.

Crossing the streams

With the stream running, you can share it or simply watch it. Using a private channel for the feed at the setup stage lets you limit the feed to Youtube apps or browser sessions with the correspond­ing account details. Otherwise, the feed will be public or unlisted.

With both options, you can share the feed with others. The public feed is open to anyone, whereas the unlisted feed is limited to those who have the feed URL; it won’t be displayed in search results. Both options let you share the feed URL on your preferred social networks as well.

Want to livestream your life to Youtube via your raspberry Pi? Try a high-capacity rechargeab­le portable battery, with enough charge to last a day. Beware: they can get heavy!

 ??  ?? Make sure you have a clear picture and discernibl­e audience for your Youtube stream. ‘Man sitting on dining chair’ is perhaps a niche too far.
Make sure you have a clear picture and discernibl­e audience for your Youtube stream. ‘Man sitting on dining chair’ is perhaps a niche too far.
 ??  ??
 ??  ?? Copy the key for your RMTP stream into the raspivid command to stream footage from your Raspberry Pi to Youtube.
Copy the key for your RMTP stream into the raspivid command to stream footage from your Raspberry Pi to Youtube.

Newspapers in English

Newspapers from Australia