How To Live Stream A USB Webcam On a Raspberry PI 2

The EEVblog Lab had a HD Dropcam so that people could watch what was happening live in the lab. It worked well enough, but then Dropcam changed their terms and conditions and wanted to merge with Nest (who bought them). This was bothering Dave so the search for an alternative began.

Surprising, there are very few options on the market that:
a) Allow low frame rates in order to reduce upload bandwidth (The lab ISP has a monthly bandwidth cap)
b) Are low enough power and work completely autonomously
c) Don’t cost an arm and a leg
d) Provide the ability to embed the stream into a public website so people could watch without having to go another site.

We had spare Logitech webcams (UVC ones should work fine, H264) which were  that would provide good image performance, so it was simply a matter of finding a hardware and software solution to match. A Logitech C905 was used in the final solution even though we had better C920’s, they will be saved for use as the live show cameras.

We have several Raspberry Pi 2 sitting around and their power usage won’t substantially increase the power bill, so it seemed natural to use one of them.
A CDN(Content Delivery Network) would allow bandwidth usage to be constant and predictable, allowing us to ensure we stay under the monthly bandwidth cap. They take one stream from the client and then provide the bandwidth and the means to serve as many people who want to view it. Youtube, Ustream, Vimeo et.al are all examples of CDN’s. A million people can watch the live stream at once and the bandwidth used from the lab is that of only one stream.


To create an economic live streaming device we require power efficiency, low frame rate and high resolution. While there are options out there such as Dropcam we wanted towards something that wasn’t controlled by an organisation with constantly updating terms. After trying out FFMPEG, OBS, and avconv (pretty much FFMPEG) I found the simplest option was to use AVCONV as it didn’t require me to compile the entire thing.

The following will provide the fundamental steps required to setup live streaming via RTMP to a service such as Twitch, ConnectCast, YouTube etc…
There are a few things you’ll need to setup the Raspberry Pi 2:

  1. Raspberry Pi 2
  2. Micro SD Card, at least 8Gb
  3. Keyboard
  4. HDMI Cable and Monitor
  5. Internet Connection via RJ45 Connector
  6. MicroSD Reader / Writer
  7. A UVC USB Webcam (we used a Logitech C905)

The first thing to do is get some prerequisite software installed on your PC you’ll need the following:

  1. SD Formatter

https://www.sdcard.org/downloads/formatter_4/

  1. Download the NOOBS Image, the Lite Image is fine

http://www.raspberrypi.org/downloads/

  1. Putty, for SSH

http://www.putty.org/

Once the above steps are complete:

  1. Insert the SD Cart into its reader, then open the SDFormatter software:
    SDFormatter
  2. Ensure that the correct drive is selected then click format.
  3. Extract the Noobs Lite archive downloaded earlier to its own folder:
    Noobs Lite Archive
  4. Open the folder that the archive was extracted into you should see the following files:
    Noobs Lite Extracted Folder
  5. Copy the contents of the above folder into the newly formatted SD card.
  6. Safely Eject the SD Card once the files are copied.
  7. Insert the SD card into the Raspberry PI 2.
  8. Raspberry Pi 2 has a keyboard, monitor and internet connection then plug in its power.
  9. The following screen should appear, the top item is what we are going to install, and it’s likely you won’t see all of the options below, this isn’t a problem as long as you see Raspbian.
    Noobs Lite Raspbian Selection
  10. Select Raspbian from the list enter, then press (i) to begin the install.
  11. At this time it’s a good idea to get a coffee because this is the most boring section of the install, it’ll take about half an hour.
    Install Lack of Progress Bar
    …WAITING
    …STILL WAITING
    …FOREVER WAITING.
    …30 minutes later still waiting…
  12. After the installation it’s time for some configuration. In this screen we will enable SSH, and disable GUI if it’s enabled, there is no need for a graphical interface for this project.
  13. Press the right arrow once in the main menu and select finish, press enter and reboot.
  14. The system will now boot to terminal, login with the user name pi, password raspberry (you should change this after completing tutorial)
  15. Type the following commands, you don’t have to do this all as root but it means less typing:

    sudo -i
    apt-get update
    apt-get upgrade
    apt-get install -f

  16. Installing AVConv is relatively simple:

    apt-get install libav-tools

  17. Time for some testing, I’ve used connectcast and ustream but you can as easily use twitch or any other rtmp service:

    Avconv -f video4linux2 –s 640×360 r 10 –b 350k -i /dev/video0 –f flv rtmp://stream.connectcast.tv/live/streamkey

    Or for ustream

    Avconv -f video4linux2 –s 640×360 r 10 –b 350k -i /dev/video0 –f flv rtmp://x.xxxxxxxx.fme.ustream.tv/ustreamVideo/xxxxxxxx/streamkey

    The URL for your streaming service can usually be found on the streaming services website and it is important to keep these secret.

  18. If the stream starts you’re going well, otherwise be sure that the steam key is correct and the other parameters are in the same order as above.
    Stream
  19. Press CTRL+C to stop the stream.

