6 use IPC::Open3 qw(open3);
8 use Data::Dump qw(dump);
12 use HTML::TreeBuilder;
15 my $movie = shift @ARGV
16 || 'www/media/video.ogv';
17 # || die "usage: $0 path/to/movie.ogv\n";
19 my $edl = "/dev/shm/edl";
20 my $subtitles = $movie;
21 $subtitles =~ s{\.\w+$}{.srt};
30 my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
38 my $epfd = epoll_create(10);
40 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno STDIN , EPOLLIN ) >= 0 || die $!;
41 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $from_mplayer , EPOLLIN ) >= 0 || die $!;
42 #epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $to_mplayer , EPOLLOUT ) >= 0 || die $!;
45 warn "$movie ", -s $movie, " bytes $edl\n";
46 print $to_mplayer qq|loadfile "$movie"\n|;
47 print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
51 my $term_id = `xdotool getwindowfocus`;
56 $mplayer_id ||= `xdotool search mplayer`;
57 warn "focus_mplayer $mplayer_id\n";
58 system "xdotool windowactivate $mplayer_id"
62 warn "focus_term $term_id\n";
63 system "xdotool windowactivate $term_id";
67 my ( $pos, $osd ) = @_;
69 warn "PREROLL $pos $osd\n";
70 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
71 my $to = $pos - $preroll;
73 print $to_mplayer "set_property time_pos $to\n";
74 print $to_mplayer "get_property time_pos\n";
75 print $to_mplayer "play\n";
88 print $to_mplayer $cmd;
101 foreach my $s ( @subtitles ) {
102 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
103 startTime => $s->[0],
108 if ( $s->[2] =~ m{video:(.+)} ) {
110 my $path = "www/media/$video";
112 warn "MISSING $path: $!\n";
114 my $frame_dir = "www/media/s/$video";
115 system "mplayer -vo jpeg:outdir=$frame_dir -frames 1 -ss 0 www/media/$video"
117 push @videos, [ @$s, $video ];
121 next unless $s->[2] =~ m{\[(\d+)\]};
123 my $res = ( $prop->{width} / 4 ) . 'x' . ( $prop->{height} / 4 );
125 push @{ $sync->{customEvents} }, {
126 startTime => $s->[0],
128 action => 'chapterChange',
130 carousel => 'theCarousel',
134 description => $s->[2],
135 src => sprintf('media/s/%s/p%03d.jpg', $res, $1),
140 push @slide_t, $s->[0];
143 my $res = $prop->{width} . 'x' . $prop->{height};
145 foreach ( 0 .. $#slide_t ) {
146 push @{ $sync->{htmlEvents}->{'#slide'} }, {
147 startTime => $slide_t[$_],
148 endTime => $slide_t[$_ + 1] || $prop->{length},
149 html => sprintf( '<img src=media/s/%s/p%03d.jpg>', $res, $_ + 1 ),
158 if ( $prop->{width} && $prop->{height} ) {
159 foreach my $factor ( 4, 2, 1 ) {
160 my $w = $prop->{width} / $factor;
161 my $h = $prop->{height} / $factor;
163 $html5tv->{slide} = {
168 my $path = "www/media/s/${w}x${h}";
171 warn "created $path\n";
176 $html5tv->{video_tags} =
183 push @{ $html5tv->{sync}->{customEvents} }, {
184 startTime => $s->[0],
186 action => 'additional_video',
190 description => $s->[2],
191 src => "media/s/$s->[3]/00000001.jpg",
197 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
198 <source src="media/$_->[3]" />
207 $html5tv->{subtitles_table}
208 = qq|<table id="subtitles">|
212 qq|<tr id="sub_$nr"><td align="right">$_->[0]</td><td align="right">$_->[1]</td><td>$_->[2]</td></tr>|
218 my $hcalendar = '<div style="color: red">Create <tt>hcalendar.html</tt> to fill this space</div>';
219 my $hcal_path = 'www/media/hcalendar.html';
220 if ( -e $hcal_path ) {
221 $html5tv->{hcalendar} = read_file $hcal_path;
222 my $tree = HTML::TreeBuilder->new;
223 $tree->parse_file($hcal_path);
224 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
225 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
229 warn "html5tv ", dump $html5tv;
231 my $sync_path = 'www/media/video.js';
232 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
233 warn "sync $sync_path ", -s $sync_path, " bytes\n";
235 my $html = read_file 'www/tv.html';
236 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
237 warn "no interpolation in template!";
239 write_file 'www/media.html', $html;
241 my $carousel_width = ( $html5tv->{slide}->{width} + 6 ) * 6; # 6 = left(2)+right(2)+border(2*1)
242 my $carousel_height = $html5tv->{slide}->{height} + 2;
244 write_file 'www/media/video.css', qq|
246 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
247 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
248 width: ${carousel_width}px;
249 height: ${carousel_height}px;
252 .jcarousel-skin-ie7 .jcarousel-item {
253 width: $html5tv->{slide}->{width}px;
254 height: $html5tv->{slide}->{height}px;
259 background-color: #ffc;
263 width: $html5tv->{video}->{width}px;
264 height: $html5tv->{video}->{height}px;
265 font-family: Arial, Helvetica, sans-serif;
266 margin: 0 10px 0px 0;
280 padding: 1px 10px 5px 10px ;
283 width: $html5tv->{video}->{width}px;
288 .jcarousel-skin-ie7 .jcarousel-item img:hover {
289 // border-color: #555 !important;
292 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
293 visibility: visible !important;
296 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
302 font-family: Arial, Verdana;
309 padding: 2px 0 2px 0;
310 width: $html5tv->{slide}->{width}px;
321 my $hh = int( $t / 3600 );
323 my $mm = int( $t / 60 );
325 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
333 foreach my $s ( @subtitles ) {
335 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
341 write_file $subtitles, $srt;
342 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
344 print $to_mplayer "sub_remove\n";
345 print $to_mplayer qq|sub_load "$subtitles"\n|;
346 print $to_mplayer "sub_select 1\n";
352 @subtitles = YAML::LoadFile "$subtitles.yaml";
353 warn "subtitles ", dump @subtitles;
357 load_subtitles if -e "$subtitles.yaml";
360 print $to_mplayer qq|pause\n|;
362 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
367 print $to_mplayer qq|pause\n|;
371 warn "subtitles ", dump( @subtitles );
374 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
380 preroll $subtitles[ $#subtitles ]->[0], $line;
384 print $to_mplayer qq|get_property time_pos\n|;
385 my $pos = <$from_mplayer>;
386 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
387 warn "# time_pos $1\n";
394 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
399 my $s = ( grep { $_->[0] < $pos } @subtitles )[0];
400 warn "<<<< subtitle ", sub_fmt $s;
401 preroll $s->[0], $s->[2];
402 # print $to_mplayer "set_property time_pos $s->[0]\n";
406 my $pos = time_pos + $preroll;
407 my $s = ( grep { $_->[0] > $pos } @subtitles )[0];
408 warn ">>>> subtitle ", sub_fmt $s;
409 preroll $s->[0], $s->[2];
410 # print $to_mplayer "set_property time_pos $s->[0]\n";
413 sub current_subtitle {
414 my $callback = shift;
417 foreach my $nr ( 0 .. $#subtitles ) {
418 my $s = $subtitles[$nr];
419 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
420 warn sub_fmt $s, $pos;
422 $callback->( $visible, $pos ) if $callback;
423 return ( $visible, $pos );
426 warn "# $pos no visible subtitle\n";
431 current_subtitle( sub {
432 my ( $nr, $pos ) = @_;
433 my $new_start = $subtitles[$nr]->[0] += $offset;
434 warn "subtitle $nr $pos $offset $new_start\n";
436 preroll $new_start, "$pos $offset $new_start";
442 # XXX main epool loop
446 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
448 warn "no events" unless $events;
450 foreach my $e ( @$events ) {
451 # warn "# event: ", dump($e), $/;
453 my $fileno = $e->[0];
455 if ( $fileno == fileno STDIN ) {
457 sysread STDIN, $chr, 1;
459 } elsif ( $fileno == fileno $from_mplayer ) {
461 sysread $from_mplayer, $chr, 1;
464 if ( $chr =~ m{[\n\r]} ) {
466 exit if $line =~ m{Exiting};
468 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
470 warn "prop $1 = $2\n";
471 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
473 # XXX keyboard shortcuts
476 : $1 eq ',' ? add_subtitle
477 : $1 eq 'F1' ? prev_subtitle
478 : $1 eq 'F2' ? move_subtitle( -0.3 )
479 : $1 eq 'F3' ? move_subtitle( +0.3 )
480 : $1 eq 'F4' ? next_subtitle
481 : $1 eq 'F5' ? save_subtitles
482 : $1 eq 'F9' ? add_subtitle
483 : $1 eq 'F12' ? edit_subtitles
487 } elsif ( $line =~ m{EDL}i ) {
489 print $to_mplayer qq|osd_show_text "$line"\n|;
491 if ( my $pos = time_pos ) {
492 if ( $line =~ m{start}i ) {
493 push @subtitles, [ $pos, $pos, '-' ];
495 $subtitles[ $#subtitles ]->[1] = $pos;
506 } elsif ( $fileno == fileno $to_mplayer ) {
509 die "invalid fileno $fileno";