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 $!;
76 warn "$movie ", -s $movie, " bytes $edl\n";
77 print $to_mplayer qq|loadfile "$movie"\n|;
78 print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
83 my $term_id = `xdotool getwindowfocus`;
88 $mplayer_id ||= `xdotool search mplayer`;
89 warn "focus_mplayer $mplayer_id\n";
90 system "xdotool windowactivate $mplayer_id"
94 warn "focus_term $term_id\n";
95 system "xdotool windowactivate $term_id";
99 my ( $pos, $osd ) = @_;
101 warn "PREROLL $pos $osd\n";
102 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
103 my $to = $pos - $preroll;
105 print $to_mplayer "set_property time_pos $to\n";
106 print $to_mplayer "get_property time_pos\n";
107 print $to_mplayer "play\n";
120 print $to_mplayer $cmd;
128 if ( ! $prop->{width} || ! $prop->{height} ) {
129 warn "SKIP no size yet\n";
133 if ( ! @subtitles ) {
134 warn "SKIP no subtitles yet\n";
145 foreach my $s ( @subtitles ) {
146 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
147 startTime => $s->[0],
152 if ( $s->[2] =~ m{video:(.+)} ) {
154 my $path = "$media_dir/$video";
156 warn "MISSING $path: $!\n";
158 my $frame_dir = "$media_dir/s/$video";
159 system "mplayer -vo jpeg:outdir=$frame_dir -frames 1 -ss 0 $media_dir/$video"
161 push @videos, [ @$s, $video ];
165 next unless $s->[2] =~ m{\[(\d+)\]};
168 push @{ $sync->{customEvents} }, {
169 startTime => $s->[0],
171 action => 'chapterChange',
173 carousel => 'theCarousel',
176 description => $s->[2],
177 src => sprintf('%s/s/%dx%d/p%03d.jpg', $media_dir, $prop->{width} / $slide_factor, $prop->{height} / $slide_factor, $1),
182 push @slide_t, $s->[0];
185 foreach ( 0 .. $#slide_t ) {
186 push @{ $sync->{htmlEvents}->{'#slide'} }, {
187 startTime => $slide_t[$_],
188 endTime => $slide_t[$_ + 1] || $prop->{length},
189 html => sprintf( '<img src=%s/s/1/p%03d.jpg>', $media_dir, $_ + 1 ),
193 my @slides_hires = glob '$media_dir/s/hires/p*.jpg';
194 @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/$factor";
204 warn "created $path\n";
206 foreach my $hires ( @slides_hires ) {
209 $file =~ s{^.+/(p\d+\.\w)}{$path/$1};
211 my $im = Imager->new( file => $hires );
212 $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
213 warn "resized $file ", -s $file, " bytes\n";
219 my ( $slide_width, $slide_height );
221 my $im = Imager->new( file => "$media_dir/s/1/p001.jpg" )
222 || Imager->new( file => "shot0001.png" ) # from mplayer [s]
226 $slide_width = $im->getwidth / $slide_factor;
227 $slide_height = $im->getheight / $slide_factor;
229 warn "can't find first slide default to 1/$slide_factor of video size\n";
230 $slide_width = $prop->{width} / $slide_factor;
231 $slide_height = $prop->{height} / $slide_factor;
238 width => $slide_width,
239 height => $slide_height,
243 $html5tv->{html}->{video_tags} =
250 push @{ $html5tv->{sync}->{customEvents} }, {
251 startTime => $s->[0],
253 action => 'additional_video',
257 description => $s->[2],
258 src => "$media_dir/s/1/00000001.jpg",
264 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
265 <source src="$media_dir/$_->[3]" />
272 sub customEvents_sorted {
274 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
276 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
280 sort { $a->{startTime} <=> $b->{startTime} }
281 @{ $html5tv->{sync}->{customEvents} }
286 $_->{args}->{index} = $index++ foreach customEvents_sorted;
288 warn "last customEvent $index\n";
290 $html5tv->{html}->{subtitles_table}
291 = qq|<table id="subtitles">|
294 <tr id="sub_$_->{index}">
295 <td class="seek_video">$_->{startTime}</td>
296 <td class="seek_video">$_->{endTime}</td>
297 <td>$_->{args}->{title}</td>
302 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
305 my $hcalendar = '<div style="color: red">Create <tt>hcalendar.html</tt> to fill this space</div>';
306 my $hcal_path = '$media_dir/hcalendar.html';
307 if ( -e $hcal_path ) {
308 $html5tv->{hcalendar} = read_file $hcal_path;
309 my $tree = HTML::TreeBuilder->new;
310 $tree->parse_file($hcal_path);
311 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
312 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
316 warn "# html5tv ", dump $html5tv;
318 my $sync_path = "$media_dir/video.js";
319 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
320 warn "sync $sync_path ", -s $sync_path, " bytes\n";
322 my $html = read_file 'www/tv.html';
323 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
324 warn "no interpolation in template!";
326 write_file "www/_editing.html", $html;
327 $html =~ s{media/_editing}{media/$media_part}gs;
328 write_file "www/$media_part.html", $html;
330 my $carousel_width = $prop->{width} + $slide_width - 80;
331 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
332 my $carousel_height = $slide_height + 2;
334 write_file "$media_dir/video.css", qq|
336 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
337 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
338 width: ${carousel_width}px;
339 height: ${carousel_height}px;
342 .jcarousel-skin-ie7 .jcarousel-item {
343 width: ${slide_width}px;
344 height: ${slide_height}px;
349 background-color: #ffc;
353 width: $prop->{width}px;
354 height: $prop->{height}px;
355 font-family: Arial, Helvetica, sans-serif;
356 margin: 0 10px 0px 0;
370 padding: 1px 10px 5px 10px ;
373 width: $html5tv->{video}->{width}px;
378 .jcarousel-skin-ie7 .jcarousel-item img:hover {
379 // border-color: #555 !important;
382 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
383 visibility: visible !important;
386 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
392 font-family: Arial, Verdana;
399 padding: 2px 0 2px 0;
400 width: ${slide_width}px;
405 font-family: monospace;
416 my $hh = int( $t / 3600 );
418 my $mm = int( $t / 60 );
420 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
431 foreach my $s ( @subtitles ) {
433 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
439 write_file $subtitles, $srt;
440 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
442 print $to_mplayer "sub_remove\n";
443 print $to_mplayer qq|sub_load "$subtitles"\n|;
444 print $to_mplayer "sub_select 1\n";
448 if ( ! -e "$subtitles.yaml" ) {
449 warn "no subtitles $subtitles to load\n";
452 @subtitles = YAML::LoadFile "$subtitles.yaml";
453 warn "subtitles ", dump @subtitles;
458 print $to_mplayer qq|pause\n|;
460 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
465 print $to_mplayer qq|pause\n|;
469 warn "subtitles ", dump( @subtitles );
472 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
478 preroll $subtitles[ $#subtitles ]->[0], $line;
482 print $to_mplayer qq|get_property time_pos\n|;
483 my $pos = <$from_mplayer>;
484 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
485 warn "# time_pos $1\n";
492 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
497 my $s = ( grep { $_->[0] < $pos } @subtitles )[0] || return;
498 warn "<<<< subtitle ", sub_fmt $s;
499 preroll $s->[0], $s->[2];
500 # print $to_mplayer "set_property time_pos $s->[0]\n";
504 my $pos = time_pos + $preroll;
505 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
506 warn ">>>> subtitle ", sub_fmt $s;
507 preroll $s->[0], $s->[2];
508 # print $to_mplayer "set_property time_pos $s->[0]\n";
511 sub current_subtitle {
512 my $callback = shift;
515 foreach my $nr ( 0 .. $#subtitles ) {
516 my $s = $subtitles[$nr];
517 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
518 warn sub_fmt $s, $pos;
520 $callback->( $visible, $pos ) if $callback;
521 return ( $visible, $pos );
524 warn "# $pos no visible subtitle\n";
529 current_subtitle( sub {
530 my ( $nr, $pos ) = @_;
531 my $new_start = $subtitles[$nr]->[0] += $offset;
532 warn "subtitle $nr $pos $offset $new_start\n";
534 preroll $new_start, "$pos $offset $new_start";
540 # XXX main epool loop
544 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
546 warn "no events" unless $events;
548 foreach my $e ( @$events ) {
549 # warn "# event: ", dump($e), $/;
551 my $fileno = $e->[0];
553 if ( $fileno == fileno STDIN ) {
555 sysread STDIN, $chr, 1;
557 } elsif ( $fileno == fileno $from_mplayer ) {
559 sysread $from_mplayer, $chr, 1;
562 if ( $chr =~ m{[\n\r]} ) {
564 exit if $line =~ m{Exiting};
566 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
568 warn "prop $1 = $2\n";
569 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
571 # XXX keyboard shortcuts
574 : $1 eq ',' ? add_subtitle
575 : $1 eq 'F1' ? prev_subtitle
576 : $1 eq 'F2' ? move_subtitle( -0.3 )
577 : $1 eq 'F3' ? move_subtitle( +0.3 )
578 : $1 eq 'F4' ? next_subtitle
579 : $1 eq 'F5' ? save_subtitles
580 : $1 eq 'F9' ? add_subtitle
581 : $1 eq 'F12' ? edit_subtitles
585 } elsif ( $line =~ m{EDL}i ) {
587 print $to_mplayer qq|osd_show_text "$line"\n|;
589 if ( my $pos = time_pos ) {
590 if ( $line =~ m{start}i ) {
591 push @subtitles, [ $pos, $pos, '-' ];
593 $subtitles[ $#subtitles ]->[1] = $pos;
604 } elsif ( $fileno == fileno $to_mplayer ) {
607 die "invalid fileno $fileno";