Live Streaming Video using AVConv and the Raspberry Pi

The Raspberry Pi is a credit-card sized $35 computer that can be used to accomplish many tasks similar to what a desktop PC can do. This includes spreadsheets, word-processing, games, and even…live video broadcasting! By connecting a powered USB Hub and webcam to your Raspberry Pi, you can broadcast live video to an unlimited audience for free. I’ll show you how this is possible.

Please be patient and read through the entire tutorial before attempting.

What You NeedCredit: Wikipedia.org

Getting started requires a few peripherals. To start up your Raspberry Pi and do initial configuration, you will need an HDMI cable, USB keyboard, and USB mouse. In addition:

Recommendations

  • Use the raspberry pi “headless”. Do not run the desktop version of Rasbian on your Pi (xwindows). It uses an excess amount of RAM and precious CPU power that you need to reserve for your live video compression and streaming. Instead, use the command line interface or access via Secure Shell from another computer. (optional)

  • Assign a static local IP to the Raspberry Pi. (Configuration can be found in: /etc/network/interfaces). This is useful when accessing the Pi remotely. (optional)

  • Don’t use a Raspberry Pi enclosure that limits heat transfer and airflow.

Preparation

Update your software repository to the latest version by running the following commands:

sudo apt-get update
sudo apt-get upgrade

Plug in your devices.  Webcam –> Powered USB Hub –> Raspberry Pi

Install Screen (optional):

apt-get install screen

Restart your Pi device:

reboot


Installation

To transcode and broadcast the video, you will need to use a Linux application called AVConv (similar to FFmpeg). It is a command line program for transcoding multimedia files using the Libav Multimedia Framework. FFmpeg will give you the same result, but I personally prefer AVConv for ease-of-use.

To Install AVConv, run the following command:

sudo apt-get install avconv

Find a Broadcasting Platform:

To broadcast live streaming video to the Internet, you will need to create an account with Ustream, Justin.tv, or similar. After creating an account, it’s important you find the “Stream Key” or “Stream URL”.

Find the Stream URL and Key in Ustream:

  • Login to Ustream.tv

  • Open your “Dashboard”

  • Click “Remote”

  • Copy the “RTMP URL”. You will need this once you begin broadcasting, in the next step of this tutorial.

Here is an example of the final URL:

rtmp://2.773.fme.ustream.tv/ustreamVideo/77775432

Find the Stream URL and Key in Justin.TV:

  • Login to Justin.tv

  • Go to the “Other” Broadcasting page: http://www.justin.tv/broadcast/adv_other

  • Press “Show” beside “Stream Key”

  • Save this Key! You will need it in the next step.

  • Paste your “Stream Key” in immediately after this URL:  rtmp://live.justin.tv/app/

    • Note: The Default RTMP server is hosted in San Francisco. On the Justin.TV forum, you can find the RTMP address URLs for a server that is closer to you. (optional)

Here is an example of the final URL using the default URL:

rtmp://live.justin.tv/app/live_3987439857_QQsAmPlEVeStREamkeYgN6a93

Save these URLs for the next step!

Broadcast Configuration

Once you have everything set up, command-line access to the Raspberry Pi, and your Justin.tv/Ustream Stream Key URL, you’re ready to initiate the broadcast! This is the fun part!

From this point forward, you only have to input TWO commands.

  • Open a new screen session:

    screen

  • Enter the AVConv command with ALL of the parameters attached.

Basic command:

Enter this single command inside the screen session to start broadcasting! Yes, it’s that easy!

avconv -f video4linux2 -s 640×360 -r 10 -b 350k -i /dev/video0 -f flv rtmp://live.justin.tv/app/live_ReplaceThisWithYourURL

In the above example,

  • -s defines the broadcasting resolution. (Widescreen = 640×360)

  • -r defines the maximum frame-rate

  • -b defines the maximum bitrate (k= Kbps)

  • -i defines the location of your USB webcam. In most cases, if you only have one USB video device plugged in, use the /dev/video0 phrase.

  • -f defines the type of video stream and location. In this example it’s FLV transcoding (Flash video) to an RTMP server at Justin.tv

  • Don’t forget to replace the rtmp:// URL with your own Stream Key URL from the “Find a Broadcasting Platform” step.

 

An advanced example: (Warning: for experienced users only)

avconv -f video4linux2 -s 640×360 -r 10 -b 350k -i /dev/video0 -vf drawtext=”fontfile=/usr/share/fonts/truetype/freefont/FreeSansBold.ttf:text=’some text here ‘:fontsize=16:fontcolor=blue:x=2:y=360″ -acodec copy -metadata title=”24×7 Pi Cam” -f flv rtmp://live.justin.tv/app/live_81777DontStealMyKey6777

The above example is what I use on my “Eric’s Pets” live stream. You may notice a big difference from the original AVConv command. The -vf drawtext command allows you to define a font (download to your /freefont/ folder). Once a font is defined, you can add formatted text such as a “watermark” in the bottom left. The “-metadata title” allows me to tell Justin.TV what the broadcast page title should be. It can even get more advanced than this!

Please be very patient with AVConv and Raspbian. It takes time to perfect the stream quality. (My settings may not work for your camera or setup).

AVConv is very similar to FFmpeg in this sense. You can add many different features to your live video broadcast by simply putting new parameters in your initial command. You can learn more about these by reading AVConv/FFmpeg documentation.

AVConv will provide information while streaming, similar to the following string. If you see this, the stream should be broadcasting correctly.

Eric's Live Bunny Cam via Raspberry Pi

Eric’s Live Bunny Cam via Raspberry Pi

   Stream #0:0 -> #0:0 (rawvideo -> flv)

frame=215  fps=8   q=15.5   size=1001kB   time=29.20  bitrate=281.0kbits/s  dup=0   drop=3   

