6 use IPC::Open3 qw(open3);
8 use Data::Dump qw(dump);
12 use HTML::TreeBuilder;
16 my $movie = shift @ARGV
17 || 'www/media/video.ogv';
18 # || die "usage: $0 path/to/movie.ogv\n";
20 my $edl = "/dev/shm/edl";
21 my $subtitles = $movie;
22 $subtitles =~ s{\.\w+$}{.srt};
26 my $slide_factor = 4; # 1/4 size of video
33 my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
41 my $epfd = epoll_create(10);
43 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno STDIN , EPOLLIN ) >= 0 || die $!;
44 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $from_mplayer , EPOLLIN ) >= 0 || die $!;
45 #epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $to_mplayer , EPOLLOUT ) >= 0 || die $!;
48 warn "$movie ", -s $movie, " bytes $edl\n";
49 print $to_mplayer qq|loadfile "$movie"\n|;
50 print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
54 my $term_id = `xdotool getwindowfocus`;
59 $mplayer_id ||= `xdotool search mplayer`;
60 warn "focus_mplayer $mplayer_id\n";
61 system "xdotool windowactivate $mplayer_id"
65 warn "focus_term $term_id\n";
66 system "xdotool windowactivate $term_id";
70 my ( $pos, $osd ) = @_;
72 warn "PREROLL $pos $osd\n";
73 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
74 my $to = $pos - $preroll;
76 print $to_mplayer "set_property time_pos $to\n";
77 print $to_mplayer "get_property time_pos\n";
78 print $to_mplayer "play\n";
91 print $to_mplayer $cmd;
99 if ( ! $prop->{width} || ! $prop->{height} ) {
100 warn "SKIP no size yet\n";
111 foreach my $s ( @subtitles ) {
112 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
113 startTime => $s->[0],
118 if ( $s->[2] =~ m{video:(.+)} ) {
120 my $path = "www/media/$video";
122 warn "MISSING $path: $!\n";
124 my $frame_dir = "www/media/s/$video";
125 system "mplayer -vo jpeg:outdir=$frame_dir -frames 1 -ss 0 www/media/$video"
127 push @videos, [ @$s, $video ];
131 next unless $s->[2] =~ m{\[(\d+)\]};
134 push @{ $sync->{customEvents} }, {
135 startTime => $s->[0],
137 action => 'chapterChange',
139 carousel => 'theCarousel',
142 description => $s->[2],
143 src => sprintf('media/s/%dx%d/p%03d.jpg', $prop->{width} / $slide_factor, $prop->{height} / $slide_factor, $1),
148 push @slide_t, $s->[0];
151 my $res = $prop->{width} . 'x' . $prop->{height};
153 foreach ( 0 .. $#slide_t ) {
154 push @{ $sync->{htmlEvents}->{'#slide'} }, {
155 startTime => $slide_t[$_],
156 endTime => $slide_t[$_ + 1] || $prop->{length},
157 html => sprintf( '<img src=media/s/%s/p%03d.jpg>', $res, $_ + 1 ),
161 my ( $slide_width, $slide_height );
163 foreach my $factor ( 4, 2, 1 ) {
164 my $w = $prop->{width} / $factor;
165 my $h = $prop->{height} / $factor;
167 my $path = "www/media/s/${w}x${h}";
171 warn "created $path\n";
173 foreach my $hires ( glob 'www/media/s/hires/p*.jpg' ) {
176 $file =~ s{^.+/(p\d+\.\w)}{$path/$1};
178 my $im = Imager->new( file => $hires );
179 $im->scale( xpixels => $w, ypixels => $h, type => 'min' );
180 $im->write( file => $file );
181 warn "resized $file ", -s $file, " bytes\n";
185 if ( $factor == $slide_factor ) {
186 my $im = Imager->new( file => "$path/p001.jpg" );
187 $slide_width = $im->getwidth;
188 $slide_height = $im->getheight;
197 width => $slide_width,
198 height => $slide_height,
202 $html5tv->{video_tags} =
209 push @{ $html5tv->{sync}->{customEvents} }, {
210 startTime => $s->[0],
212 action => 'additional_video',
216 description => $s->[2],
217 src => "media/s/$s->[3]/00000001.jpg",
223 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
224 <source src="media/$_->[3]" />
234 sort { $a->{startTime} <=> $b->{startTime} }
235 @{ $html5tv->{sync}->{customEvents} }
237 $e->{args}->{index} = $index++;
238 warn "e = ", dump $e;
241 warn "last customEvent $index\n";
245 $html5tv->{subtitles_table}
246 = qq|<table id="subtitles">|
250 qq|<tr id="sub_$nr"><td align="right">$_->[0]</td><td align="right">$_->[1]</td><td>$_->[2]</td></tr>|
256 my $hcalendar = '<div style="color: red">Create <tt>hcalendar.html</tt> to fill this space</div>';
257 my $hcal_path = 'www/media/hcalendar.html';
258 if ( -e $hcal_path ) {
259 $html5tv->{hcalendar} = read_file $hcal_path;
260 my $tree = HTML::TreeBuilder->new;
261 $tree->parse_file($hcal_path);
262 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
263 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
267 warn "html5tv ", dump $html5tv;
269 my $sync_path = 'www/media/video.js';
270 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
271 warn "sync $sync_path ", -s $sync_path, " bytes\n";
273 my $html = read_file 'www/tv.html';
274 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
275 warn "no interpolation in template!";
277 write_file 'www/media.html', $html;
279 my $carousel_width = $prop->{width} + Imager->new( file => "www/media/s/$res/p001.jpg" )->getwidth - 80;
280 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
281 my $carousel_height = $slide_height + 2;
283 write_file 'www/media/video.css', qq|
285 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
286 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
287 width: ${carousel_width}px;
288 height: ${carousel_height}px;
291 .jcarousel-skin-ie7 .jcarousel-item {
292 width: ${slide_width}px;
293 height: ${slide_height}px;
298 background-color: #ffc;
302 width: $prop->{width}px;
303 height: $prop->{height}px;
304 font-family: Arial, Helvetica, sans-serif;
305 margin: 0 10px 0px 0;
319 padding: 1px 10px 5px 10px ;
322 width: $html5tv->{video}->{width}px;
327 .jcarousel-skin-ie7 .jcarousel-item img:hover {
328 // border-color: #555 !important;
331 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
332 visibility: visible !important;
335 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
341 font-family: Arial, Verdana;
348 padding: 2px 0 2px 0;
349 width: ${slide_width}px;
361 my $hh = int( $t / 3600 );
363 my $mm = int( $t / 60 );
365 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
376 foreach my $s ( @subtitles ) {
378 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
384 write_file $subtitles, $srt;
385 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
387 print $to_mplayer "sub_remove\n";
388 print $to_mplayer qq|sub_load "$subtitles"\n|;
389 print $to_mplayer "sub_select 1\n";
393 @subtitles = YAML::LoadFile "$subtitles.yaml";
394 warn "subtitles ", dump @subtitles;
398 load_subtitles if -e "$subtitles.yaml";
401 print $to_mplayer qq|pause\n|;
403 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
408 print $to_mplayer qq|pause\n|;
412 warn "subtitles ", dump( @subtitles );
415 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
421 preroll $subtitles[ $#subtitles ]->[0], $line;
425 print $to_mplayer qq|get_property time_pos\n|;
426 my $pos = <$from_mplayer>;
427 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
428 warn "# time_pos $1\n";
435 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
440 my $s = ( grep { $_->[0] < $pos } @subtitles )[0];
441 warn "<<<< subtitle ", sub_fmt $s;
442 preroll $s->[0], $s->[2];
443 # print $to_mplayer "set_property time_pos $s->[0]\n";
447 my $pos = time_pos + $preroll;
448 my $s = ( grep { $_->[0] > $pos } @subtitles )[0];
449 warn ">>>> subtitle ", sub_fmt $s;
450 preroll $s->[0], $s->[2];
451 # print $to_mplayer "set_property time_pos $s->[0]\n";
454 sub current_subtitle {
455 my $callback = shift;
458 foreach my $nr ( 0 .. $#subtitles ) {
459 my $s = $subtitles[$nr];
460 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
461 warn sub_fmt $s, $pos;
463 $callback->( $visible, $pos ) if $callback;
464 return ( $visible, $pos );
467 warn "# $pos no visible subtitle\n";
472 current_subtitle( sub {
473 my ( $nr, $pos ) = @_;
474 my $new_start = $subtitles[$nr]->[0] += $offset;
475 warn "subtitle $nr $pos $offset $new_start\n";
477 preroll $new_start, "$pos $offset $new_start";
483 # XXX main epool loop
487 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
489 warn "no events" unless $events;
491 foreach my $e ( @$events ) {
492 # warn "# event: ", dump($e), $/;
494 my $fileno = $e->[0];
496 if ( $fileno == fileno STDIN ) {
498 sysread STDIN, $chr, 1;
500 } elsif ( $fileno == fileno $from_mplayer ) {
502 sysread $from_mplayer, $chr, 1;
505 if ( $chr =~ m{[\n\r]} ) {
507 exit if $line =~ m{Exiting};
509 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
511 warn "prop $1 = $2\n";
512 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
514 # XXX keyboard shortcuts
517 : $1 eq ',' ? add_subtitle
518 : $1 eq 'F1' ? prev_subtitle
519 : $1 eq 'F2' ? move_subtitle( -0.3 )
520 : $1 eq 'F3' ? move_subtitle( +0.3 )
521 : $1 eq 'F4' ? next_subtitle
522 : $1 eq 'F5' ? save_subtitles
523 : $1 eq 'F9' ? add_subtitle
524 : $1 eq 'F12' ? edit_subtitles
528 } elsif ( $line =~ m{EDL}i ) {
530 print $to_mplayer qq|osd_show_text "$line"\n|;
532 if ( my $pos = time_pos ) {
533 if ( $line =~ m{start}i ) {
534 push @subtitles, [ $pos, $pos, '-' ];
536 $subtitles[ $#subtitles ]->[1] = $pos;
547 } elsif ( $fileno == fileno $to_mplayer ) {
550 die "invalid fileno $fileno";