SSH Setup

  1. Type the following to get the local IP address of the Raspberry PI:

    ifconfig

  2. On the second line of the resulting text note the address after “inetaddr:” mine is 192.168.1.13:
    IP Address Arrow
  3. Open Putty and copy the IP address from before into the host name section:
    Putty Setup
  4. Click Open
  5. Login with the same username and password as before, you will have to accept a public key thing before you do this.
  6. SSH Setup then become root.

    sudo -i

Setup the Boot Script

  1. Enter the init.d directory, you can copy these commands from this page into putty with right click:

    cd /etc/init.d/

  2. Create a file in this directory by typing the following command:

    sudo nano STREAM.sh

  3. Copy the following into the file, make sure you insert your stream key, the comments must not be left out, debian requires them:
    For connectcast:

    ### BEGIN INIT INFO
    #Provides:Stream startup
    #Should-Start:console-screen debus network-manager
    #Required-Start:$all
    #Required-Stop:$remote_fs
    #Default-Start:2 3 4 5
    #Default-Stop: 0 1 6
    #Short-Description: Starts avconv and the webcam live stream
    ### END INIT INFO
    set -e
    case “$1″ in
    start)
    RESOLUTION=”hd720″
    BITRATE=”200k”
    FPS=”2″ #fps
    STREAM_KEY=”STREAMKEY”
    SERVER_URL=”rtmp://stream.connectcast.tv/live/”
    echo “Starting avconv stream”
    echo $RESOLUTION ” ” $FPS ” ” $BITRATE ” ” $SERVER_URL ” ” $STREAM_KEY
    sudo avconv -f video4linux2 -s “$RESOLUTION” -r “$FPS” -b $BITRATE -i /dev/video0 -f flv $SERVER_URL$STREAM_KEY
    ;;
    stop)
    echo “STOPPING STERAM”
    skill $PROGRAMNAME
    exit 1
    ;;
    esac
    exit 0

    Or for USTREAM:

    ### BEGIN INIT INFO
    #Provides:Stream startup
    #Should-Start:console-screen debus network-manager
    #Required-Start:$all
    #Required-Stop:$remote_fs
    #Default-Start:2 3 4 5
    #Default-Stop: 0 1 6
    #Short-Description: Starts avconv and the webcam live stream
    ### END INIT INFO
    set -e
    case “$1″ in
    start)
    RESOLUTION=”hd720″
    BITRATE=”200k”
    FPS=”2″ #fps
    STREAM_KEY=”STREAMKEY”
    SERVER_URL=”rtmp://x.xxxxxxxx.fme.ustream.tv/ustreamVideo/xxxxxxxx/”
    echo “Starting avconv stream”
    echo $RESOLUTION ” ” $FPS ” ” $BITRATE ” ” $SERVER_URL ” ” $STREAM_KEY
    sudo avconv -f video4linux2 -s “$RESOLUTION” -r “$FPS” -b $BITRATE -i /dev/video0 -f flv $SERVER_URL$STREAM_KEY
    ;;
    stop)
    echo “STOPPING STERAM”
    skill $PROGRAMNAME
    exit 1
    ;;
    esac
    exit 0

  4. Make the script executable with the following commands:

    chmod 755 /etc/init.d/STREAM.sh
    chown root:root /etc/init.d/STREAM.sh

  5. Register it to be run at start-up:

    update-rc.d STREAM.sh defaults

  6. Reboot and your device should start on boot!

    reboot

Me
A connectcast stream from a device setup as per the instructions above. 🙂
Live streaming from ustream.
Live streaming from ustream.

 

About Seppy

Check Also

EEVblog #943 – How EEVblog Does Video Editing

Dave takes you step-by-step through his video editing, rendering, and transcoding process for an EEVblog ...

The EEVblog Store generally ships twice a week, on Tuesdays & Fridays, Sydney time. Dismiss