At any time, you can stop the stream by typing Ctrl-C, and exit the screen session by typing “exit”. To close the screen session but leave AVConv running, type Ctrl-A-D. To enter a screen session that’s already running, type “screen -r”.

Still hungry? Learn more at http://libav.org/avconv.html

Let me know what the next blog post should include!



If you liked this post, say thanks by sharing it:
  • Jackson Christopher

    Awesome work! Now I have a use for my Pi! It’s just sitting in the closet gathering dust, but I have an unused webcam, so this will happen!

  • Jorge Sanchez

    Thanks for the info. Really great!
    Do you think it would be possible also with the cubieboard?

    • techzany

      Thanks for the feedback. If it runs Debian, this tutorial should work for it. But I can’t guarantee.

      • Jorge Sanchez

        Confirmed. It works even better. I have tested both devices and with the Cubieboard 2, performance is much better. Thanks!

  • WacoJohn

    This is just what I have been looking for … AFTER following another
    tutorial which did not work out .. it was/is outdated and was for
    Bambuser. Here is my problem … that tutorial said to do a GIT for
    FFmpeg which I did. I now have some kind of ‘hybrid'(?) FFmpeg. I
    really don’t want to install any more software … so this would be
    perfect IF it explained for FFmpeg rather than avconv. Any chance of
    that happening …. PLEASE?? I am a rookie w/ Linux, Raspberry, etc.

    • TechZany

      In my experience, AVConV works much better. FFMPeg uses the same commands though so you’re free to install and use it instead. What’s wrong with using avconv though?

      Good luck!

      • WacoJohn

        The only thing ‘wrong’ is my inexperience. I know from the other tutorial I have FFmpeg and I TRY to not install more than I actually need (storage space ‘phobia’). Thought it prudent to use what I have. I have no idea how much space AVConV takes up.

        Great tutorial. Would like to try it w/ FFmpeg … I have minimal needs for streaming. Not sure how … to use your tutorial but with FFmpeg/ustream. I assume by substituting avconv code with FFMpeg(?), but knowing my concerns, happy to take your advice. Let me know what you think (maybe you already have), and I will go with whatever you say. Thank you for your reply.

        • techzany

          If you’re not sure how all of this works and don’t have good linux skills, scrap FFmpeg and use AVConv. Don’t worry about the space it takes up — just follow the tutorial :)

          • WacoJohn

            sudo apt-get install avconv …. pkg not found.

          • techzany

            You need to follow the sudo update/upgrade steps. Please read the entire tutorial and follow every step.

          • WacoJohn

            Oh, .. I am 99% certain I did. I always update/upgrade .. first thing before anything else.

          • Micky Del Favero

            # apt-get install libav-tools
            The libav-tools package contain avconv.

  • WacoJohn

    “avconv -f video4linux2 -s 640×360 -r 10 -b 350k -i /dev/video0 -f flv rtmp://live.justin.tv/app/live_ReplaceThisWithYourURL”

    for USB cam. How so for raspicam? Don’t see raspicam in /dev.

    • techzany

      If your camera isn’t showing up in /dev/, you can change the location to match.

      Sorry, This tutorial is for USB webcams (such as the Logitech HD type). Good luck!!

      -Eric

      • WacoJohn

        Thank you, Mr. Eric. Sorry to be a pain. I have tried so many tutorials .. and failed. I did not know raspicam is not in /dev. I really don’t know where it is, but have asked in the raspicam forum. Hopefully I just need to point the command to it. I will also employ AVConV as advised. Thank you for your courtesy and patience.

  • Kanli1623

    Thank you for sharing. Your post worth of reading. Waiting for new posts. buyincoins

  • Exacom

    Any option to turn off the sound and mute the stream instead (or put music)?

    • http://techzany.com/ TechZany

      There won’t be any sound using this tutorial.
      In the future, I hope to write a tutorial explaining how to add sound. If you check the LibAV docs, you can see the symbol that defines audio – I think you can easily loop a file, and hopefully play a stream. Good luck!

  • Rahij Ramsharan

    Where do I see the live video?

    • http://techzany.com/ TechZany

      Depending on where you’re streaming it to, you have to go to the corresponding website.

      If using Justin.tv, go to http://justin.tv/YourUsernameHere

  • Luca

    Nice!! But what about if I have a video and I want to share it in lan (broadcast)?
    I use:
    avconv -i video.mov -c:v libx264 -f mpegts udp://DEST_IP:1234

    but if I want to specify a range of IP??

    • http://techzany.com/ TechZany

      Sorry, I’m not sure, you would need some sort of RTMP server setup on your local server.

      You can read more in the LibAV docs: http://libav.org/avconv.html

  • koreniac

    Hi TechZany

    I follow you step but got this error from justin tv

    RTMP_Connect0, failed to connect socket. 110 (Connection timed out)

    rtmp://live.justin.tv/app/live_64925189_WgGLwhXKscuCNPQxesxk2wRUNx2G8l: Operation not permitted

  • koreniac

    now I can fixed below error with this server rtmp://live-sin-backup.justin.tv/app/

    but I got new issue

    Attention! We are making changes to our video infrastructure. Your broadcast settings will be incompatible with this new system.

    Reason 1: Video codec must be set to h.264 (it is currently “CodecId_SorensonH263″). Click here to fix the problem.

    Reason 2: Audio codec must be set to MP3 or AAC (it is currently “unknown”). Click here to fix the problem.

    how can i solve this problem?

    thanks.

  • Manish

    Hey techzany
    please help me im facing this problem
    [video4linux2 @ 0x17c9740] ioctl(VIDIOC_STREAMON): Broken pipe

    /dev/video0: Broken pipe