Pinterest Stumbleupon Whatsapp

By adding a camera module (or USB webcam) 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 heck do you get the footage onto YouTube?

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

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 (Model B+ or later).
  • Raspberry Pi Camera Module (original or NoIR revision, either is fine) or a USB webcam. These instructions assume a Raspberry Pi Camera Module is in use.
  • Wireless dongle if using pre-Raspberry Pi 3 model.
  • Portable battery supply (optional).


For the operating system, the standard Raspbian Jessie 5 Ways New Raspbian Jessie Makes Raspberry Pi Even Easier to Use 5 Ways New Raspbian Jessie Makes Raspberry Pi Even Easier to Use Following the release of Debian Jessie in July, the Raspberry Pi community has been blessed with a new release of the Raspbian variant, based on the "parent" distro. Read More will be fine, preferably with the Pixel desktop Upgrade Raspberry Pi's Raspbian OS With the PIXEL Desktop Environment Upgrade Raspberry Pi's Raspbian OS With the PIXEL Desktop Environment Since its release in 2012, the Raspberry Pi's Raspbian operating system has had a few revisions, but the desktop environment had remained largely the same. Pixel changes that. Read More . But you might prefer Ubuntu or Arch Linux, or any of the other Raspberry Pi distros Not Just Raspbian: 10 Linux Distros Your Pi Can Run Not Just Raspbian: 10 Linux Distros Your Pi Can Run You're presumably running the latest version of Raspbian on your Raspberry Pi. But is it the best Linux distro for the device? Could there be an alternative that you might find suits you better? Read More currently available.

You’ll also need a YouTube channel, for streaming your footage to. This isn’t as difficult to set up as you might think, and unlike other solutions, it’s free.

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. We need a special URL from here that we can use to direct the footage captured by the Raspberry Pi’s camera to YouTube, thus streaming it.

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

youtube streaming live

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.

youtube streaming key

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. In the next tab, Stream Options, look for Encoder Setup and copy the Server URL and Stream name/key (you’ll need to click Reveal to see this). Note that the Stream key needs to be kept private — anyone with this information can stream to your YouTube channel!

Prepare the Raspberry Pi for Live YouTube Streaming

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

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

diy picamera enable

Next, connect your camera and boot up. If you don’t have a monitor attached, use VNC to establish a remote desktop connection to the Pi How to Run a Remote Desktop on Raspberry Pi with VNC How to Run a Remote Desktop on Raspberry Pi with VNC What if you need access to the Raspberry Pi desktop from your PC or laptop, without having to plug in a keyboard, mouse and monitor? This is where VNC comes in. Read More , and test the camera. Our previous guide to setting up the Raspberry Pi Camera Module 5 Things You Can Do with the Raspberry Pi Camera Module 5 Things You Can Do with the Raspberry Pi Camera Module One of the first expansions you should buy for the Raspberry Pi is the camera module. With a dedicated connector, the camera can be used for a variety of tasks. Let's take a look. Read More should help here. If you don’t have time for that, open a terminal window and enter:

sudo raspi-config

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

To stream the feed from your Pi’s camera, you’ll need to install avconv. This is part of the libav-tools package, so you can should be able to install it with:

sudo apt-get install libav-tools

Unfortunately, it doesn’t always work that way.

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.

(If you’re doing this via SSH, it will be easier to simply copy the stream name/key from the YouTube browser window into your remote Raspberry Pi command line.)

The command, however, is long. Really 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://[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:

youtube streaming output

When this happens, switch back to the YouTube browser tab. You’ll see something like this:

youtube streaming health

And a few moments later, the footage will start streaming:

youtube streaming stream

Problems? Try ffmpeg

In some cases, avconv won’t push your Pi’s stream to YouTube. If this happens to you, then you should consider using ffmpeg, the precursor to avconv, which was available for older versions of Raspbian.

Although deprecated from Debian, ffmpeg can be downloaded and compiled manually, using these instructions. Be aware that this can take a while, so make sure you’ve got hot drinks and snacks to hand. Or a book to read.

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 x 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 flv, the preferred format for YouTube live streams.

You’re Streaming: What Next?

With the Pi streaming video from the camera, everything should be working fine. But there is a chance that things can overheat, which will slow down the stream. This is particularly likely on older devices, prior to the Raspberry Pi 2, especially if you’ve set a high resolution for streaming.

As such, you’re likely to get better results from the Raspberry Pi 2, and almost perfect results with the Raspberry Pi 3 The Raspberry Pi 3: Faster, Better, with Wi-Fi and Bluetooth The Raspberry Pi 3: Faster, Better, with Wi-Fi and Bluetooth Today, the Raspberry Pi foundation announced the release of the Raspberry Pi 3. It's the most significant update to the popular line of low-cost computers yet. Read More .

