Flex Intelligent Video Buffering Component – Part 1

Posted on July 31, 2007 by Tony Fendall.
Categories: Adobe, Components, Flex, Technology.

Recently at work I have been working on a project which involves streaming large amounts of video using progressive download.  If you use Flash Media Server (has a new name now doesn’t it?), then it will automatically figure out how large the video buffer should be for you, but I wanted to implement the same functionality for progressive downloads within a flex component.

To determine the correct buffer size, you need three bits of information:

  • The available bandwidth that the user has
  • The size of the video to be played, and
  • The bitrate of the video to be played

The user’s bandwidth can be detected fairly easily. In the example application below, the code simply times how long it takes to download the image show at the top right of this post, which is a 150 KB file.  This gives a crude estimation of the user’s connection speed which is good enough for my purposes.

 Click on image to view the applicaiton, right click on the application to view source.

The size of the file can be detected easily by getting a reference to the loader object which is retrieving the file. The bytesTotal property gives the total size of the file in bytes.

That just leaves the bitrate of the video file. This information is contained within the metadata on the FLV file, and is used by flash to display the video correctly. I have looked long and hard though the Flex documentation and source code, and I cannot find a way to get this information at all. It did occur to me that maybe I could divide the number of bytes in the file by the length of the video in seconds, and this might give a ‘good enough’ answer. It does still feel like a bit of a hack to me though.

I’m hoping that someone who reads this will be able to give me some adive for my problem. Does anyone know how to get the bitrate of a FLV file programatically? Or is dividing the file size by the length of the video the way to go? Any advice would be much appreciated.

If someone can help me out, I’ll hopefully post my finished buffering video control at the end of the week.

Mayur
Comment on July 31st, 2007.

You can look into it

http://www.betriebsraum.de/blog/2007/02/27/flash-video-tip-1-calculating-an-optimal-buffer-size/

Comment on July 31st, 2007.

Hi, thanks for the response :)

I found that article through my own searching, and most of my work on this project is based on the information there.

The problem is that he states that the bitrate “can be found in the flv’s metadata property which is set when you encode your video”. I am hoping to get the bitrate from the file itself.

In flash you have access to an ‘onFlvMetaData’ event which allows you to get the metadata from the file. I can’t find the quivalent of this in flex however.

Comment on September 20th, 2007.

Update:
You can find the finished version of this component at http://www.munkiihouse.com/?p=41

Pingback on March 3rd, 2009.

[...] my BufferingVideoDisplay component. I mentioned that I was playing around with this a while ago in a previous post but got stuck trying to get the bitrate of the loading FLV file. It turns out that there [...]