Random Raspberry Pi - Tutorials and Fix's and How to's from Random_robbie the raspberry pi Geek
Monday, 20 January 2014
Stream to Justin TV from your pi!
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 Need
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:
A Raspberry Pi Model B running Debian or “Rasbian”
A powered USB hub
A Debian-supported webcam (Logitech HD C series) – I use the Logitech HD V-U0016 HD C510
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.
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:
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”
Copy the “RTMP URL”. You will need this once you begin broadcasting, in the next step of this tutorial.
-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 drawtextcommand 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.
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”.