Angel’s Blog

Someone right now is looking pretty tired, staring at a laptop trying to get inspired…

FLV steaming – server/client side


This is the shortest how-to ever. You won’t get any extra info, the idea is to install the whole scheme fast as possible and then spend some time reading docs for each separate module.
This is how to make HTTP FLV streaming using the JW FLV Media Player and the lighttpd web server.
Here we go:
1. Installation
Install the lighttpd server. I am using Ubuntu, so i will install it via the apt-get package system:

sudo apt-get install lighttpd

2. Configuration

The first we have to do is change the default listening port to 81 (optional if you are already using apache on the same host, or whatever you decide to use) and enable the needed for flv streaming modules (mod_flv_streaming and mod_secdownload ). Edit the following file : /etc/lighttpd/lighttpd.conf and change the server.modules and the server.port parameters:

  1. server.modules = (
  2. "mod_access",
  3. "mod_alias",
  4. "mod_accesslog",
  5. "mod_compress",
  6. #           "mod_rewrite",
  7. #           "mod_redirect",
  8. #           "mod_status",
  9. #           "mod_evhost",
  10. #           "mod_usertrack",
  11. #           "mod_rrdtool",
  12. #           "mod_webdav",
  13. #           "mod_expire",
  14.     "mod_flv_streaming",
  15.     "mod_secdownload", ## optional
  16. #           "mod_evasive"
  17. )
  18. flv-streaming.extensions = ( ".flv")

and

  1. ## bind to port (default: 80)
  2. server.port = 81

Then start the lighttpd server ( Ubuntu )

sudo /etc/init.d/lighttpd start

3.1 Converting regulat video clips into .flv

Before compiling the ffmpeg binary, you have to download and install some development libraries. Please install liblame-dev and libvorbis-dev and all their dependencies:

apt-get install liblame-dev

apt-get install libvorbis-dev

Then go to your favorite source folder and download there the current svn server of ffmpeg

cd /usr/local/src/

If you don’t have subversion installed:

apt-get install subversion

Download the source code:

svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg

Open the downloaded directory:

cd ffmpeg/

and compile the ffmpeg binary with the following keys

./configure –enable-shared –prefix=/usr –enable-gpl –enable-pthreads –enable-libmp3lame –enable-libvorbis

make

Wait a lot of time …

and finally install it :

make install

2. Converting movies
Lets create two separate folders for “ready to be streamed movies” and “unconverted movies”

mkdir /var/www/movies/

mkdir /var/www/movies/convert_me/

mkdir /var/www/movies/flv/

Copy a favorite movie or clip in /var/www/movies/convert_me/ and then execute:

ffmpeg -i /var/www/movies/convert_me/myclip.avi -s 320×240 -ar 44100 -r 12 /var/www/movies/flv/myclip.flv

3.2 FLV files

You have to prepare your flv for streaming. I am not really a video encoding/decoding guru, so i am sharing my solution to the problem, but i am not sure it is the best possible.

Install flvtool2:

sudo apt-get install flvtool2

Put some flv movie somewhere inside your DocumentRoot folder ( for example /var/www/path/clip.flv) and then use the flvtool2 to generate the needed for the server meta-tags inside your flv video.

flvtool2 -U -p /var/www/path/clip.flv

Here i am using two keys :
-U Updates FLV with an onMetaTag event
-p Preserve mode only updates FLVs that have not been processed before
another interesting switch for whole folders will be :
-r Recursion for directory processing

As you may notice, the flvtool2 don’t have so many options at all, so it will be good to make a little more research about the features of this tool.
You can test the generated meta tags using the debug command in flvtool2.

flvtool2 -D clip.flv

Be prepared for a lot of output… It looks like this :

  1. #1 Meta Tag (onMetaData): timestamp 0, size 7910, data size 7899
  2. #2 Audio Tag: timestamp 0, size 274, data size 263
  3. #3 Audio Tag: timestamp 26, size 274, data size 263
  4. #4 Video Tag (Keyframe): timestamp 38, size 39766, data size 39755
  5. #5 Audio Tag: timestamp 52, size 274, data size 263
  6. #6 Video Tag (Interframe): timestamp 71, size 5289, data size 5278
  7. #7 Audio Tag: timestamp 78, size 274, data size 263
  8. #8 Audio Tag: timestamp 104, size 274, data size 263
  9. #9 Video Tag (Interframe): timestamp 105, size 4334, data size 4323
  10. #10 Audio Tag: timestamp 130, size 274, data size 263

Now your clip.flv is ready to be streamed.

4. FLV Client Player

Download the JW FLV Media Player from http://www.jeroenwijering.com/ and unpack the archive somewhere in your www directory. Create a html file in the same directory as your player with the following source:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.  <title>JW Player for Flash</title>
  5. </head>
  6. <body>
  7.  <div id="container"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>
  8.  <script type="text/javascript" src="swfobject.js"></script>
  9.  <script type="text/javascript">
  10.   var s1 = new SWFObject("player.swf","ply","328","200","9","#FFFFFF");
  11.   s1.addParam("allowfullscreen","true");
  12.   s1.addParam("allowscriptaccess","always");
  13.   s1.addParam('wmode','opaque');
  14.   s1.addParam('flashvars','file=http://your_lighttpd_server.com:81/path/clip.flv&streamer=lighttpd');
  15.   s1.write("container");
  16.  </script>
  17. </body>
  18. </html>

Save it as test_stream.html and open it with your web browser ( http://your_lighttpd_server.com:81/path/mediaplayer/test_stream.html). If your paths are correct you will be able to watch your clip.flv with the flv player.
In this example i am using the same server (lighttpd) for hosting the flv player and the clip, but it is possible another webserver to be used. For example you can host your site on Apache and use lighttpd only for storing the streamed videos!

5. Some technical information to help you understand the idea
Let’s take more detailed view of the JW Player’s configuration:

  1. s1.addParam('flashvars','file=http://your_lighttpd_server.com:81/path/clip.flv&streamer=lighttpd');

Whe have two parameters:
file = link to the flv file we want to stream
streamer = lighttpd
With the second parameter we are activating the native flv stream support in the lighttpd server. After you start the playback of the video and forward to a spot that has not been downloaded yes, you will notice that another parameter will be passed to the flv file :
start = # of frame to start (here is where the meta tags we generated with flvtool2 are used to index the frames in the flv file). The lighttpd parse all the requests to the flv file and starts the streaming from the requested frame.

That’s all folks.

For more info, here is where i got the info for this mini-how-to from:

http://www.jeroenwijering.com/?item=JW_FLV_Media_Player
http://www.jeroenwijering.com/?item=HTTP_Video_Streaming
http://www.inlet-media.de/flvtool2
http://blog.lighttpd.net/articles/2006/03/09/flv-streaming-with-lighttpd







Write a Comment

Note: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>