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:

ffmpeg -i /var/videos/incoming/video.avi -s 320x240 -ar 44100 -r 12 /var/videos/flv/video.flv

(For more options, take a look at

man ffmpeg


This can take some time, and the output should look something like this:

server1:~# ffmpeg -i /var/videos/incoming/video.avi -s 320x240 -ar 44100 -r 12 /var/videos/flv/video.flv

FFmpeg version SVN-rUNKNOWN, Copyright (c) 2000-2004 Fabrice Bellard

configuration: --enable-gpl --enable-pp --enable-pthreads --enable-mp3lame --enable-vorbis --enable-libogg

--enable-a52 --enable-dts --enable-libgsm --enable-dc1394 --disable-debug --enable-shared --prefix=/usr

libavutil version: 0d.49.0.0

libavcodec version: 0d.51.11.0

libavformat version: 0d.50.5.0

built on Aug 14 2007 15:02:25, gcc: 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)

Input #0, avi, from '/var/videos/incoming/video.avi':

Duration: 00:10:53.8, start: 0.000000, bitrate: 5455 kb/s

Stream #0.0: Video: mpeg4, yuv420p, 1024x576, 24.00 fps(r)

Stream #0.1: Audio: ac3, 48000 Hz, 5:1, 448 kb/s

Output #0, flv, to '/var/videos/flv/video.flv':

Stream #0.0: Video: flv, yuv420p, 320x240, q=2-31, 200 kb/s, 12.00 fps(c)

Stream #0.1: Audio: mp3, 44100 Hz, stereo, 64 kb/s

Stream mapping:

Stream #0.0 -> #0.0

Stream #0.1 -> #0.1

No accelerated IMDCT transform found

Press [q] to stop encoding

frame= 7847 q=2.0 Lsize= 21682kB time=653.8 bitrate= 271.7kbits/s

video:16061kB audio:5108kB global headers:0kB muxing overhead 2.427536%


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:

cat /var/videos/flv/video.flv | flvtool2 -U stdin /var/videos/flv/video.flv

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!

vi /etc/lighttpd/lighttpd.conf

[...] server.modules              = (
# "mod_rewrite",
# "mod_redirect",
# "mod_status",
# "mod_evhost",
# "mod_compress",
# "mod_usertrack",
# "mod_rrdtool",
# "mod_webdav",
# "mod_expire",
# "mod_evasive" )

In the same file, we add also add the following configuration (you can add it right at the end of /etc/lighttpd/lighttpd.conf):

flv-streaming.extensions = ( ".flv" )
secdownload.secret = "somesecret"
secdownload.document-root = "/var/videos/flv/"
secdownload.uri-prefix = "/dl/"
secdownload.timeout = 120

Please replace somesecret with your own secret string (you can choose one).

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




where <token> is an MD5 of

  1. a secret string (user supplied)

  2. <rel-path> (starts with /)

  3. <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.

You can find more information about mod_secdownload here:

Don't forget to restart lighttpd after your changes to /etc/lighttpd/lighttpd.conf:

/etc/init.d/lighttpd restart

No comments: