Building a Youtube Live Streaming Camera With A Raspberry Pi

pi_situ

For the ManCadet, we built a couple of cheap streaming cameras to stream content to Youtube Live. You can see the quality of the output here.

To do this, we used Raspberry Pis with the camera module and “official” case. We stuck them to the wall with gaffer tape and positioned the camera module with blue-tak. For power, we used a 5m USB female to male connector, a standard 2amp usb charger and a 20cm micro usb cable. This gave us some flexibility in positioning the camera a significant distance from power sockets.

For software, we tried a couple of approaches. Raspian includes a client for the camera, raspivid which can output the camera output to standard output. A video4linux driver also exists for the camera, and whilst we could get this to work to host a local rtmp stream, we couldn’t get this to work to stream directly to Youtube. We couldn’t get avconv to work either, Youtube tells us there’s a feed, but there’s no output. Poo.

On a suggestion, we tried a third party cross-compiled ffmpeg library, encoding the output from raspivid. There’s a 15-30 second delay in the content being processed at Youtube’s end, but it works. To make it work yourself, you’ll need to install ffmpeg from here

You’ll then need the details of where you’re uploading to. Head to the Youtube Live dashboard. You’ll need the Server URL and the Stream Name/Key fields:

Once you’ve done all that, execute the following command on your Pi (ideally in a tmux or screen session so you can leave it running):

raspivid -o - -t 0 -vf -hf -fps 30 -b 6000000 | ffmpeg -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 [SERVER URL]/[STREAM NAME]

These happily streamed six hours at a time with no complaints. They’re pretty robust as well, one of them was knocked off the wall a few times. Tape it back up, restart the stream, and presto it carried on working.

Cost per camera:
1 x Raspberry Pi 2B – £28.95
1 x Raspberry Pi Camera Module – £18.58
1 x Pi Hut USB Wifi Adapter – £7.34
1 x Raspberry Pi Case – £6.38
1 x Raspberry Pi Camera Module Add On Cover – £6.45
1 x Lindy 5m USB 2.0 Extension Cable – £4.99
1 x Micro Usb cable (already owned)
1 x 2A Usb Charger (already owned)

Total cost per camera: £72.69

  • Pingback: Running a Modern, Connected, Fencing Competition()

  • Suraj Joneja

    Hi Kian,

    I needed sound with the live youtube video, I cross compiled another version of ffmpeg which supports alsa input and gave the command a little modified to read from my usb soundcard on the pi, but I get alsa buffer xrun, it seems nobody on the net has been able to do it as yet, as I saw another user facing the same problems on the raspberry pi forums. do you have any solution??

    Thansk and regards, Suraj

  • kianryan

    I have not attempted to mix sound in as of yet, so I don’t have a solution.

  • Popescu Ana Maria

    Hello! I need a bit of help on this if you don’t mind having a read… I have been struggling a lot with this line of code. It makes no difference if I use ffmpeg or avconv – I get the same error: “Option re (read input at native frame rate) cannot be applied to output file acodec — you are trying to apply an input option to an output file or vice versa. Move this option before the file it belongs to. Error parsing options for output file codec. Error opening output files: Invalid argument” If I try moving the input option before the file it belongs to, I get another error: “[NULL 0xb60170] Unable to find a suitable output format for ‘acodec'”…so this does not work for me. It is frustrating seeing how many people can use this. Can you help me get it working? Thank you in advance for any suggestions you may have.