| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Apache2::EmbedFLV - Embed FLV videos into a templated web interface | ||
| 4 | using Flowplayer. | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | On your Apache configuration file: | ||
| 9 | |||
| 10 | <Files ~ "\.flv$"> | ||
| 11 | SetHandler modperl | ||
| 12 | PerlResponseHandler Apache2::EmbedFLV | ||
| 13 | </Files> | ||
| 14 | |||
| 15 | If you want to restrict only a certain directory to serve FLVs using | ||
| 16 | C<Apache2::EmbedFLV>, you can wrap the C<Files> declaration on a | ||
| 17 | C<Directory> block. For more information, take a look at excellent | ||
| 18 | Apache2's documentation. | ||
| 19 | |||
| 20 | By default, you have to have C</flowplayer.swf> and C</flowplayer.controls.swf> | ||
| 21 | available on your webserver. Both SWFs are provided on the GPL | ||
| 22 | distribution of Flowplayer. You can C<alias> them with something like: | ||
| 23 | |||
| 24 | Alias /flowplayer.swf /home/web/flowplayer-3.0.3.swf | ||
| 25 | Alias /flowplayer.controls.swf /home/web/flowplayer.controls.swf | ||
| 26 | |||
| 27 | That's it. Just go to any FLV video within your web server. With that | ||
| 28 | setup, C<Apache2::EmbedFLV> will use a default template. | ||
| 29 | |||
| 30 | =head1 TEMPLATING | ||
| 31 | |||
| 32 | Take a look at the default template located at example/template.tt. | ||
| 33 | That is not the real file used by this module but it's a verbatim copy. | ||
| 34 | The file is placed there just as an example so you can make | ||
| 35 | your own template without too much internal poking. | ||
| 36 | |||
| 37 | Once you have your own template, just C<PerlSetVar> it to the handler: | ||
| 38 | |||
| 39 | <Files ~ "\.flv$"> | ||
| 40 | SetHandler modperl | ||
| 41 | PerlSetVar template /path/to/my/template.tt | ||
| 42 | PerlResponseHandler Apache2::EmbedFLV | ||
| 43 | </Files> | ||
| 44 | |||
| 45 | In order to specify a different path than C</flowplayer.swf> for Flowplayer, | ||
| 46 | just C<PerlSetVar flowplayer> (remember to accompany the controls SWF): | ||
| 47 | |||
| 48 | <Files ~ "\.flv$"> | ||
| 49 | SetHandler modperl | ||
| 50 | PerlSetVar template /path/to/my/template.tt | ||
| 51 | # you would have to have http://yourserver.com/somewhere/flowplayer.swf: | ||
| 52 | PerlSetVar flowplayer /somewhere/flowplayer.swf | ||
| 53 | # or... | ||
| 54 | PerlSetVar flowplayer http://my.other.server/rocks/flowplayer.swf | ||
| 55 | PerlResponseHandler Apache2::EmbedFLV | ||
| 56 | </Files> | ||
| 57 | |||
| 58 | I believe it's pretty obvious that the templating system used and | ||
| 59 | required is L<Template::Toolkit>. Wherever you want to embed the video | ||
| 60 | within, just call: C<[% video %]>. | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | C<Apache2::EmbledFLV> has been already described on the previous section | ||
| 65 | :-) | ||
| 66 | |||
| 67 | However... | ||
| 68 | |||
| 69 | C<Apache2::EmbedFLV> enables Apache to show FLV videos using Flowplayer. | ||
| 70 | This will ease any deployment of FLV video galleries you'd need to do | ||
| 71 | since you could just put the FLVs on an Apache accessible location, and | ||
| 72 | they will be presented on a proper way to your final user. | ||
| 73 | |||
| 74 | =head1 SEE IT IN ACTION | ||
| 75 | |||
| 76 | You can see it in action here: L<http://axiombox.com/apache2-embedflv/flv>. | ||
| 77 | |||
| 78 | =head1 FLOWPLAYER | ||
| 79 | |||
| 80 | Find the Flowplayer distribution on L<http://flowplayer.org>. It is a nice | ||
| 81 | little GPL video player. | ||
| 82 | |||
| 83 | =head1 DOWNLOAD | ||
| 84 | |||
| 85 | Download it at CPAN: L<http://search.cpan.org/~damog>. | ||
| 86 | |||
| 87 | =head1 PROJECT | ||
| 88 | |||
| 89 | You can always see the latest information on this project on: | ||
| 90 | L<http://axiombox.com/apache2-embedflv>. | ||
| 91 | |||
| 92 | Code is hosted at L<http://github.com/damog/apache2-embedflv>. | ||
| 93 | |||
| 94 | =head1 AUTHOR | ||
| 95 | |||
| 96 | David Moreno <david@axiombox.com>, L<http://damog.net/>. | ||
| 97 | Some other similar projects are announced on the Infinite Pig | ||
| 98 | Theorem blog: L<http://log.damog.net>. | ||
| 99 | |||
| 100 | =head1 THANKS | ||
| 101 | |||
| 102 | =over | ||
| 103 | |||
| 104 | =item * Bill Cromie, who allowed me to use my employer's resources to | ||
| 105 | have some fun with this little project. | ||
| 106 | |||
| 107 | =item * Flowplayer L<http://flowplayer.com/>. | ||
| 108 | |||
| 109 | =item * Raquel Hernándex, L<http://maggit.net>, who made the default template. | ||
| 110 | |||
| 111 | =back | ||
| 112 | |||
| 113 | =head1 COPYRIGHT | ||
| 114 | |||
| 115 | Copyright (C) 2009 by David Moreno. | ||
| 116 | |||
| 117 | This library is free software; you can redistribute it and/or modify | ||
| 118 | it under the same terms as Perl itself. | ||
| 119 | |||
| 120 | Flowplayer, also shipped with this distribution, is GPL: | ||
| 121 | L<http://flowplayer.org/download/LICENSE_GPL.txt>. | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | package Apache2::EmbedFLV; | ||
| 126 | |||
| 127 | our $VERSION = '0.2'; | ||
| 128 | |||
| 129 | use Apache2::Request; | ||
| 130 | use Apache2::RequestRec (); | ||
| 131 | use Apache2::RequestIO (); | ||
| 132 | use Apache2::RequestUtil (); | ||
| 133 | use Apache2::Util (); | ||
| 134 | use Apache2::Const -compile => qw/OK :common DIR_MAGIC_TYPE/; | ||
| 135 | use Apache2::EmbedFLV::Template; | ||
| 136 | use Data::Dumper; | ||
| 137 | use Cwd; | ||
| 138 | use Digest::MD5 qw/md5_hex/; | ||
| 139 | |||
| 140 | sub handler { | ||
| 141 | my($r) = shift; | ||
| 142 | |||
| 143 | if(! -e $r->filename) { | ||
| 144 | return Apache2::Const::NOT_FOUND; | ||
| 145 | } elsif(! -r $r->filename) { | ||
| 146 | return Apache2::Const::FORBIDDEN; | ||
| 147 | } else { | ||
| 148 | my $req = Apache2::Request->new($r); | ||
| 149 | |||
| 150 | if( | ||
| 151 | # let's face it, this is why Perl sucks | ||
| 152 | $req->param and | ||
| 153 | scalar keys %{$req->param} == 1 and | ||
| 154 | not $req->param( ${ [ keys %{$param} ] }[0] ) and | ||
| 155 | length ${ [ keys %{$req->param} ] }[0] == 32 | ||
| 156 | ) { | ||
| 157 | my($md5) = keys %{$req->param}; # not too intuitive | ||
| 158 | if($md5 eq md5_hex($r->filename)) { | ||
| 159 | $r->content_type("video/x-flv"); | ||
| 160 | open my $fh, "<", $r->filename or die "Apache2::EmbedFLV wrong $!!"; | ||
| 161 | while(<$fh>) { | ||
| 162 | $r->print($_); | ||
| 163 | } | ||
| 164 | close $fh; | ||
| 165 | return Apache2::Const::OK; | ||
| 166 | } else { | ||
| 167 | return Apache2::Const::FORBIDDEN; | ||
| 168 | } | ||
| 169 | } else { | ||
| 170 | my $md5 = md5_hex($r->filename); | ||
| 171 | $r->content_type("text/html"); | ||
| 172 | |||
| 173 | my $template = $r->dir_config('template'); | ||
| 174 | my $flowplayer = $r->dir_config("flowplayer"); | ||
| 175 | |||
| 176 | my $t = Apache2::EmbedFLV::Template->new($template); | ||
| 177 | $r->print( | ||
| 178 | $t->process( | ||
| 179 | uri => $r->uri, | ||
| 180 | md5 => $md5, | ||
| 181 | flowplayer => $flowplayer, | ||
| 182 | template => $template | ||
| 183 | ) | ||
| 184 | ); | ||
| 185 | |||
| 186 | return Apache2::Const::OK; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | 1; | ||
| 192 | |||
| 193 | __END__ |