1 |
package Frey::Web::FLVPlayer; |
2 |
use Moose; |
3 |
|
4 |
=head1 SCRIPTS |
5 |
|
6 |
=head2 bin/flvplayer-checkout.sh |
7 |
|
8 |
Checkout source code and install required tools to compile modified player |
9 |
|
10 |
=head2 bin/flvplayer-build.sh |
11 |
|
12 |
patch and build modified C<player_flv_maxi.swf> player |
13 |
|
14 |
=head2 bin/flvplayer-diff.sh |
15 |
|
16 |
generate C<patch/flvplayer.diff> diff of local changes |
17 |
|
18 |
=head1 SEE ALSO |
19 |
|
20 |
L<http://flv-player.net/> |
21 |
|
22 |
=cut |
23 |
|
24 |
extends 'Frey'; |
25 |
with 'Frey::Web'; |
26 |
#with 'Frey::Storage'; |
27 |
with 'Frey::File::FLV'; |
28 |
|
29 |
use Video::Subtitle::SRT; |
30 |
|
31 |
has title => ( |
32 |
is => 'rw', |
33 |
isa => 'Str', |
34 |
); |
35 |
|
36 |
has path => ( |
37 |
is => 'rw', |
38 |
isa => 'Str', |
39 |
required => 1, |
40 |
default => 'var/flv/codeswarm.flv', |
41 |
); |
42 |
|
43 |
has player_swf => ( |
44 |
is => 'rw', |
45 |
isa => 'Str', |
46 |
required => 1, |
47 |
default => 'http://flv-player.net/medias/player_flv_maxi.swf', |
48 |
); |
49 |
|
50 |
has FlashVars => ( |
51 |
is => 'rw', |
52 |
isa => 'ArrayRef[Str]', |
53 |
default => sub { [] }, |
54 |
lazy => 1, # hide it from user |
55 |
); |
56 |
|
57 |
sub as_markup { |
58 |
my ($self) = @_; |
59 |
|
60 |
my $path = $self->path; |
61 |
die "can't find $path" unless -e $path; |
62 |
|
63 |
my $url = "http://localhost:3000/$path"; # FIXME |
64 |
|
65 |
my $swf = $self->player_swf; |
66 |
|
67 |
my %info = $self->flv_info; |
68 |
warn "# info ", $self->dump( \%info ); |
69 |
|
70 |
my $width = $info{video_width}; |
71 |
my $height = $info{video_height}; |
72 |
|
73 |
$self->FlashVars( [ "flv=$url", "autoload=1", "showtime=1" ] ); |
74 |
|
75 |
push @{ $self->FlashVars }, 'showvolume=1' if $info{audio_count} > 0; |
76 |
|
77 |
my $subtitles = $self->subtitles_as_markup; |
78 |
|
79 |
push @{ $self->FlashVars }, 'title=' . $self->title if $self->title; |
80 |
|
81 |
my $FlashVars = join('&', @{ $self->FlashVars }); |
82 |
|
83 |
$self->add_css(q| |
84 |
#subtitle small { |
85 |
color: #888; |
86 |
} |
87 |
|); |
88 |
|
89 |
$self->add_js(q| |
90 |
var _subtitle_active = -1; |
91 |
|
92 |
function flv_subtitle(subtitle,nr) { |
93 |
|
94 |
// remove current subtitle |
95 |
if ( _subtitle_active >= 0 ) { |
96 |
document.getElementById('subtitle-' + _subtitle_active).style.background = '#fff'; |
97 |
} |
98 |
|
99 |
var s = document.getElementById('subtitle'); |
100 |
if ( subtitle.message ) { |
101 |
s.innerHTML = '' |
102 |
+ subtitle.message |
103 |
+ ' <small>' |
104 |
+ nr + ' ' + |
105 |
+ subtitle.timeStart |
106 |
+ ' ... ' |
107 |
+ subtitle.timeEnd |
108 |
+ '</small>' |
109 |
; |
110 |
document.getElementById('subtitle-' + nr).style.background = '#ff0'; |
111 |
_subtitle_active = nr; |
112 |
} else { |
113 |
s.innerHTML = ' '; |
114 |
_subtitle_active = -1; |
115 |
} |
116 |
} |
117 |
|); |
118 |
|
119 |
my $info = $self->dropdown( $self->path, \%info ); |
120 |
qq| |
121 |
<object id="flvplayer" type="application/x-shockwave-flash" data="$swf" width="$width" height="$height"> |
122 |
<param name="movie" value="$swf" /> |
123 |
<param name="FlashVars" value="$FlashVars" /> |
124 |
<param name="AllowScriptAccess" value="always"> <!-- domain --> |
125 |
</object> |
126 |
<div id="subtitle"> <small>no subtitle</small></div> |
127 |
<div style="border: 1px slashed #888;"> |
128 |
<input type="button" onclick="document.getElementById('flvplayer').SetVariable('player:jsPlay', '');" value="Play" > |
129 |
<input type="button" onclick="alert(document.getElementById('flvplayer').GetVariable('method:getPosition'))" value="getPosition" > |
130 |
</div> |
131 |
$subtitles |
132 |
<div>$info</div> |
133 |
|; |
134 |
|
135 |
} |
136 |
|
137 |
sub subtitles_as_markup { |
138 |
my ( $self ) = @_; |
139 |
|
140 |
my $srt = $self->path; |
141 |
$srt =~ s{\.flv}{.srt}; |
142 |
my $html = ''; |
143 |
|
144 |
if ( -e $srt ) { |
145 |
push @{ $self->FlashVars }, 'srt=1'; |
146 |
|
147 |
my $nr = 0; |
148 |
|
149 |
$self->add_js(qq| |
150 |
function play_from(position) { |
151 |
var p = document.getElementById('flvplayer'); |
152 |
p.SetVariable('player:jsSetPosition', position); |
153 |
p.SetVariable('player:jsPlay',''); |
154 |
return false; |
155 |
} |
156 |
|); |
157 |
|
158 |
sub jump_to { |
159 |
my $t = shift; |
160 |
my $position = |
161 |
$1 * 60 * 60 + |
162 |
$2 * 60 + |
163 |
$3 |
164 |
if ( $t =~ m/(\d\d):(\d\d):(\d\d),\d/ ); |
165 |
; |
166 |
|
167 |
qq|<a href="#$t" onclick="return play_from($position);">$t</a>|; |
168 |
} |
169 |
|
170 |
my $callback = sub { |
171 |
my $data = shift; |
172 |
my $s = jump_to( $data->{start_time} ); |
173 |
my $e = jump_to( $data->{end_time} ); |
174 |
$html .= qq|<tr id="subtitle-$nr"><td>$s</td><td>$e</td><td> $data->{text} </td></tr>|; |
175 |
$nr++; |
176 |
}; |
177 |
|
178 |
my $subtitle = Video::Subtitle::SRT->new($callback); |
179 |
$subtitle->debug(1); |
180 |
$subtitle->parse( $srt ); |
181 |
|
182 |
$html = qq| |
183 |
<table class="html"> |
184 |
<thead> |
185 |
<tr><th>from</th><th>to</th><th>subtitle</th></tr> |
186 |
</thead> |
187 |
<tbody> |
188 |
$html |
189 |
<tbody> |
190 |
</table> |
191 |
|; |
192 |
} |
193 |
|
194 |
return $html; |
195 |
} |
196 |
|
197 |
1; |