Broccoli Products home | contact us | about us
(by Lou Burne, last updated 10-APRIL-2012)
Live TV
Project Summary
This project demonstrates how to download audio and video from a live RTMP stream.

Live streams are freely available over the Internet, and include 24 hour news and televsion, and speciality broadcasts from organisations such as NASA.

This example grabs 60 seconds of BBC World News and saves it to an FLV file.  Some alternate live feeds are listed in the Feed Directory.
The Code
The code consists of a single C# file, LiveTv.cs, which contains a single function, Main.  This function is described below.

To build the project, use the most recent build of the RtmpClient, which can be found in the file archive.

Main Function (Main)
The main application function in LiveTv.cs works as follows:

A Create an FlvStuffer object.  The FlvStuffer is a class for saving RTMP audio, video and meta-data messages into an FLV file.
B Create an RtmpClient object. The RtmpClient constructor takes one parameters, a reference to the main application window. Since LiveTv.cs runs in a console, which is windowless, this parameter can be null.

EventsEnabled - Set EventsEnabled to false so the RtmpClient doesn't waste time composing events when no one is listening.
C Create a new RTMP connection. The RtmpConnection object returned encapsulates a connection to the remote RTMP server.

The behavior flags instruct the RtmpClient library to automatically respond to any request for pings or data acknowledgements that come from the server.
D Live feeds can take a few seconds to get going so we set the response timeout to 20 seconds.
E Set up a callback function to automaytically mount any streams that the server opens.

It is common for RTMP servers to automaytically open stream id=0, "stream0", when a connection is made.  In the callback, we use an C# anonymous function to set the buffer size.

For the majority of RTMP servers this code will still work if this section is commented out.
F Connect to the RTMP application.

The RTMP application we are connecting to uses standard connection parameters.
G Call an RtmpConnection function to create a stream. This returns an RtmpStream object.

An RTMP connection can theoretically support multiple streams, but in reality few RTMP servers are sophisticated enough to do this well.
G Create a WorkingSet object.  WorkingSet is a simple class that wraps up some objects so that they can be passed to a callback function as a single parameter.
H Register a callback function for the new stream.

Instead of a separate callback function defined in our code file, we are using a C# anonymous function. 

For each RTMP message received on our stream, we display the details on the console, and then pass the message to the FlvStuffer to update the FLV file.

This callback could be made more efficient by limiting the callbacks to only those types of messages we need to put into the FLV file, i.e., audio, video and meta-data.
I Play the stream.

We need to use a special start position value (SpecialStartValue_PlayLiveStream) to indicate to the RTMP Server that this is a live stream.
J Loop until the streaming stops, or the user presses the "C" key.
K Clean up the RTMP session. Delete the stream, close the connection.

The RtmpClient object will be disposed of when it falls out of scope.
L Closing the FlvStuffer will set the correct duration for the FLV file, and flush and close the FLV file.
What To Do With An Flv File
Flv files are usually played in an Adobe Flash player embedded in a web page.

VLC Media Player
To play an Flv without using a browser try VideoLan's VLC Media Player, which is open-source, and plays a huge range of formats, both audio and video.

Flv is a skeleton file format, and finding a single free application to convert an Flv file to another format is not easy. 

There are two freely available applications, Mplayer and Lame.

Mplayer will convert an FLV file into a WAV audio file or AVI video file.

Lame is a freely available open-source application for converting WAV files into MP3.

Mplayer and Lame are both console applications, so can be run from a batch file or by starting a process in code.

The batch file for converting an FLV called hello.flv into an MP3 will look like this:

Mplayer hello.flv -ao pcm:file=output1.wav

Lame --priority 0 -b 128 -h --resample 44.1 output1.wav output1.mp3
Contact form 
Use the contact form to send comments and requests for information to Broccoli Products.
Topic:
Message:
Email:
Broccoli Products Ltd © 1998-2012 Broccoli Products Ltd
Reg Number: 2895355
Reg Office: 27 Old Gloucester Street, London. WC1N 3AX
Privacy Policy
Copyright Notice
Liability Disclaimer
Contact Us