Thursday, August 23, 2007

Build Your Own Video Community With Lighttpd And FlowPlayer (Debian Etch) part 2

5 C'reating Video Directories

In this tutorial I'm assuming that your lighttpd document root for your video web site is /var/www (the default document root for lighttpd on Debian). Of course, we don't want to store the original videos and the FLV videos in the document root (or a subdirectory) to prevent that anyone can download them directly (if he knows the link). Therefore we create a directory for the original videos (e.g. /var/videos/incoming) and a directory for the FLV videos (e.g. /var/videos/flv) outside the document root:

mkdir -p /var/videos/incoming

mkdir -p /var/videos/flv

You (or your users) can then upload their original videos to /var/videos/incoming (e.g. through FTP or some web interface that you program), and you can then encode the videos to FLV (either manually or through some script), as shown in the next chapter.

6 Encoding Videos To FLV

Let's assume we have a video called video.avi in /var/videos/incoming (works for the extensions .mp4 .mov .mpg .3gp .mpeg .wmv as well). We want to convert it to the file video.flv and store it in the directory /var/videos/flv. I want video.flv to have a size of 320x240 pixels with an audio sampling frequency of 44100 Hz and a frame rate of 12 fps. This is how we do it:

Please make sure that in the Output section, you see two streams, one for video, one for audio. If you see video only, this means that the sound gets lost which means you've probably done something wrong in chapters two and three.

After the conversion, we can now add metadata to video.flv with flvtool2:

7 Configuring Lighttpd

Now we have to open lighttpd's main configuration file, /etc/lighttpd/lighttpd.conf, and enable the modules mod_secdownload and mod_flv_streaming in it. It is very important that mod_secdownload is listed before mod_flv_streaming in the server.modules stanza. When I did it the other way round, I found that fast-forwarding the video in FlowPlayer didn't work!

What mod_secdownload does is this: a web application (e.g. a PHP script) can have a link in it of the following form:

<uri-prefix>/<token>/<timestamp-in-hex>/<rel-path>

e.g.

/dl/d8a8cb150f7e5962f6a8443b0b6c6cc2/46c1d9f6/video.flv

where <token> is an MD5 of

a secret string (user supplied)

<rel-path> (starts with /)

<timestamp-in-hex>

mod_secdownload will then map this link to the appropriate file in the secdownload.document-root (which is outside the document root of the web site) and allow access to that file for secdownload.timeout seconds. After secdownload.timeout seconds, the link isn't valid anymore, and access is denied.

After we have installed FlowPlayer, we will use a PHP script to generate the appropriate video links for mod_secdownload.