6 use IPC::Open3 qw(open3);
8 use Data::Dump qw(dump);
12 use HTML::TreeBuilder;
16 my $movie = shift @ARGV;
18 sub base_dir { $1 if $_[0] =~ m{^(.+)/[^/]+$} }
20 if ( ! $movie && -e 'media/editing' ) {
21 $movie = 'media/' . readlink('media/editing') . '/video.ogv';
22 warn "using media/editing -> $movie\n";
23 } elsif ( -d $movie && $movie =~ m{media/} ) {
24 $movie .= '/video.ogv';
25 } elsif ( -f $movie && $movie !~ m{video\.ogv} ) {
26 my $movie_master = $movie;
27 my $to = $movie = base_dir($movie) . '/video.ogv';
29 symlink $movie_master, $to;
30 warn "symlink $to -> $movie\n";
31 } elsif ( -f $movie ) {
32 warn "using video $movie";
34 die "Usage: $0 media/conference-Title_of_talk[/video.ogv'\n";
37 my $media_part = my $media_dir = base_dir($movie);
38 $media_part =~ s{media/}{};
40 unlink 'media/editing';
41 symlink $media_part, 'media/editing';
43 warn "# media_part $media_part\n";
45 my $edl = "/dev/shm/edl";
46 my $subtitles = $movie;
47 $subtitles =~ s{\.\w+$}{.srt};
51 my $slide_factor = 4; # 1/4 size of video
58 my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
64 '-vf' => 'screenshot',
67 my $epfd = epoll_create(10);
69 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno STDIN , EPOLLIN ) >= 0 || die $!;
70 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $from_mplayer , EPOLLIN ) >= 0 || die $!;
71 #epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $to_mplayer , EPOLLOUT ) >= 0 || die $!;
74 warn "$movie ", -s $movie, " bytes $edl\n";
75 print $to_mplayer qq|loadfile "$movie"\n|;
76 print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
80 my $term_id = `xdotool getwindowfocus`;
85 $mplayer_id ||= `xdotool search mplayer`;
86 warn "focus_mplayer $mplayer_id\n";
87 system "xdotool windowactivate $mplayer_id"
91 warn "focus_term $term_id\n";
92 system "xdotool windowactivate $term_id";
96 my ( $pos, $osd ) = @_;
98 warn "PREROLL $pos $osd\n";
99 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
100 my $to = $pos - $preroll;
102 print $to_mplayer "set_property time_pos $to\n";
103 print $to_mplayer "get_property time_pos\n";
104 print $to_mplayer "play\n";
117 print $to_mplayer $cmd;
125 if ( ! $prop->{width} || ! $prop->{height} ) {
126 warn "SKIP no size yet\n";
130 if ( ! @subtitles ) {
131 warn "SKIP no subtitles yet\n";
142 foreach my $s ( @subtitles ) {
143 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
144 startTime => $s->[0],
149 if ( $s->[2] =~ m{video:(.+)} ) {
151 my $path = "$media_dir/$video";
153 warn "MISSING $path: $!\n";
155 my $frame_dir = "$media_dir/s/$video";
156 system "mplayer -vo jpeg:outdir=$frame_dir -frames 1 -ss 0 $media_dir/$video"
158 push @videos, [ @$s, $video ];
162 next unless $s->[2] =~ m{\[(\d+)\]};
165 push @{ $sync->{customEvents} }, {
166 startTime => $s->[0],
168 action => 'chapterChange',
170 carousel => 'theCarousel',
173 description => $s->[2],
174 src => sprintf('%s/s/%dx%d/p%03d.jpg', $media_dir, $prop->{width} / $slide_factor, $prop->{height} / $slide_factor, $1),
179 push @slide_t, $s->[0];
182 my $res = $prop->{width} . 'x' . $prop->{height};
184 foreach ( 0 .. $#slide_t ) {
185 push @{ $sync->{htmlEvents}->{'#slide'} }, {
186 startTime => $slide_t[$_],
187 endTime => $slide_t[$_ + 1] || $prop->{length},
188 html => sprintf( '<img src=%s/s/%s/p%03d.jpg>', $media_dir, $res, $_ + 1 ),
192 my @slides_hires = glob '$media_dir/s/hires/p*.jpg';
193 @slides_hires = glob 'shot*.png' unless @slides_hires;
196 foreach my $factor ( 4, 2, 1 ) {
197 my $w = $prop->{width} / $factor;
198 my $h = $prop->{height} / $factor;
200 my $path = "$media_dir/s/${w}x${h}";
201 $factor_s_path->{$factor} = $path;
205 warn "created $path\n";
207 foreach my $hires ( @slides_hires ) {
210 $file =~ s{^.+/(p\d+\.\w)}{$path/$1};
212 my $im = Imager->new( file => $hires );
213 $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
214 warn "resized $file ", -s $file, " bytes\n";
220 my ( $slide_width, $slide_height );
222 my $im = Imager->new( file => $factor_s_path->{ 1 } . '/p001.jpg"')
223 || Imager->new( file => "shot0001.png" ) # from mplayer [s]
227 $slide_width = $im->getwidth / $slide_factor;
228 $slide_height = $im->getheight / $slide_factor;
230 warn "can't find first slide default to 1/$slide_factor of video size\n";
231 $slide_width = $prop->{width} / $slide_factor;
232 $slide_height = $prop->{height} / $slide_factor;
239 width => $slide_width,
240 height => $slide_height,
244 $html5tv->{video_tags} =
251 push @{ $html5tv->{sync}->{customEvents} }, {
252 startTime => $s->[0],
254 action => 'additional_video',
258 description => $s->[2],
259 src => "$media_dir/s/$s->[3]/00000001.jpg",
265 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
266 <source src="$media_dir/$_->[3]" />
273 sub customEvents_sorted {
275 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
277 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
281 sort { $a->{startTime} <=> $b->{startTime} }
282 @{ $html5tv->{sync}->{customEvents} }
287 $_->{args}->{index} = $index++ foreach customEvents_sorted;
289 warn "last customEvent $index\n";
291 $html5tv->{subtitles_table}
292 = qq|<table id="subtitles">|
295 <tr id="sub_$_->{index}">
296 <td class="seek_video">$_->{startTime}</td>
297 <td class="seek_video">$_->{endTime}</td>
298 <td>$_->{args}->{title}</td>
303 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
306 my $hcalendar = '<div style="color: red">Create <tt>hcalendar.html</tt> to fill this space</div>';
307 my $hcal_path = '$media_dir/hcalendar.html';
308 if ( -e $hcal_path ) {
309 $html5tv->{hcalendar} = read_file $hcal_path;
310 my $tree = HTML::TreeBuilder->new;
311 $tree->parse_file($hcal_path);
312 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
313 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
317 warn "# html5tv ", dump $html5tv;
319 my $sync_path = "$media_dir/video.js";
320 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
321 warn "sync $sync_path ", -s $sync_path, " bytes\n";
323 my $html = read_file 'www/tv.html';
324 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
325 warn "no interpolation in template!";
327 write_file "$media_dir.html", $html;
329 my $carousel_width = $prop->{width} + $slide_width - 80;
330 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
331 my $carousel_height = $slide_height + 2;
333 write_file "$media_dir/video.css", qq|
335 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
336 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
337 width: ${carousel_width}px;
338 height: ${carousel_height}px;
341 .jcarousel-skin-ie7 .jcarousel-item {
342 width: ${slide_width}px;
343 height: ${slide_height}px;
348 background-color: #ffc;
352 width: $prop->{width}px;
353 height: $prop->{height}px;
354 font-family: Arial, Helvetica, sans-serif;
355 margin: 0 10px 0px 0;
369 padding: 1px 10px 5px 10px ;
372 width: $html5tv->{video}->{width}px;
377 .jcarousel-skin-ie7 .jcarousel-item img:hover {
378 // border-color: #555 !important;
381 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
382 visibility: visible !important;
385 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
391 font-family: Arial, Verdana;
398 padding: 2px 0 2px 0;
399 width: ${slide_width}px;
404 font-family: monospace;
415 my $hh = int( $t / 3600 );
417 my $mm = int( $t / 60 );
419 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
430 foreach my $s ( @subtitles ) {
432 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
438 write_file $subtitles, $srt;
439 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
441 print $to_mplayer "sub_remove\n";
442 print $to_mplayer qq|sub_load "$subtitles"\n|;
443 print $to_mplayer "sub_select 1\n";
447 @subtitles = YAML::LoadFile "$subtitles.yaml";
448 warn "subtitles ", dump @subtitles;
452 load_subtitles if -e "$subtitles.yaml";
455 print $to_mplayer qq|pause\n|;
457 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
462 print $to_mplayer qq|pause\n|;
466 warn "subtitles ", dump( @subtitles );
469 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
475 preroll $subtitles[ $#subtitles ]->[0], $line;
479 print $to_mplayer qq|get_property time_pos\n|;
480 my $pos = <$from_mplayer>;
481 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
482 warn "# time_pos $1\n";
489 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
494 my $s = ( grep { $_->[0] < $pos } @subtitles )[0] || return;
495 warn "<<<< subtitle ", sub_fmt $s;
496 preroll $s->[0], $s->[2];
497 # print $to_mplayer "set_property time_pos $s->[0]\n";
501 my $pos = time_pos + $preroll;
502 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
503 warn ">>>> subtitle ", sub_fmt $s;
504 preroll $s->[0], $s->[2];
505 # print $to_mplayer "set_property time_pos $s->[0]\n";
508 sub current_subtitle {
509 my $callback = shift;
512 foreach my $nr ( 0 .. $#subtitles ) {
513 my $s = $subtitles[$nr];
514 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
515 warn sub_fmt $s, $pos;
517 $callback->( $visible, $pos ) if $callback;
518 return ( $visible, $pos );
521 warn "# $pos no visible subtitle\n";
526 current_subtitle( sub {
527 my ( $nr, $pos ) = @_;
528 my $new_start = $subtitles[$nr]->[0] += $offset;
529 warn "subtitle $nr $pos $offset $new_start\n";
531 preroll $new_start, "$pos $offset $new_start";
537 # XXX main epool loop
541 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
543 warn "no events" unless $events;
545 foreach my $e ( @$events ) {
546 # warn "# event: ", dump($e), $/;
548 my $fileno = $e->[0];
550 if ( $fileno == fileno STDIN ) {
552 sysread STDIN, $chr, 1;
554 } elsif ( $fileno == fileno $from_mplayer ) {
556 sysread $from_mplayer, $chr, 1;
559 if ( $chr =~ m{[\n\r]} ) {
561 exit if $line =~ m{Exiting};
563 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
565 warn "prop $1 = $2\n";
566 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
568 # XXX keyboard shortcuts
571 : $1 eq ',' ? add_subtitle
572 : $1 eq 'F1' ? prev_subtitle
573 : $1 eq 'F2' ? move_subtitle( -0.3 )
574 : $1 eq 'F3' ? move_subtitle( +0.3 )
575 : $1 eq 'F4' ? next_subtitle
576 : $1 eq 'F5' ? save_subtitles
577 : $1 eq 'F9' ? add_subtitle
578 : $1 eq 'F12' ? edit_subtitles
582 } elsif ( $line =~ m{EDL}i ) {
584 print $to_mplayer qq|osd_show_text "$line"\n|;
586 if ( my $pos = time_pos ) {
587 if ( $line =~ m{start}i ) {
588 push @subtitles, [ $pos, $pos, '-' ];
590 $subtitles[ $#subtitles ]->[1] = $pos;
601 } elsif ( $fileno == fileno $to_mplayer ) {
604 die "invalid fileno $fileno";