FLV Streaming Using NGINX and JW Player 5.1

It so happened that a need arose to set up a "mini YouTube". More precisely, the ability to watch video on the web and to be able to "scrub" through it. This feature is especially important when the video is long.

It so happened that a need arose to set up a "mini YouTube". More precisely, the ability to watch video on the web and to be able to "scrub" through it. This feature is especially important when the video is long, since with comparatively short (and small) videos, once the entire file has loaded, this problem is no longer relevant.

After searching far and wide through Google for information on how to get JW Player and Nginx to work together, nothing yielded results. The reason - two things that must be observed:

1. The FLV file must have metadata! Encoding with Adobe Video Encoder does not add such information.
2. In JW Player, under flashvars, provider=http must be specified.

 

About FLV.

Along with Flash's rapid rise in popularity, Adobe's video format - FLV - also gained traction. The format is comparatively new, having appeared in Flash 7, which, as can be read on wikipedia.org, was released only in 2003. I can imagine how slowly end users updated their Flash players to newer versions back when Flash had not yet become so popular. So it would not be right to assume that FLV was already in use in Latvia in 2003 :). Now it is!

Besides FLV there are three alternatives:
1. Play the video file by reading it directly from the server. The result would be having to wait for the entire file before it can be played. The player - whatever the OS supports will play it. Of course, the age-old question of which format to encode video in.
2. Encode the video not in a video format but using one of the container formats - wmv, mov. The benefit - the file can start playing while it is only partially loaded. The drawback - it requires a specific player, and who knows whether the end user will have it or not.
3. Use a media server. All searches for an open-source or reasonably priced option ended in failure. Not to mention adding media files, etc.

Installation.

If you are a shared hosting user - you can stop reading here, as what follows will require fairly broad permissions to modify server settings. But! The good news is that there is pseudo-streaming, which requires only PHP and no server configuration at all [4]. The latter will not require Nginx either.

So - Debian. Fresh, as found on 27 April 2010. Any other Linux distribution will work too, only some things will differ during installation and in directory layout :)
Note that at this time, the nginx package in the Debian repository includes the FLV module.


My configuration:

1. Apache2 on port 8080, which also serves and works with PHP and Ruby.
2. Nginx on port 80, acting as a proxy and forwarding everything to Apache on port 8080. More detail here [2].

Nginx configuration:

server {

  listen 80;

  server_name video.mydomain.tld;

  access_log /var/log/nginx/video.access.log;

  location ~ .flv$ {
    flv;
    root /var/www;
  }

  location / {
    root /var/www;
    index index.html index.htm;
  }

}


3. Encode the video to FLV. Any converter can be used for this purpose. In a Linux environment: ffmpeg -i "01.avi" -y -f flv -ar 44100 "movie.flv"
4. Inject FLV metadata. Also doable with any tool. For example this one [5].
In a Linux environment: flvtool2 -UP movie.flv
5. Download JW Player. Here [3].
6. Prepare the web page:

<div id="flashContent"></div>

<script type="text/javascript">
  var so = new SWFObject('player.swf','flashContent','720','576','9');
  so.addParam('allowfullscreen','true');
  so.addParam('allowscriptaccess','always');
  so.addParam('bgcolor','#FFFFFF');
  so.addParam("flashvars","file=movie.flv&provider=http");
  so.write('flashContent');
</script>

assuming that index.html is in the same directory as the video.


After these steps everything should work! :)


When jumping forward in the player, the nginx logs should show something like this:

"GET /movie.flv?start=27503944 HTTP/1.1" 200 1752013
"GET /movie.flv?start=88336188 HTTP/1.1" 200 13564873


If nothing works - this might help:
- If a 404 error appears in the logs for movie.flv - the root path in the nginx configuration is incorrect.
- If you cannot seek in the player and the log entry shows movie.flv without the "start" attribute - the FLV metadata attributes have not been created or were not created correctly.

 

[1] http://developer.longtailvideo.com/trac/wiki/Player5Formats
[2] http://kovyrin.net/2006/05/18/nginx-as-reverse-proxy/
[3] http://www.longtailvideo.com/players/jw-flv-player/
[4] http://stream.xmoov.com/
[5] http://www.buraks.com/flvmdi/

Share:
Rate: 5 (28)
Views: 7829

comments



What are others reading?