--- trunk/lib/Frey/Web/Flowplayer.pm 2008/12/11 20:18:28 809 +++ trunk/lib/Frey/Web/FLVPlayer.pm 2008/12/23 21:00:14 883 @@ -1,52 +1,183 @@ -package Frey::Web::Flowplayer; +package Frey::Web::FLVPlayer; use Moose; =head1 SEE ALSO -L +L =cut extends 'Frey'; with 'Frey::Web'; #with 'Frey::Storage'; +with 'Frey::File::FLV'; -has skeleton => ( +use Video::Subtitle::SRT; + +has title => ( + is => 'rw', + isa => 'Str', +); + +has path => ( + is => 'rw', + isa => 'Str', + required => 1, + default => 'var/flv/codeswarm.flv', +); + +has player_swf => ( is => 'rw', isa => 'Str', required => 1, - default => 'skeleton', + default => 'http://flv-player.net/medias/player_flv_maxi.swf', +); + +has FlashVars => ( + is => 'rw', + isa => 'ArrayRef[Str]', + default => sub { [] }, + lazy => 1, # hide it from user ); sub as_markup { my ($self) = @_; - warn $self->skeleton; + my $path = $self->path; + die "can't find $path" unless -e $path; + + my $url = "http://localhost:3000/$path"; # FIXME + + my $swf = $self->player_swf; + my %info = $self->flv_info; + warn "# info ", $self->dump( \%info ); + + my $width = $info{video_width}; + my $height = $info{video_height}; + + $self->FlashVars( [ "flv=$url", "autoload=1", "showtime=1" ] ); + + push @{ $self->FlashVars }, 'showvolume=1' if $info{audio_count} > 0; + + my $subtitles = $self->subtitles_as_markup; + + push @{ $self->FlashVars }, 'title=' . $self->title if $self->title; + + my $FlashVars = join('&', @{ $self->FlashVars }); + + $self->add_css(q| + #subtitle small { + color: #888; + } + |); + + $self->add_js(q| + var _subtitle_active = -1; + + function flv_subtitle(subtitle,nr) { + + // remove current subtitle + if ( _subtitle_active >= 0 ) { + document.getElementById('subtitle-' + _subtitle_active).style.background = '#fff'; + } + + var s = document.getElementById('subtitle'); + if ( subtitle.message ) { + s.innerHTML = '' + + subtitle.message + + ' ' + + nr + ' ' + + + subtitle.timeStart + + ' ... ' + + subtitle.timeEnd + + '' + ; + document.getElementById('subtitle-' + nr).style.background = '#ff0'; + _subtitle_active = nr; + } else { + s.innerHTML = ' '; + _subtitle_active = -1; + } + } + |); + + my $info = $self->dropdown( $self->path, \%info ); qq| - - - - - - - - - - - + + + + + +
 no subtitle
+
+ + +
+ $subtitles +
$info
|; } +sub subtitles_as_markup { + my ( $self ) = @_; + + my $srt = $self->path; + $srt =~ s{\.flv}{.srt}; + my $html = ''; + + if ( -e $srt ) { + push @{ $self->FlashVars }, 'srt=1'; + + my $nr = 0; + + $self->add_js(qq| + function play_from(position) { + var p = document.getElementById('flvplayer'); + p.SetVariable('player:jsSetPosition', position); + p.SetVariable('player:jsPlay',''); + return false; + } + |); + + sub jump_to { + my $t = shift; + my $position = + $1 * 60 * 60 + + $2 * 60 + + $3 + if ( $t =~ m/(\d\d):(\d\d):(\d\d),\d/ ); + ; + + qq|$t|; + } + + my $callback = sub { + my $data = shift; + my $s = jump_to( $data->{start_time} ); + my $e = jump_to( $data->{end_time} ); + $html .= qq|$s$e $data->{text} |; + $nr++; + }; + + my $subtitle = Video::Subtitle::SRT->new($callback); + $subtitle->debug(1); + $subtitle->parse( $srt ); + + $html = qq| + + + + + + $html + +
fromtosubtitle
+ |; + } + + return $html; +} + 1;