Have you tried streaming live on YouTube with your Raspberry Pi? Perhaps you have some other camera-based projects for your Pi you’d like to share? Let us know below.

  1. Techguy
    January 11, 2017 at 3:07 pm

    Has anyone got this to work. I am getting an error "Failed to read RTMP Packet Header"

    • Christian Cawley
      January 11, 2017 at 10:42 pm

      Planning to mount this project again over the weekend, and see what's causing some of these problems.

      • techguy
        January 17, 2017 at 3:51 pm

        Figure anything out on this? Appears to be something with youtube servers.

        • Christian Cawley
          January 18, 2017 at 1:57 pm

          No, still trying. Problem I think is confusing success with stability. We will get this right, however, and make the tutorial more accurate, as soon as it becomes stable.

        • Techguy
          January 19, 2017 at 2:46 am

          There seems to be some issue with Youtube and this. A couple users on a google group forum had posted similar errors streaming to Youtube.

  2. Kyle
    January 10, 2017 at 8:36 pm

    Hi! I've followed your instructions, but can't seem to get streaming. I'm using a USB webcam, and am able to get stills from the camera using fswebcam. I tried substituting fswebcam for raspivid in your code, but am getting the following script:

    avconv version 11.8-6:11.8-1~deb8u1+rpi1, Copyright (c) 2000-2016 the Libav developers
    built on Oct 8 2016 02:37:00 with gcc 4.9.2 (Raspbian 4.9.2-10)
    [s16le @ 0x540cc0] Estimating duration from bitrate, this may be inaccurate
    Guessed Channel Layout for Input Stream #0.0 : stereo
    Input #0, s16le, from '/dev/zero':
    Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0.0: Audio: pcm_s16le, 44100 Hz, 2 channels, s16, 1411 kb/s
    [h264 @ 0x553ba0] missing picture in access unit
    [h264 @ 0x553ba0] no frame!
    [h264 @ 0x54b400] Could not find codec parameters (Video: h264)
    [h264 @ 0x54b400] Estimating duration from bitrate, this may be inaccurate
    pipe:: could not find codec parameters

    I'm a total rookie at linux and coding, so any help would be GREATLY appreciated!

  3. Kyle
    January 10, 2017 at 8:32 pm

    Thanks for this! I'm struggling to get it working with a USB webcam and hoped you might have some insight. I have the camera working for stills via fswebcam. I tried replacing '''raspivid''' with '''fswebcam''' in the command and get the following errors:
    [h264 @ 0x553ba0] missing picture in access unit
    [h264 @ 0x553ba0] no frame!
    [h264 @ 0x54b400] Could not find codec parameters (Video: h264)
    [h264 @ 0x54b400] Estimating duration from bitrate, this may be inaccurate
    pipe:: could not find codec parameters

    any help would be greatly appreciated!

  4. Matt
    January 6, 2017 at 1:59 pm

    I plan to follow this guide pretty much later today, but I did have a question I wanted to see about: are there any good solutions for audio? To my knowledge the Pi and the Camera module both lack a built in Mic. Is it possible to just plug in a USB Mic and have it 'just work' or is there further configuration?

    • Christian Cawley
      January 11, 2017 at 10:40 pm

      The audio side of this isn't supported by a device, AFAIK, which is why we add a silent audio track.

  5. Josh
    December 31, 2016 at 11:14 pm

    In your example you reference raspistill to test your R_Pi camera module OR webcam and then in the stream you use raspivid to send the data to Youtube. You cannot use raspistill or raspivid with a webcam, so you might want to remove that reference from your article. (Well, cannot might be too strong of a word.) Fswebcam will work on taking the still, but I haven't tested (nor do I know) if that will send a stream.

    • Christian Cawley
      January 11, 2017 at 10:41 pm

      Thanks Josh, will look into this.

  6. Ernie
    December 29, 2016 at 6:05 pm

    I followed the detailed tutorial. From the PI side of things it seems that I am transferring Video ok. 31fps @ 6000Kbps, on the you tube side it says stream starting like in the screenshot above, but I never see the live feed. Eventually on the you tube side it says not enough data received. After stopping the encoder on the raspberry pi, a video of the stream is added in you tube. In these videos I can see a few seconds of the feed from the pi, and that is it. Any thoughts?

    • Christian Cawley
      December 29, 2016 at 8:11 pm

      31fps/6000k seems a little to high to me, it would be wise to reduce both of these -- certainly worth experimenting with different rates.

    • ossama
      January 11, 2017 at 1:51 am

      I had the same issue when using avconv, however when using ffmpeg instead, the video started working. you will notice audio delay above 5seconds. i solved this by using -itsoffset 6

  7. Jeff Burr
    December 28, 2016 at 4:38 pm

    This is awesome stuff. I'd really like to make one of these, but I've never done anything with Raspberry Pi before - should be fun!

    How are you powering this setup? Will the USB bring enough power for everything if I just use a USB "wall charger"?

  8. Dale
    December 23, 2016 at 9:47 pm

    Thanks for sharing this! Do you know if it is possible to stream more than one video at a time?

    • That guy
      December 28, 2016 at 2:27 pm

      Probably not on the same Pi. 2 different Pi's would work.

Leave a Reply

Your email address will not be published. Required fields are marked *