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).

muo-diy-picamera-device

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://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:

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.

Leave a Reply

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

  1. Wiebe
    June 6, 2017 at 6:12 pm

    I have tryed on my Raspberry Pi 3, and i am using a Logitech C920 webcam, with Motion it works.. and i am now trying it with this, and i am have a error:

    mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:EN OMEM)
    mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
    mmal: Failed to create camera component
    mmal: main: Failed to create camera component
    mmal: Camera is not detected. Please check carefully the camera module is instal led correctly

    avconv version 11.9-6:11.9-1~deb8u1+rpi1, Copyright (c) 2000-2017 the Libav deve lopers
    built on Apr 26 2017 06:57:28 with gcc 4.9.2 (Raspbian 4.9.2-10)
    [s16le @ 0x1188cc0] 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 @ 0x1193400] Could not find codec parameters (Video: h264)
    [h264 @ 0x1193400] Estimating duration from bitrate, this may be inaccurate
    pipe:: could not find codec parameters

  2. Matt
    April 18, 2017 at 2:26 pm

    I get the same issue as all the other commenters. It just sits there on "Starting".

  3. John
    April 6, 2017 at 9:34 am

    Hye, i seem to be having a small problem, in youtube i get stuck on the starting part, it doesn't stream. Got any idea what the problem could be?

    • Matt
      April 18, 2017 at 2:27 pm

      I'm wondering if these instructions are now out of date.

  4. Andy
    March 20, 2017 at 12:18 am

    I'm using an RPi3, and I'm unable to start streaming. When I run the code, the image appears on the screen, rather than the code that you have shown in your screen shot. Suggestions

    • Emilio
      March 20, 2017 at 11:53 pm

      If you want to see the terminal, add the -n tag for "no output" for raspivid.

  5. Noam
    February 27, 2017 at 12:14 am

    Christian,
    Do you have an extra 0 in the -b variable?
    Seems like you have 6Mil instead of 6k

  6. kedar
    February 24, 2017 at 5:44 am

    Can we make that live streaming from youtube for private, I have to use it for office purpose. Is it possible?

    • Emilio
      March 21, 2017 at 12:12 am

      On your YouTube Live dashboard, there is an option for privacy where you can select "Private".

  7. Scott
    February 19, 2017 at 2:37 pm

    Followed your tutorial and it still doesn't work.

    I can take an image with raspi-still and it works fine

    I run your command and it shows green on Youtube but it always says I'm offline.

    • Christian Cawley
      February 21, 2017 at 7:45 am

      Recent testing unreliable, however, and best plan would seem to be to manually compile ffmpeg, or wait for it to be reintroduced to Raspbian.

      We'll be updating the article in due course to reflect this.

  8. dTA
    February 15, 2017 at 2:08 pm

    I was a bit confused as to what this thing is about ffmpeg being a precursor to avconv is. I looked it up and it seems avconv is a fork and both are maintained with debian in fact having switched back to ffmpeg apparently. In my opinion as such this article is a bit misleading in that regard.

    • Christian Cawley
      February 21, 2017 at 7:42 am

      At time of writing, avconv was being used in Raspbian Pixel. So hardly misleading!

  9. suhail
    January 31, 2017 at 4:46 pm

    hi sir, I have one problem with it....
    whenever I stream the video I receive only a blank screen but buffering in terminal window. Can you help to resolve this.

  10. bubiman
    January 25, 2017 at 4:38 pm

    Hi, I got it kind of working. The Pie is streaming to youtube and youtube in receiving data. Youtube even claims the stream quality as good (green) but youtube will simply not start the stream. After 1-2min the stream quality turns to red but still not video visible on youtube.

    Is there a way to hide the video output on the raspi so I can still see the console?

    Thanks for your help and time
    bubiman

    • bubiman
      January 26, 2017 at 1:26 pm

      I tried to stream to twitch because they got a nice Stream Inspector.
      It recognize the video resolution but neither video codec, audio codec, fps nor bitrate.
      Somthing wrong with muxing video and audio? Missing some headers?

  11. Bill Bradstreet
    January 21, 2017 at 8:39 pm

    Thanks for putting this together. I'm weighing in solely so you have more information. I trust you are trying to figure this all out yourself?

    So, I have tested things and I get as far as YouTube telling me that I have started a livestream, but I never get an image. I see my pi is sending up to YouTube at 7Mbps, which is above the -b 6000000 parameter setting, but still YouTube won't show an image. Eventually, it tells me my upload speed isn't fast enough and that users may experience buffering. I never get more than a black screen on the livestream (I see an image on the display connected to the pi).

    I have tried to set -w & -h to 800x600 and -b to something much lower, but with any different settings, I do not get any response from YouTube's livestream page.

    This is my first pi project. I haven't used unix-ish in a long time!! I appreciate you sharing all the commands I've forgotten!

    • Christian Cawley
      January 22, 2017 at 8:28 am

      Thanks Bill. Some more spare time coming this week, so hoping to find some answers...

  12. 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.
      (Hopefully!)

      • 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.

    • kim kloster
      January 23, 2017 at 6:20 pm

      I had my 'secret' in brackets, when I removed them the error vanished. But that was probably just me.

  13. 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!

  14. 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!

  15. 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.

  16. 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.

  17. 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

  18. 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"?

    • PacoOtaktay
      May 28, 2017 at 4:25 am

      You should be using a 5V 2A at least for the Raspberry Pi 2 and a 5V 2.5A for the Raspberry Pi 3. Most cheap cell phone chargers are not enough for stable operation of the Raspberry Pi's.

  19. 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.